Hey guys, i was wondering if anyone could explain this to me
Code:
int maxdepth (treeptr tree){
int leftdepth = 0;
int rightdepth = 0;
if (tree == NULL)
return 0;
else{
leftdepth = maxdepth(tree->left);
rightdepth = maxdepth(tree->right);
if (leftdepth > rightdepth)
return leftdepth +1;
else
return rightdepth + 1;
}
}
This is supposed to calculate the maximun 'depth' of the binary tree. However, I don't see the part in which left/right depth is ever incremented. Infact, is it not just reset to 0 all the time?
Also, in the code for traversing a tree
Code:
void traverse(treeptr tree){
traverse(tree->left);
cout << tree->data;
traverse(tree->right);
}
How does the function ever break its recursion loop? Surely it would just keep going on and on.
many thanks,