删除的二进制树有不正确的后续遍历
我有一个问题,现在真的让我很烦。我想制作一棵二进制树,将其删除,然后创建一个新的树。我一直在尝试自己制作该程序,但我一直陷入问题上:使用后遍历删除二进制树后,我尝试制造一棵新的二进制树。但是最初的树尚未被删除,因此实际上我要添加到树上,而不是制作一棵新树。
我以为我在做一些非常错误的事情,所以我在网上找到了一些C代码,该c代码与我的代码相同的核心功能( c代码中的二进制树):
#include<stdlib.h>
#include<stdio.h>
struct bin_tree {
int data;
struct bin_tree * right, * left;
};
typedef struct bin_tree node;
void insert(node ** tree, int val)
{
node *temp = NULL;
if(!(*tree))
{
temp = (node *)malloc(sizeof(node));
temp->left = temp->right = NULL;
temp->data = val;
*tree = temp;
return;
}
if(val < (*tree)->data)
{
insert(&(*tree)->left, val);
}
else if(val > (*tree)->data)
{
insert(&(*tree)->right, val);
}
}
void print_preorder(node * tree)
{
if (tree)
{
printf("%d\n",tree->data);
print_preorder(tree->left);
print_preorder(tree->right);
}
}
void print_inorder(node * tree)
{
if (tree)
{
print_inorder(tree->left);
printf("%d\n",tree->data);
print_inorder(tree->right);
}
}
void print_postorder(node * tree)
{
if (tree)
{
print_postorder(tree->left);
print_postorder(tree->right);
printf("%d\n",tree->data);
}
}
void deltree(node * tree)
{
if (tree)
{
deltree(tree->left);
deltree(tree->right);
free(tree);
}
}
node* search(node ** tree, int val)
{
if(!(*tree))
{
return NULL;
}
if(val < (*tree)->data)
{
search(&((*tree)->left), val);
}
else if(val > (*tree)->data)
{
search(&((*tree)->right), val);
}
else if(val == (*tree)->data)
{
return *tree;
}
}
void main()
{
node *root;
node *tmp;
root = NULL;
/* Inserting nodes into tree */
insert(&root, 9);
insert(&root, 4);
insert(&root, 15);
insert(&root, 6);
insert(&root, 12);
insert(&root, 17);
insert(&root, 2);
/* Printing nodes of tree */
printf("Pre Order Display\n");
print_preorder(root);
printf("In Order Display\n");
print_inorder(root);
printf("Post Order Display\n");
print_postorder(root);
/* Search node into tree */
tmp = search(&root, 4);
if (tmp)
{
printf("Searched node=%d\n", tmp->data);
}
else
{
printf("Data Not found in tree.\n");
}
/* Deleting all nodes of tree */
deltree(root);
printf("Tree has been deleted\n");
insert(&root, 90);
insert(&root, 40);
insert(&root, 150);
insert(&root, 60);
insert(&root, 120);
insert(&root, 170);
insert(&root, 20);
print_inorder(root);
}
但是奇怪的是,我在网上发现的代码与我的问题相同。在上面的代码中,在main()
函数中,将树被删除,然后将新节点添加到树中。但是,一旦添加了节点并将树在有序中打印出来,就可以看出输出是两棵树的组合,而不是第二个树的组合。
换句话说,此代码与我的问题完全相同。如何解决上述代码的问题?作为参考,我将CodeBlocks用作编译器,并且可以在此处找到程序的输出,可以在此处找到:
注意:我已经在Internet上包含了一个代码,因为我想要尝试解决精确描述的问题而不共享我的个人代码太多。
I have an issue and it is really annoying me right now. I want to make a binary tree, delete it and then create a new one. I have been trying to make the program myself but I keep getting stuck on the issue: After deleting the binary tree using post-order traversal I try to make a new binary tree. But the initial tree has not been deleted, so in practice I am adding to the tree instead of making a new one.
I thought that I was doing something horribly wrong so I found some C code online, which executes the same core functionality as my code, source (Binary Tree in C code):
#include<stdlib.h>
#include<stdio.h>
struct bin_tree {
int data;
struct bin_tree * right, * left;
};
typedef struct bin_tree node;
void insert(node ** tree, int val)
{
node *temp = NULL;
if(!(*tree))
{
temp = (node *)malloc(sizeof(node));
temp->left = temp->right = NULL;
temp->data = val;
*tree = temp;
return;
}
if(val < (*tree)->data)
{
insert(&(*tree)->left, val);
}
else if(val > (*tree)->data)
{
insert(&(*tree)->right, val);
}
}
void print_preorder(node * tree)
{
if (tree)
{
printf("%d\n",tree->data);
print_preorder(tree->left);
print_preorder(tree->right);
}
}
void print_inorder(node * tree)
{
if (tree)
{
print_inorder(tree->left);
printf("%d\n",tree->data);
print_inorder(tree->right);
}
}
void print_postorder(node * tree)
{
if (tree)
{
print_postorder(tree->left);
print_postorder(tree->right);
printf("%d\n",tree->data);
}
}
void deltree(node * tree)
{
if (tree)
{
deltree(tree->left);
deltree(tree->right);
free(tree);
}
}
node* search(node ** tree, int val)
{
if(!(*tree))
{
return NULL;
}
if(val < (*tree)->data)
{
search(&((*tree)->left), val);
}
else if(val > (*tree)->data)
{
search(&((*tree)->right), val);
}
else if(val == (*tree)->data)
{
return *tree;
}
}
void main()
{
node *root;
node *tmp;
root = NULL;
/* Inserting nodes into tree */
insert(&root, 9);
insert(&root, 4);
insert(&root, 15);
insert(&root, 6);
insert(&root, 12);
insert(&root, 17);
insert(&root, 2);
/* Printing nodes of tree */
printf("Pre Order Display\n");
print_preorder(root);
printf("In Order Display\n");
print_inorder(root);
printf("Post Order Display\n");
print_postorder(root);
/* Search node into tree */
tmp = search(&root, 4);
if (tmp)
{
printf("Searched node=%d\n", tmp->data);
}
else
{
printf("Data Not found in tree.\n");
}
/* Deleting all nodes of tree */
deltree(root);
printf("Tree has been deleted\n");
insert(&root, 90);
insert(&root, 40);
insert(&root, 150);
insert(&root, 60);
insert(&root, 120);
insert(&root, 170);
insert(&root, 20);
print_inorder(root);
}
But the weird thing is, that the code I have found online has the same issue as mine. In the code above, in the main()
function the tree is being deleted, and then new nodes are added to the tree. But once the nodes are added and the tree is printed inorder, it can be seen that the output is a combination of the two trees and not the second tree on its own.
In other words, this code has the exact same issue as mine. How can the issue for the above code be fixed? For reference, I am using CodeBlocks as my compiler and the output of the program with the issue I describe can be found here:
Note: I have included a code off the internet since I want to try and solve the exact described issue without sharing too much of my personal code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如评论中所述,这可能是由于未将删除树的指针设置为
null
。以下是
deltree
的修改版本,该版本将删除树的指针设置为null
。请注意,为了使要在
deltree
函数中修改的deltree
参数,您将必须将指针传递给该指针,例如deltree(root);
现在应为deltree(&amp; root);
。这被指针称为通行证。As said in the comments, this is probably due to not setting the pointer to the deleted tree to
null
.The following is a modified version of
deltree
that sets the pointer to the deleted tree tonull
.Note that in order for the
deltree
argument to be modified within thedeltree
function you will have to pass a pointer to that pointer, for exampledeltree(root);
should now bedeltree(&root);
. This is known as pass by pointer.