answersLogoWhite

0


Best Answer

Really the best way to traverse any binary tree is recursion. In this case we are going to be some node defined as having 3 values, a pointer to a left node, a pointer to a right node, and a value.

then in psudocode we can do it as:

int height(node n, int depth){

int leftDepth;

int rightDepth;

if(n.left != NULL)

leftDepth = height(n.left, depth+1)

else

leftDepth = depth;

if(n.right != NULL)

rightDepth = height(n.right, depth+1)

else

rightDepth = depth;

if(leftDepth > rightDepth) return leftDepth;

return rightDepth;

}

Essentially what you are doing is calling the algorithm on both the left and right nodes which in turn will call it on their left and right nodes, down to where all the nodes are null. Then what is returned is the greater depth of the two; because it will traverse before returning a depth, and only traverses if there is a deeper node, it will return the depth of the deepest node, or the height of the binary tree.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

int maxDepth( Node* node /* = NULL */ )

{

if( !node )

return( 0 );

int l = maxDepth( node->left );

int r = maxDepth( node->right );

return( 1 + ( l>r?l:r ));

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is recursive algorithm to find the height of a binary search tree with n numbers of nodes?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Algorithm that prompts a user to entre his or her height and store the user input in a variable height?

int height; print("Enter height"); height=getString();


Design an algorithm that prompts the user to enter his or her height and stores the user's input in a variable name height?

int height; print("Enter height"); height=getString();


Height of a binary heap?

log2(N+1)


The height of Complete Binary tree is in terms of :option1. n2. logn3. n^2?

The height of a complete binary tree is in terms of log(n) where n is the number of nodes in the tree. The height of a complete binary tree is the maximum number of edges from the root to a leaf, and in a complete binary tree, the number of leaf nodes is equal to the number of internal nodes plus 1. Since the number of leaf nodes in a complete binary tree is equal to 2^h where h is the height of the tree, we can use log2 to find the height of a complete binary tree in terms of the number of nodes.


How to find height of subtree in a Binary tree?

Check this out! http://stackoverflow.com/questions/575772/the-best-way-to-calculate-the-height-in-a-binary-search-tree-balancing-an-avl


What is the height of binary search tree in worst case?

In the worst case a binary search tree is linear and has a height equal to the number of nodes. so h=O(h).


What are the properties of binary tree?

A binary tree of n elements has n-1 edgesA binary tree of height h has at least h and at most 2h - 1 elementsThe height of a binary tree with n elements is at most n and at least ?log2 (n+1)?


What is the height of a binary tree with n nodes in the worst case?

For the height `h' of a binary tree, for which no further attributes are given than the number `n' of nodes, holds:ceil( ld n)


Minimum height of a binary tree having 1000 nodes?

11


How many leaf nodes does the full binary tree of height h 3 have?

For a full binary tree of height 3 there are 4 leaf nodes. E.g., 1 root, 2 children and 4 grandchildren.


What is the minimum no of nodes in a complete binary tree of height h?

h+1


Write an algorithm to calculate the area of a triangle?

Area of any triangle: 0.5*base*perpendicular height