I am really confused about finding an item in binary tree.
Question . When we say we are looking for an element in a binary tree, the maximum in this case, suppose the tree is sorted ???
If not, look at the code below, I got it from a book, and almost every online address offers a similar pattern
int FindMaxNmb(BinaryTreeNode root)
{
int root_val,left,right,max;
if(root!=null)
{
root_val = root.getData();
left = FindMaxNmb(root.getLeft());
right = FindMaxNmb(root.getRight());
if(left > right)
{
max = left;
}
else
{
max = right;
}
if(root_val > max)
{
max = root_val;
}
}
return max;
}
What I do not understand: take this recusrion for example
left = FindMaxNmb(root.getLeft());it will continue until it reaches the leftmost bottom sheet and then assigns a value, the same with getRight().... but this thing only works for the left node, which has 2 children ... how to do this checks the value of the remaining nodes (I assume that the binary tree is not sorted)
, - ... , !!