Traversing a Three-Node Tree
Traversing a Three-Node Tree
Let’s look at a simple example to get an idea of how this recursive traversal routine works. Imagine traversing a tree with only three nodes: a root (A), with a left child
Traversing the Tree 383
inOrder (A)
1. Call inOrder (B) 2. Visit A 3. Call inOrder (C)
inOrder (B)
inOrder (C)
1. Call inOrder (null)
1. Call inOrder (null)
2. Visit B
2. Visit C
3. Call inOrder (null)
3. Call inOrder (null)
inOrder (null)
inOrder (null) Returns
inOrder (null)
inOrder (null)
FIGURE 8.9 The inOrder() method applied to a three-node tree. We start by calling inOrder() with the root A as an argument. This incarnation of
inOrder() we’ll call inOrder(A) . inOrder(A) first calls inOrder() with its left child, B, as an argument. This second incarnation of inOrder() we’ll call inOrder(B) .
inOrder(B) now calls itself with its left child as an argument. However, it has no left child, so this argument is null . This creates an invocation of inorder() we could call inOrder(null) . There are now three instances of inOrder() in existence: inOrder(A) , inOrder(B) , and inOrder(null) . However, inOrder(null) returns immediately when it finds its argument is null . (We all have days like that.)
Now inOrder(B) goes on to visit B; we’ll assume this means to display it. Then inOrder(B) calls inOrder() again, with its right child as an argument. Again this argu- ment is null , so the second inorder(null) returns immediately. Now inOrder(B) has carried out steps 1, 2, and 3, so it returns (and thereby ceases to exist).
384 CHAPTER 8 Binary Trees
Now we’re back to inOrder(A) , just returning from traversing A’s left child. We visit A and then call inOrder() again with C as an argument, creating inOrder(C) . Like inOrder(B) , inOrder(C) has no children, so step 1 returns with no action, step 2 visits
C, and step 3 returns with no action. inOrder(B) now returns to inOrder(A) . However, inOrder(A) is now done, so it returns and the entire traversal is complete.
The order in which the nodes were visited is A, B, C; they have been visited inorder. In a binary search tree this would be the order of ascending keys.
More complex trees are handled similarly. The inOrder() function calls itself for each node, until it has worked its way through the entire tree.
Traversing with the Workshop Applet
To see what a traversal looks like with the Workshop applet, repeatedly press the Trav button. (You don’t need to type in any numbers.)
Here’s what happens when you use the Tree Workshop applet to traverse inorder the tree shown in Figure 8.10. This is slightly more complex than the three-node tree seen previously. The red arrow starts at the root. Table 8.1 shows the sequence of node keys and the corresponding messages. The key sequence is displayed at the bottom of the Workshop applet screen.
FIGURE 8.10 Traversing a tree inorder.
Traversing the Tree 385
TABLE 8.1 Workshop Applet Traversal
Red List of Step
Arrow Nodes Number
on Node
Will check left child 2 30 Will check left child 3 20 Will check left child 4 20 Will visit this node 5 20 Will check right child
6 20 Will go to root of previous subtree
7 30 Will visit this node
8 30 Will check right child
9 40 Will check left child
10 40 Will visit this node
11 40 Will check right child
12 40 Will go to root of previous subtree
13 50 Will visit this node
14 50 Will check right child
15 60 Will check left child
16 60 Will visit this node
17 60 Will check right child
18 60 Will go to root of previous subtree
19 50 Done traversal
It may not be obvious, but for each node, the routine traverses the node’s left subtree, visits the node, and traverses the right subtree. For example, for node 30 this happens in steps 2, 7, and 8.
The traversal algorithm isn’t as complicated as it looks. The best way to get a feel for what’s happening is to traverse a variety of different trees with the Workshop applet.