c - 对于二叉搜索树,获取 0 而不是 null

标签 c binary-search-tree

我一直在尝试清除我的二叉树,当这样做时,我从下面的代码中得到 0 而不是 null。当树引用 NULL 时,它被定义为空,但这种情况并没有发生。在遍历树时可以看到错误。如何更改它以便在插入第二组数字时不显示 0?

    #include <stdio.h>
    #include <stdlib.h>

    typedef struct Node{
            int value;
            struct Node * left;
            struct Node * right;

    } Node;

    Node * insert(Node * node, int value){
        if(node == NULL){
            Node *temp;
            temp = (Node *)malloc(sizeof(Node));
            temp->value = value;
            temp->left = temp->right = NULL;
            return temp;
        }
        if(value >(node->value)){
            node->right = insert(node->right,value);
        }
        else if(value < (node->value)){
            node->left = insert(node->left,value);
        }
        return node;
    }

    void inorder(Node * node){
        if(node == NULL) {
            return;
        }
        inorder(node->left);
        printf("%d ", node->value);
        inorder(node->right);
    }

    void preorder(Node * node){
            if(node == NULL){
                return;
            }
            printf("%d ",node->value);
            preorder(node->left);
            preorder(node->right);
    }

    void postorder(Node *node){
            if(node == NULL){
                return;
            }
            postorder(node->left);
            postorder(node->right);
            printf("%d ",node->value);
    }

   /* issue here is produces a 0 instead of null and that can be seen in the traversing :( */
    void empty(Node * node) {
        if (node->left)
            node->left = NULL;
        if (node->right)
            node->right = NULL;
        if (node->value)
            node->value = NULL;
        node->value = NULL;
    }

    int main(){

            Node * root = NULL;
            root = insert(root, 5);
            root = insert(root, -1);
            root = insert(root, 3);
            root = insert(root, -14);
            root = insert(root, 8);
            root = insert(root, 10);
            root = insert(root, 9);
            root = insert(root, 6);

            inorder(root);
            printf("\n");
            preorder(root);
            printf("\n");
            postorder(root);
            printf("\n");

            empty(root);

            root = insert(root, 1);
            root = insert(root, 2);       
            root = insert(root, 3); 

            inorder(root);
            printf("\n");
            preorder(root);
            printf("\n");
            postorder(root);
            printf("\n");      

    }

最佳答案

void empty(Node ** node) {
    if(*node){
        empty(&(*node)->left);
        empty(&(*node)->right);
        free(*node);
        *node = NULL;
    }
}
...
//at main
empty(&root);

关于c - 对于二叉搜索树,获取 0 而不是 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22765870/

相关文章:

c - 在 C 中取消引用结构变量

data-structures - 将非重叠范围映射到值的数据结构?

java - 返回二叉搜索树的高度

c - 树元素未按正确顺序显示

c - 是否有一种排序算法使用二叉树按位排序?

c - 试图学习 C,但我遇到了奇怪的错误(至少对我而言)

c - boolean 表达式求值器错误

c - 多个线程之一的 pthread_join

algorithm - 我试图在时间 O(1) 的二叉搜索树中找到一个键的后继

c++ - 内部路径长度函数的问题