Hi all,
I am having trouble understanding BST recursive algorithm. Here is the code:
int maxDepth(struct node* node) {
if (node==NULL) {
return(0);
}
else {
// compute the depth of each subtree
int lDepth = maxDepth(node->left);
int rDepth = maxDepth(node->right);
// use the larger one
if (lDepth > rDepth) return(lDepth+1);
else return(rDepth+1);
}
}
and this is the structure of my tree [int data, leftpointer, rightpointer]
Tree *a,*b, *leftsub, *rightsub, *d,*e, *f, *mytree;
e=new Tree(14,NULL,NULL);
d= new Tree(15,NULL, NULL);
leftsub= new Tree(18,e,d);
b= new Tree(22,NULL,NULL);
f= new Tree(80,NULL, NULL);
rightsub = new Tree(30, b,f);
mytree(20, leftsub, rightsub);
maxDepth(mytree);
here is what i understand:
1. mytree is called
2. because mytree is not null then int lDepth = maxDepth(node->left); is called, and at this moment ldepth= 0 and maxDepth(a)
3. the function goes back to the top, and again node a is not null therefore ldepth = 0 and maxdepth(e);
then i am not too sure whats happening after this.
It will be really great if you can help me explains how does this function works
Thanks heaps =D