search
Log In
27 votes
3.8k views

The height of a tree is defined as the number of edges on the longest path in the tree. The function shown in the pseudo-code below is invoked as height (root) to compute the height of a binary tree rooted at the tree pointer root.

int height(treeptr n)
{ if(n == NULL) return -1;
  if(n -> left == NULL)
     if(n -> right == NULL) return 0;
     else return B1; // Box 1
  
  else{h1 = height(n -> left);
       if(n -> right == NULL) return (1+h1);
       else{h2 = height(n -> right);
            return B2; // Box 2
            }
       }
}

The appropriate expressions for the two boxes B1 and B2 are:

  1. B1: $(1+\text{height}(n \to \text{ right}))$; B2: $(1+\max(h1, h2))$
  2. B1: $(\text{height}(n \to \text{ right}))$ ; B2: $(1+\max(h1,h2))$
  3. B1: $\text{height}(n \to \text{ right})$ ; B2: $\max(h1, h2) $
  4. B1: $(1+ \text{height}(n \to \text{ right}))$ ; B2: $\max(h1, h2)$
in DS
edited by
3.8k views
0

In tree many variation of question can be asked like count no of internal nodes,no of leaf nodes,no of nodes which has only left child or right child , no of nodes with degree 2 and many more.

So next question how can we answer such problems??

so generally code  will be given to us and they would left some blank space and out of given option we have to fill the blank space .

the best approach i can find as of now for verifying option  is Stack method .  In Tree generally node would be called preorder and execution will done postorder. So start running the code by  pushing  root (by taking a simple example)in stack it and and also keep in mind where you have gone to call next node (line no) continue this push and pop operation until  you get the asked value.

if any one is having any other  approaches​​​​​​ then ...please share

1

Assumption: You have the required background knowledge of trees, recursion and stack.


We're using recursion to cut down our problem into smaller parts.

Height of a tree would be equal to height of it's immediate left or right subtree (whichever's maximum) + 1.

Now, that subtree can be treated as a tree in itself, and we call the function recursively with the root of this subtree as the tree.

This process goes on until we reach leafs.

We know that height of a leaf = 0.

So, we return from this recursive call, and keep adding the 1's that we encountered in our stack while placing those recursive calls. This fetches us the height.

Clearly, Option A.

3 Answers

39 votes
 
Best answer

Answer is option A.
From the diagram below we are able to see how this works :

 


edited by
0
is that 1 is added for the root node ?if not plz explain @ arjun sir
0
this is a great explanation @amarVashisth
2
worst explaination
34 votes

I got a hint ; How to quickly approach this type of question :--In this type of questions where we already know we want to find something(i.e. Height ) then simply draw min possible tree which reach us upto the Given Boxes.

For Box1,Box2  tree will be as given in below img :-->

Now Analyse the code for above trees we get Option A as Ans.


edited by
0
didn't understood your logic???
4 votes

1st if condition is true only if there is no node in binary tree.

2nd if condition told if there is only 1 node in the tree.Then height of tree is 0.

Now,

if the tree has at least one left node, then it is going to calculate B2

otherwise, if the tree has  only right subtree, tree height calculated by B1 only

B1= right subtree+1 to get height of the tree

Now, h1= height of right subtree of a node

h2= height of left subtree of a node

height of a tree= 1+ max(h1,h2)

0
Why added '1' in th final calculation, can't understand!
1

h1 is height of left subtree and h2 height of right subtree.

Now, to get total height of tree we have to add root with max (left subtree, right subtree) of the tree.

got it?

1
Actually

if u take 3 node tree(as shown in @Rajesh answer h1 and h2 returning 0, So, there to get height we have to take 1+max(h1,h2)=1+0=1)
0
Leaf node is taken as at zero height therefore addition of one will be there to make height one
Answer:

Related questions

29 votes
4 answers
1
6.9k views
Suppose a circular queue of capacity $(n −1)$ elements is implemented with an array of $n$ elements. Assume that the insertion and deletion operations are carried out using REAR and FRONT as array index variables, respectively. Initially, $REAR = FRONT = 0$. The conditions to detect queue full and ... $(REAR+1) \mod n == FRONT$ full: $(FRONT+1) \mod n == REAR$ empty: $REAR == FRONT$
asked Sep 26, 2014 in DS gatecse 6.9k views
33 votes
2 answers
2
4.2k views
The worst case running time to search for an element in a balanced binary search tree with $n2^{n}$ elements is $\Theta(n\log n)$ $\Theta(n2^n)$ $\Theta(n)$ $\Theta(\log n)$
asked Aug 5, 2014 in DS gatecse 4.2k views
27 votes
5 answers
3
3.4k views
Consider the following C code segment. int a, b, c = 0; void prtFun(void); main() { static int a = 1; /* Line 1 */ prtFun(); a += 1; prtFun(); printf( \n %d %d , a, b); } void prtFun(void) { static int a = 2; /* Line 2 */ int b = 1; a += ++b; printf( \n %d %d , a, ... $\begin{array}{ll} \text{4} & \text{2} \\ \text{4} & \text{2} \\ \text{2} & \text{0} \\ \end{array}$
asked Apr 21, 2016 in Programming jothee 3.4k views
30 votes
2 answers
4
4.5k views
Consider the following relations $A, B$ and $C:$ ... following SQL query contain? SELECT A.Id FROM A WHERE A.Age > ALL (SELECT B.Age FROM B WHERE B.Name = Arun') $4$ $3$ $0$ $1$
asked Apr 21, 2016 in Databases jothee 4.5k views
...