c - 二叉树删除-看不懂一些指针

标签 c binary-search-tree

我对这个二叉树删除有疑问,代码是

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int key;
    struct node *left, *right;
};
struct node *newNode(int item)
{
    struct node *temp =  (struct node *)malloc(sizeof(struct node));
    temp->key = item;
    temp->left = temp->right = NULL;
    return temp;
}
void inorder(struct node *root)
{
    if (root != NULL)
    {
        inorder(root->left);
        printf("%d ", root->key);
        inorder(root->right);
    }
}
struct node* insert(struct node* node, int key)
{
    if (node == NULL) return newNode(key);

    if (key < node->key)
        node->left  = insert(node->left, key);
    else
        node->right = insert(node->right, key);
    return node;
}
struct node * minValueNode(struct node* node)
{
    struct node* current = node;
     while (current->left != NULL)
        current = current->left;

    return current;
}
struct node* deleteNode(struct node* root, int key)
{
   if (root == NULL) return root;

   if (key < root->key)
        root->left = deleteNode(root->left, key);
     else if (key > root->key)
        root->right = deleteNode(root->right, key);
   else
    {
        if (root->left == NULL)
        {
            struct node *temp = root->right;
            free(root);
            return temp;
        }
        else if (root->right == NULL)
        {
            struct node *temp = root->left;
            free(root);
            return temp;
        }

    struct node* temp = minValueNode(root->right);

         root->key = temp->key;

       root->right = deleteNode(root->right, temp->key);
    }
    return root;
}

int main()
{
    struct node *root = NULL;
    root = insert(root, 50);
    root = insert(root, 30);
    root = insert(root, 20);
    root = insert(root, 40);
    root = insert(root, 70);
    root = insert(root, 60);
    root = insert(root, 80);

    printf("Inorder traversal of the given tree \n");
    inorder(root);

    printf("\nDelete 20\n");
    root = deleteNode(root, 20);
    printf("Inorder traversal of the modified tree \n");
    inorder(root);

    printf("\nDelete 30\n");
    root = deleteNode(root, 30);
    printf("Inorder traversal of the modified tree \n");
    inorder(root);

    printf("\nDelete 50\n");
    root = deleteNode(root, 50);
    printf("Inorder traversal of the modified tree \n");
    inorder(root);

    return 0;
}

我无法理解的台词是:

if (root->left == NULL)
        {
            struct node *temp = root->right;
            free(root);
            return temp;
        }
        else if (root->right == NULL)
        {
            struct node *temp = root->left;
            free(root);
            return temp;
        }

这段代码是否实际上返回了 NULL 值,那么我可以在这两种情况下将代码重写为 struct node *temp=NULL 但是当我这样做时,最后的 inorder 值不显示。

最佳答案

您查询的代码是处理要删除的节点是树的根节点的代码的一部分。它检查根节点是否只有一个子节点(或零个),在这种情况下,只需将一个子节点设为树的新根即可执行删除操作。

请特别注意,当 root->left == NULL 时,它是 right child ,而不是左边的 child ,被选为新的根(并且暂时记录在 temp) 中,反之亦然。无论哪种方式,原始根节点都被释放,因为它不再在树中,并且无法通过函数即将返回的新根指针访问。

仅当根节点最初是树中的唯一节点时,该函数才返回 NULL

关于c - 二叉树删除-看不懂一些指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31570186/

相关文章:

c++ - 用于在 OSX 中查看/绘制 C 或 C++ 源文件的函数调用层次结构的工具

C 可变参数 - va_copy 问题

c++ - 遍历二叉搜索树

algorithm - 给定后序遍历如何构建BST

c - 我的树程序在插入一个根节点后崩溃

c# - malloc 和 Marshal.AllocHGlobal 之间有什么区别?

c - 在 C 中使用梯形法则对某些值给出错误答案的积分

data-structures - 在序言中根据遍历查找二叉搜索树

c++ - "boolean short circuiting"是由标准规定的还是仅用作优化?

algorithm - 树 : Performance comparison between stack implementation and recursive call of Traversal in BST