简体   繁体   English

如何删除根节点作为完整树删除的一部分

[英]How to delete root node as part of full tree deletion

I am making a binary search tree that has a function that is deleting all nodes in the tree.我正在制作一个二叉搜索树,它有一个 function 正在删除树中的所有节点。 When called later on it seems all nodes are being deleted but the root node.稍后调用时,似乎所有节点都被删除,但根节点除外。 Before adding the conditionals to the code below there were also other nodes that weren't getting deleted.在将条件添加到下面的代码之前,还有其他节点没有被删除。 That's fixed at this time, but the root node is not getting deleted.此时已修复,但根节点并未被删除。 Wondering what conditionals should be added or if there's something I'm not understanding about root deletion.想知道应该添加哪些条件,或者我是否对根删除不了解。

I've tried a simpler solution where no conditionals were used.我尝试了一个不使用条件的更简单的解决方案。 Program ran fine but after calling a traversal again at the end it seems not everything is actually getting deleted.程序运行良好,但在最后再次调用遍历后,似乎并非所有内容都被删除了。

TreeNodePtr deleteTree(TreeNodePtr node)
{


    if(node -> left)
    {
        deleteTree(node -> left);
        printf("Deleting node %s \n", node -> left -> data.word);
        free(node -> left);
        node -> left = NULL;
    }

    if(node -> right)
    {
        deleteTree(node -> right);
        printf("Deleting node %s \n", node -> right -> data.word);
        free(node -> right);
        node -> right = NULL;
    }


    if(allocation_count == 1)
    {
        printf("Deleting node %s \n", node -> data.word);
        free(node);
        node = NULL;
    }


    //whenever a node is deleted this decreases by one, when at one  
    //attempt to delete root node
    allocation_count--;

    return node;

} }

All of the deletion instances are being printed out, but the root is not actually getting removed from the tree.所有的删除实例都被打印出来,但根实际上并没有从树中删除。 One node value remains and gets printed out when a traversal is called after the deletion process.在删除过程之后调用遍历时,会保留一个节点值并打印出来。

Your algorithm immediately starts by deleting the left and right subtrees, and does not delete the root node.您的算法立即从删除左右子树开始,并且不删除根节点。

Remember that trees are a recursive structure, and each subtree has its own root node.请记住,树是递归结构,每个子树都有自己的根节点。 Hence, you should delete node and then recursively call deleteTree on the left and right subtrees.因此,您应该删除node然后在左右子树上递归调用deleteTree

This should work:应该有效:

void deleteTree(TreeNodePtr node)
{
    if(node -> left)
    {
        deleteTree(node -> left);
        node -> left = NULL;
    }

    if(node -> right)
    {
        deleteTree(node -> right);
        node -> right = NULL;
    }

    free(node);
}

The code you show is unneccessarily convoluted, hiding subtle issues.您显示的代码不必要地令人费解,隐藏了微妙的问题。
Anyway, it cannot modify the argument passed, as in C all arguments are passed by value .无论如何,它不能修改传递的参数,如 C 所有 arguments 都是按值传递的。
Condiering you return a TreeNode* , the caller is probably responsible for assigning it to the root-pointer.考虑到您返回TreeNode* ,调用者可能负责将其分配给根指针。

Also, unless you only ever have at most one tree, using a global for allocationCount is wrong, and leads to a bug.此外,除非您最多只有一棵树,否则使用全局allocationCount是错误的,并且会导致错误。

And finally, what if your tree is empty?最后,如果你的树是空的怎么办?

Simplified fixed code:简化的固定代码:

TreeNode* deleteTree(TreeNode* node) {
    if (!node)
        return 0;
    deleteTree(node->left);
    deleteTree(node->right);
    printf("Deleting node %s\n", node->data.word);
    free(node);
    --allocationCount; // Whatever for. Statistics maybe?
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM