Comparing Traversal Methods

When working with binary tree data structures, we often want to quickly sketch in our minds what a particular approach's traversal sequence looks like. Having this sketch can be a powerful tool for gainign some traction on solving a problem. Accordingly, let's investigate some techniques for quickly determining a particular sequence.

First, it's helpful to associate the traversal approaches with a few visit sequences. For the visit sequences in the table below, L stands for "visit the left subtree," root stands for "visit the root," and R stands for "visit the right subtree."

Preorderroot L R
InorderL root R
PostorderL R root
GenerationalGeneration by generation, left to right

Knowing these sequences can make traversal sequences for more complicated trees clearer. For example, consider the following tree:

Trying to determine the traversal sequence for any given traversal approach can appear daunting. The trick, however, is divide and conquer: Break the tree down into subtrees. In this case, let's break it down into a left-subtree (the nodes in red) and a right-subtree (the nodes in blue):

Let's first consider preorder traversal. For preorder traversal, we have root L R. So, we write:

a,(     )L,(     )R a, (~~~~~)_{\texttt{L}}, (~~~~~)_{\texttt{R}}

Now we fill in the parentheses. First, the left subtree, following the same sequence, root L R:

a,(b,d,e)L,(     )R a, (b, d, e)_{\texttt{L}}, (~~~~~)_{\texttt{R}}

Then the right subtree, following the same sequence, root L R.

a,(b,d,e)L,(c,f,g)R a, (b, d, e)_{\texttt{L}}, (c, f, g)_{\texttt{R}}

Thus, the inorder traversal sequence for the tree is:

(a,b,d,e,c,f,g) (a, b, d, e, c, f, g)

Using the same method, let's try inorder traversal. For inorder traversal, the visit sequence is L root R. So we write:

(     )L,a,(     )R (~~~~~)_{\texttt{L}}, a, (~~~~~)_{\texttt{R}}

Now we fill in the subtree parentheses. Starting with the left subtree, we use the same sequence, L root R:

(d,b,e)L,a,(     )R (d, b, e)_{\texttt{L}}, a, (~~~~~)_{\texttt{R}}

Then we fill in the right subtree's parentheses, again using L root R:

(d,b,e)L,a,(f,c,g)R (d, b, e)_{\texttt{L}}, a, (f, c, g)_{\texttt{R}}

Hence, the inorder traversal sequence is:

(a,b,d,e,c,f,g) (a, b, d, e, c, f, g)

Now let's try postorder traversal. The visit sequence is L R root, so we have:

(     )L,(     )R,a (~~~~~)_{\texttt{L}}, (~~~~~)_{\texttt{R}}, a

Now we fill in the subtrees, again using L R root. For the left subtree:

(d,e,b)L,(     )R,a (d, e, b)_{\texttt{L}}, (~~~~~)_{\texttt{R}}, a

And for the right subtree:

(d,e,b)L,(f,g,c)R,a (d, e, b)_{\texttt{L}}, (f, g, c)_{\texttt{R}}, a

This yields the postorder traversal sequence:

(d,e,b,f,g,c,a) (d, e, b, f, g, c, a)

Finally, generational traversal is simple enough that we don't need to apply some visit sequence. We merely traverse the tree from the first generation to the last generation, visiting each node from left to right:

(a,b,c,d,e,f,g) (a, b, c, d, e, f, g)