2
votes
c - Print the height of binary tree separately - Stack Overflow
Asked
Viewed 119 times
2
int height(struct node* node) 
{ 
    /* base case tree is empty */
    if (node == NULL) 
        return 0; 
  
    /* If tree is not empty then height = 1 + max of left 
      height and right heights */
    return 1 + max(height(node->left), height(node->right)); 
}

Above is very basic code to find the height of binary Tree. But I am not able to print the left and right height separately in the calling function . I tried by passing the call by reference too, but I got unexpected results.

Problem Statement: find the height of binary search tree where a tree with 1 node will be considered as height 0, and a tree with no node will be considered as height -1.

Expected Output: left height :1, right height: 3 (Cat is the root node)

          cat
         /    \
      bear     dog
     /         /   \
 alligator   dear   tiger
             /       /
            cow    lion
                   /
                 horse

This code has to be written in the C language. Is this method correct to find the height?

Following output generated to cross verify tree by inorder traversal

 node:alligator 
 node:bear   bear:->left  alligator
 node:cat    cat:->left  bear     cat:->right dog
 node:cow 
 node:deer   deer:->left  cow
 node:dog    dog:->left  deer     dog:->right tiger
 horse 
 lion   lion:->left  horse
 tiger      tiger:->left  lion
1
  • why expected left height is 1 , per definition it is to be 2? and when node is 'null' height is -1 Nov 20 2020 at 11:46
0

If you want the left and right height separately, find it out separately:

int  left_height = height( root->left) - 1;
int right_height = height(root->right) - 1;
5
  • I tried this way too , but it shows the same value for entire tree. shows 5 ,5 for both tree..
    – john byro
    Nov 20 2020 at 10:35
  • @johnbyro Then the issue is elsewhere, this code is fine.
    – orlp
    Nov 20 2020 at 10:37
  • please read the problem statement . It is not as usual , it returns -1 if no node present , and 1 if one node present . I am not able to interpret this info,
    – john byro
    Nov 20 2020 at 10:43
  • @johnbyro Excuse me? I read your question, please don't assume I didn't. You said for a single node the result should be 0, hence the -1. Your issue lies in the other code, not this code.
    – orlp
    Nov 20 2020 at 11:02
  • I agree with you , i have upadted the question with the cross output of inorder traversal , it shows correctly built tree
    – john byro
    Nov 20 2020 at 11:16
0

Calling printf("%d", height(root->left)) and printf("%d", height(root->right)) on your root node should do the job.

    0

    Write a wrapper function for your height function:

    void printHeight(struct node* root)
    {
        int leftHeight = (root==nullptr) ? 0 : height(root->left);
        int rightHeight = (root==nullptr) ? 0 : height(root->right);
    
        printf("left height: %d, right height: %d", leftHeight-1, rightHeight-1);
    }
    
    3
    • did not worked .. Is the method height() is correct which has been provided in the code??
      – john byro
      Nov 20 2020 at 10:38
    • Updated to do the -1 thing that I had hoped you would have identified yourself as an easy bug fix.
      – selbie
      Nov 20 2020 at 10:49
    • did not work i mean , it is not printing the correct height as expected in the problem statement . However , when i do the inorder traversal , it shows tree is build correctly as expected
      – john byro
      Nov 20 2020 at 10:57

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

    Not the answer you're looking for? Browse other questions tagged or ask your own question.

     
    3
    why expected left height is 1 , per definition it is to be 2? and when node is 'null' height is -1 - TruthSeeker

    3 Answers

    0
    votes

    If you want the left and right height separately, find it out separately:

    int  left_height = height( root->left) - 1;
    int right_height = height(root->right) - 1;
    
    0
    votes

    Calling printf("%d", height(root->left)) and printf("%d", height(root->right)) on your root node should do the job.

    0
    votes

    Write a wrapper function for your height function:

    void printHeight(struct node* root)
    {
        int leftHeight = (root==nullptr) ? 0 : height(root->left);
        int rightHeight = (root==nullptr) ? 0 : height(root->right);
    
        printf("left height: %d, right height: %d", leftHeight-1, rightHeight-1);
    }