Getting 0 instead of null for binary search tree

I have been trying to clear my binary tree and when doing so I get a 0 instead of null from the code below. The tree is defined to be empty when it references NULL but that's not happening. The error can be seen in the traversing over the tree. How can I change it so that when the second set of numbers are inserted, the 0 doesn't show?

    #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");      

    }

Answers


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

You assign NULL to value, but value is int so the NULL ptr gets converted to int type – and the result of such conversion is a number zero.

By the way, assigning NULL to the pointer variable replaces the value of the pointer, but does not release the memory block, which the pointer was pointing at.


Need Your Help

MigratorDotNet and Text Datatype on SQL Server 2008

sql-server-2008 migratordotnet

I am using migratordotnet to sync my SQLServer 2008 database and need to create a column of type Text...

How is this code correct?

c++ arrays

I have downloaded a code, it looks correct (and runs):