Search code examples
cbinary-treebinary-search-tree

Why am I not getting the last print statement, unable to get height of the tree?


After taking values for nodes, program terminates. I don't know what I am doing wrong. I don't even know if the values are getting inserted.

struct Node  
{ 
  int data; 
  struct Node* left; 
  struct Node* right; 
};

struct Node* GetNode(int data){
    struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
    newnode->data=data;
    newnode->left = NULL;
    newnode->right = NULL;
}

struct Node* insert(struct Node* root,int data){
    if(root=NULL){
        root = GetNode(data);
        return root;
    }
    else if(data <= root->data){
        root->left = insert(root->left,data);
    }
    else{
        root->right = insert(root->right,data);
    }
    return root;
}

int height(struct Node* root){
    if(root==NULL){
        return 0;
    }
    else{
        int lh = height(root->left);
        int rh = height(root->right);
        if(lh>rh){
            return (lh+1);
        }
        else{
            return (rh+1);
        }
    }
}  

int main() {
    
    struct Node* root =NULL;
    int n,i;
    printf("Enter the total number of nodes in the tree: ");
    scanf("%d",&n);
    int value[n];
    for(i=0;i<n;i++){
        printf("Enter the %d node ",i+1);
        scanf("%d",&value[i]);  
    }
    for(i=0;i<n;i++){
        root = insert(root,value[i]);
    }
    int high = height(root);
    printf("Height of the given tree is : %d ",high);
    
    return 0;
}

No, idea if it is the height function or the insert function.


Solution

  • You have 2 problems in your code.

    1. In the function struct Node* GetNode(int data), you are not returning the newnode.
    2. In the function struct Node* insert(struct Node* root,int data), in the if condition if(root=NULL), you should use ==

    Here is the corrected working code:

    struct Node  
    { 
      int data; 
      struct Node* left; 
      struct Node* right; 
    };
    
    struct Node* GetNode(int data){
        struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
        newnode->data=data;
        newnode->left = NULL;
        newnode->right = NULL;
        return newnode;
    }
    
    struct Node* insert(struct Node* root,int data){
        if(root==NULL){
            root = GetNode(data);
            return root;
        }
        else if(data <= root->data){
            root->left = insert(root->left,data);
        }
        else{
            root->right = insert(root->right,data);
        }
        return root;
    }
    
    int height(struct Node* root){
        if(root==NULL){
            return 0;
        }
        else{
            int lh = height(root->left);
            int rh = height(root->right);
            if(lh>rh){
                return (lh+1);
            }
            else{
                return (rh+1);
            }
        }
    }  
    
    int main() {
        struct Node* root =NULL;
        int n,i;
        printf("Enter the total number of nodes in the tree: ");
        scanf("%d",&n);
        int value[n];
        for(i=0;i<n;i++){
            printf("Enter the %d node ",i+1);
            scanf("%d",&value[i]);  
        }
        for(i=0;i<n;i++){
            root = insert(root, value[i]);
        }
        int high = height(root);
        printf("Height of the given tree is : %d ",high);
        
        return 0;
    }