[英]Could someone tell me why the binary tree I am creating in this code using it's preorder and postorder is not being created in the desired way?
I have written this code to create a binary tree using given preorder and inorder traversals. 我已经编写了这段代码,以使用给定的预遍历和有序遍历创建二叉树。 According to the dry run I do, there is no problem, and the tree should be created properly.
根据我的空运行,这没有问题,应该正确地创建树。 But it is not happening that way.
但是事实并非如此。 I have printed the inorder of the tree just to check if the tree has been created of the tree.
我已经打印了树的顺序,只是为了检查树是否已创建。 But instead of the inorder, the postorder is being printed.
但是将打印订单,而不是按顺序打印。 On further investigation I found that the tree itself is not created rightly, that is why the error is coming.
在进一步调查中,我发现树本身没有正确创建,这就是错误即将来临的原因。 There is nothing on the right part of the root which is created.
在创建的根目录的右侧没有任何内容。 But according to the dry run, there is no problem.
但是根据空运行,没有问题。 Could someone help me find my mistake?
有人可以帮我找到我的错误吗?
Sample Input: abdefcghjlkdbfeagcljh k 样本输入:abdefcghjlkdbfeagcljh k
Output: dfebgljkhca 输出:dfebgljkhca
#include <stdio.h>
typedef struct Node
{
int data;
struct node *left;
struct node *right;
}node;
typedef node *tree;
tree root=NULL;
char pre[11];
char in[11];
static int i;
void create( tree temp, int start_left, int end_left, int start_right, int end_right )
{
if ( start_left <= end_left )
{
temp->left= (tree) malloc(sizeof( node ));
temp=temp->left;
temp->left=NULL;
temp->right=NULL;
int j;
for ( j=start_left; j<=end_left; j++)
{
if ( pre[i]==in[j] )
{
temp->data=pre[i];
break;
}
}
i++;
create( temp, start_left, j-1, j+1, end_left );
}
if ( start_right <= end_right )
{
temp->right= (tree) malloc(sizeof( node ));
temp=temp->right;
temp->left=NULL;
temp->right=NULL;
int j;
for ( j=start_right; j<=end_right; j++)
{
if ( pre[i]==in [j] )
{
temp->data=pre[i];
break;
}
}
i++;
create( temp, start_right, j-1, j+1, end_right );
}
return ;
}
void inorder_print(tree temp)
{
if ( temp!=NULL )
{
inorder_print(temp->left);
printf("%c",temp->data);
inorder_print(temp->right);
}
}
int main()
{
for( i=0; i<11; i++)
{
scanf("%c", &pre[i]);
fflush(stdin);
}
for( i=0; i<11; i++)
{
scanf("%c", &in[i]);
fflush(stdin);
}
int j;
for( j=0; j<11; j++)
{
if( pre[0]==in[j] )
{
root= (tree) malloc(sizeof( node ));
root->data=pre[0];
root->left=NULL;
root->right=NULL;
break;
}
}
i=1;
create( root, 0, j-1, j+1, 10);
inorder_print(root);
return 0;
}
What about something more generalized? 那更笼统的呢? My suggestion has the following output:
我的建议具有以下输出:
prorder:
a a b b d c c d e e f f g g h h j j l k k l
inorder:
a a b b c c d d e e f f g g h h j j k k l l
[a]
+--+-----+
[a] [b]
+--+-----------+
[b] [d]
+-----+-----+
[c] [e]
+--+--+ +--+-----+
[c] [d] [e] [f]
+--+-----+
[f] [g]
+--+-----+
[g] [h]
+--+-----+
[h] [j]
+--+-----------+
[j] [l]
+-----+
[k]
+--+--+
[k] [l]
And the code is: 代码是:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct s_tree
{
void *content;
size_t content_size;
struct s_tree *left;
struct s_tree *right;
} t_tree;
/*
** Tree content casted to given type
*/
#define TCONT(tree, t_type) ((t_type)((tree)->content))
/*
** Helpers
*/
void *ft_memdup(void const *ptr, size_t size)
{
void *result;
result = (void*)malloc(size);
if (result == NULL)
return (NULL);
memcpy(result, ptr, size);
return (result);
}
t_tree *ft_tree_new(void const *content, size_t content_size)
{
t_tree *result;
result = (t_tree*)malloc(sizeof(t_tree));
if (result == NULL)
return (NULL);
result->left = NULL;
result->right = NULL;
if (content == NULL)
{
result->content = NULL;
result->content_size = 0;
}
else
{
result->content = ft_memdup(content, content_size);
result->content_size = content_size;
}
return (result);
}
void ft_tree_add(
t_tree **root,
t_tree *new_branch,
int (*cmp)(void*, void*))
{
if (*root == NULL)
*root = new_branch;
else
{
if (cmp((*root)->content, new_branch->content) >= 0)
ft_tree_add(&(*root)->left, new_branch, cmp);
else
ft_tree_add(&(*root)->right, new_branch, cmp);
}
}
void ft_tree_map(t_tree *root, void (*f)(void*), char *map)
{
int i;
if (root != NULL)
for (i = 0; map[i]; i++)
{
switch (map[i])
{
case 'R':
f(root->content);
break;
case 'r':
ft_tree_map(root->right, f, map);
break;
case 'l':
ft_tree_map(root->left, f, map);
break;
}
}
}
/*
** Print tree function
*/
int _print_t(t_tree *tree, int is_left, int offset, int depth, char s[50][256])
{
char b[50];
int width = 3;
int i;
if (!tree)
return 0;
sprintf(b, "[%s]", TCONT(tree, char*));
int left = _print_t(tree->left, 1, offset, depth + 1, s);
int right = _print_t(tree->right, 0, offset + left + width, depth + 1, s);
for (i = 0; i < width; i++)
s[2 * depth][offset + left + i] = b[i];
if (depth && is_left)
{
for (i = 0; i < width + right; i++)
s[2 * depth - 1][offset + left + width/2 + i] = '-';
s[2 * depth - 1][offset + left + width/2] = '+';
s[2 * depth - 1][offset + left + width + right + width/2] = '+';
} else if (depth && !is_left)
{
for (int i = 0; i < left + width; i++)
s[2 * depth - 1][offset - width/2 + i] = '-';
s[2 * depth - 1][offset + left + width/2] = '+';
s[2 * depth - 1][offset - width/2 - 1] = '+';
}
return left + width + right;
}
void print_tree(t_tree *tree)
{
char s[50][256];
for (int i = 0; i < 50; i++)
sprintf(s[i], "%100s", " ");
_print_t(tree, 0, 0, 0, s);
for (int i = 0; i < 50; i++)
printf("%s\n", s[i]);
}
//end of print tree function
void print_str(char *str)
{
printf("%s ", str);
}
int main()
{
t_tree *root;
char input[] = "abdefcghjlkdbfeagcljhk";
char tmp[2];
int i;
tmp[1] = 0;
root = NULL;
for (i = 0; input[i]; i++)
{
tmp[0] = input[i];
ft_tree_add(&root,
ft_tree_new(tmp, strlen(tmp) + 10),
(int (*)(void*, void*))(&strcmp));
}
//Preorder
printf("preorder:\n");
ft_tree_map(root, (void (*)(void*))(&print_str), "Rlr");
printf("\n");
//Inorder
printf("inorder:\n");
ft_tree_map(root, (void (*)(void*))(&print_str), "lRr");
printf("\n");
print_tree(root);
}
If you want to change the way of insertion, make your own comparison function and change &strcmp
with &your_function
. 如果要更改插入方式,请创建自己的比较函数,并使用
&your_function
更改&strcmp
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.