Reputation: 650
I came across a traversing binary search tree function but I can't wrap my head around it. Here's the code:
public void inOrderTraverseTree(Node focusNode){
if(focusNode != null){
inOrderTraverseTree(focusNode.leftChild);
System.out.println(focusNode);
inOrderTraverseTree(focusNode.rightChild);
}
}
Assuming a "two-levels" balanced binary search tree, this is how I understand this recursive method:
root != null
--> inOrderTraverseTree(root.leftChild)
method runsroot.leftChild != null
--> inOrderTraverseTree(root.leftChild.leftChild)
method is runsroot.leftChild
has no leftChild --> focusNode.leftChild == null
, the if
loop will not runBut it supposedly works (https://www.youtube.com/watch?v=M6lYob8STMI). Can anyone point out where I've gone wrong?
Upvotes: 0
Views: 88
Reputation: 2436
However, since root.leftChild has no
leftChild
-->focusNode.leftChild
== null
, the if loop will not run
No, it will. since it is recursive it will continue doing for previous calls, check for righchild node in this case.
watch the call order of the function:
it continues ...
Upvotes: 0
Reputation: 2592
You need to understand the basic recursion and binary search tree structure here. Lets take below tree to understand this algorithm:
1. Focus Node is 9 , since it is not null, thus , inOrderTraverseTree
is invoked with focus node as 7(with left node to 9)
2. Focus Node is 7 , since it is not null, thus , inOrderTraverseTree is invoked with focus node as null(with left
node to 7)
3. Focus Node is null , thus , if condition is not satisfied and execution of function call at step 2 continues.
4. Control goes to method call with node as 7 (since left node was blank) and 7 is printed at System.out.println statement.
5. Now inOrderTraverseTree for focus node null is invoked (with right node to 7)
6. Focus Node is null , thus , if condition is not satisfied and execution of function call at step 2 continues.
7. inOrderTraverseTree for focus node 2 exits and control goes to method call with node as 9.
8. Control goes to method call with node as 9 and 9 is printed at System.out.println statement.
9. inOrderTraverseTree for focus node 11 is invoked (with right node to 9)
10.inOrderTraverseTree for focus node null is invoked (with left node to 11), thus, if condition is not satisfied and control is sent
back to step 9
11. Control goes to method call with node as 11 and 11 is printed at System.out.println statement.
12.inOrderTraverseTree for focus node null is invoked (with right node to 11), thus, if condition is not satisfied and control is sent
back to step 9.
And the execution ends
Upvotes: 0
Reputation: 3764
The logic applied here:
Check the focused node. If null
then return
to parent's inOrderTraverseTree
. But since nothing happens after the check for null, an additional return
statement is same as no statement at all.
If not null
, repeat from (1) for the left child node.
Print the value of the focused node.
Repeat from (1) for the right child node.
Return from current node's inOrderTraverseTree
to parent node's inOrderTraverseTree
.
Of course, if the focused node is the root
node, inOrderTraverseTree
simply returns to main
, or the method that first invoked it
As an example, consider the tree below:
A
/ \
B C
/ \ / \
D E F G
Trace:
A->inOrderTraverseTree(left)
B->inOrderTraverseTree(left)
D->inOrderTraverseTree(left) //null, return to D
print(D)
D->inOrderTraverseTree(right) //null, return to D
return to B
print(B)
B->inOrderTraverseTree(right)
E->inOrderTraverseTree(left) //null, return to E
print(E)
E->inOrderTraverseTree(right) //null, return to E
return to B
return to A
print(A)
A->inOrderTraverseTree(right)
//Continue for the right subtree.
Upvotes: 3