@sharad height will be log2(n) only in case of balanced BST. what if its
terribly unbalanced, you may get height as 'n' as well ! :)
So you will have to go till the bottom of the tree to see the depth and find
the height accordingly.
Anurag Sharma
On Wed, Jun 16, 2010 at 9:12 AM, Anurag Sharma
Write a pseudo code 4 that..using c/c++...
how can we find the depth(height) of BST ?
--
You received this message because you are subscribed to the Google Groups
Algorithm Geeks group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group,
here's a recursive solution
int depth(Node n){
if (n==NULL)
return 0;
else
return 1 + max( depth( n.right ) , depth( n.left ) );
}
calling depth(root) will yield the height of the tree
On 6/15/10, ajay kumar ajaykr@gmail.com wrote:
Write a pseudo code 4 that..using
@ajay:recursively count the number of nodes then use formula h=log2(number
of nodes)
On Wed, Jun 16, 2010 at 5:39 AM, Amir hossein Shahriari
amir.hossein.shahri...@gmail.com wrote:
here's a recursive solution
int depth(Node n){
if (n==NULL)
return 0;
else
return 1 + max(
height of current node = max(height of left child, height of right child) +1
Hope now you get that? :)
Anurag Sharma
On Tue, Jun 15, 2010 at 5:31 PM, ajay kumar ajaykr@gmail.com wrote:
Write a pseudo code 4 that..using c/c++...
how can we find the depth(height) of BST ?