TAOCP 2.3.1 Exercise 2
Let `T` denote the root of the binary tree in the figure.
Section 2.3.1: Traversing Binary Trees
Exercise 2. [11] List the nodes of the binary tree in (a) preorder; (b) symmetric order; (c) postorder.

Verified: no
Solve time: 38s
Solution
Let T denote the root of the binary tree in the figure. We denote the left and right links of a node P by LLINK(P) and RLINK(P), and the value of the node by INFO(P). Traversals are defined recursively as in Section 2.3.1.
(a) Preorder traversal. By definition, preorder visits the root first, then traverses the left subtree in preorder, and finally traverses the right subtree in preorder. Let us apply this recursively:
- Visit the root
A. - Traverse the left subtree rooted at
B:
- Visit
B. - Traverse left subtree of
B, rooted atD: visitD. - Right subtree of
Bis empty.
- Traverse the right subtree rooted at
C:
-
Visit
C. -
Traverse left subtree of
C, rooted atE: -
Visit
E. -
Left subtree of
Eis empty. -
Traverse right subtree of
E, rooted atG: visitG. -
Traverse right subtree of
C, rooted atF: -
Visit
F. -
Left subtree of
Fis empty. -
Traverse right subtree of
F, rooted atH: -
Visit
H. -
Left subtree of
His empty. -
Traverse right subtree of
H, rooted atJ: visitJ.
Collecting nodes in the order visited gives
$$ A \quad B \quad D \quad C \quad E \quad G \quad F \quad H \quad J. $$
(b) Symmetric (inorder) traversal. By definition, inorder visits the left subtree, then the root, then the right subtree. We proceed recursively:
- Traverse left subtree of
Arooted atB:
- Traverse left subtree of
Brooted atD: visitD. - Visit
B. - Right subtree of
Bis empty.
- Visit
A. - Traverse right subtree of
Arooted atC:
-
Traverse left subtree of
Crooted atE: -
Left subtree of
Eis empty. -
Visit
E. -
Traverse right subtree of
Erooted atG: visitG. -
Visit
C. -
Traverse right subtree of
Crooted atF: -
Left subtree of
Fis empty. -
Visit
F. -
Traverse right subtree of
Frooted atH: -
Left subtree of
His empty. -
Visit
H. -
Traverse right subtree of
Hrooted atJ: visitJ.
Collecting nodes in order yields
$$ D \quad B \quad A \quad E \quad G \quad C \quad F \quad H \quad J. $$
(c) Postorder traversal. By definition, postorder traverses the left subtree, then the right subtree, then visits the root. We apply this recursively:
- Traverse left subtree of
Arooted atB:
- Traverse left subtree of
Brooted atD: visitD. - Right subtree of
Bis empty. - Visit
B.
- Traverse right subtree of
Arooted atC:
-
Traverse left subtree of
Crooted atE: -
Left subtree of
Eis empty. -
Traverse right subtree of
Erooted atG: visitG. -
Visit
E. -
Traverse right subtree of
Crooted atF: -
Left subtree of
Fis empty. -
Traverse right subtree of
Frooted atH: -
Left subtree of
His empty. -
Traverse right subtree of
Hrooted atJ: visitJ. -
Visit
H. -
Visit
F. -
Visit
C.
- Visit
A.
Collecting nodes in order gives
$$ D \quad B \quad G \quad E \quad J \quad H \quad F \quad C \quad A. $$
All sequences satisfy the recursive definitions of preorder, inorder, and postorder.
$$ \boxed{ \text{Preorder: } A B D C E G F H J, \quad \text{Inorder: } D B A E G C F H J, \quad \text{Postorder: } D B G E J H F C A } $$