删除的二进制树有不正确的后续遍历

发布于 2025-02-04 09:45:10 字数 3464 浏览 3 评论 0原文

我有一个问题,现在真的让我很烦。我想制作一棵二进制树,将其删除,然后创建一个新的树。我一直在尝试自己制作该程序,但我一直陷入问题上:使用后遍历删除二进制树后,我尝试制造一棵新的二进制树。但是最初的树尚未被删除,因此实际上我要添加到树上,而不是制作一棵新树。

我以为我在做一些非常错误的事情,所以我在网上找到了一些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: Binary Tree in C code - output

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囍孤女 2025-02-11 09:45:10

如评论中所述,这可能是由于未将删除树的指针设置为null

以下是deltree的修改版本,该版本将删除树的指针设置为null

void deltree(node** tree) // pass by reference 
{
    if (*tree)
    {
        deltree(&(*tree)->left);
        deltree(&(*tree)->right);
        free(*tree);
    }
    *tree = NULL; // set the pointer to the deleted tree to 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 to null.

void deltree(node** tree) // pass by reference 
{
    if (*tree)
    {
        deltree(&(*tree)->left);
        deltree(&(*tree)->right);
        free(*tree);
    }
    *tree = NULL; // set the pointer to the deleted tree to null
}

Note that in order for the deltree argument to be modified within the deltree function you will have to pass a pointer to that pointer, for example deltree(root); should now be deltree(&root);. This is known as pass by pointer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文