The specification of the method claims that the method, starting from the root, will remove the leftmost node and repair the tree structure. It also says that if the leftmost node has no left child to attach a right child (may be null
) to parent of leftmost node as left child (I don't see where this happens in the code). Here is the code:
public BTNode removeLeftmost()
{
if (left == null)
return right;
left = left.removeLeftmost();
return this;
}
I just don't see how it is returning back the whole tree with the leftmost node returned. Mainly the first part is confusing me where it says to return the right child if left == null
. If I were deep into the tree (due to the recursive call), wouldn't returning right cut off a lot of the tree?