Tree Traversal
Tree traversal is a way to visit every vertex of a tree exactly once in a fixed order. For a binary tree, preorder visits the root before its two subtrees, inorder visits the left subtree, then the root, then the right subtree, and postorder visits both subtrees before the root.
Before this lesson: Trees in Graph Theory and Minimum Spanning Trees
What is tree traversal?
A list of a tree’s vertices is only useful if everyone builds it the same way. A traversal is a rule that fixes the order, so that every vertex appears exactly once. Traversals are how computers search, copy, print and evaluate data stored in trees.
This lesson uses binary trees: rooted trees in which every vertex has at most two children, called the left child and the right child. The left child and everything below it form the left subtree; the right subtree is defined the same way. Order matters: a single child on the left is different from a single child on the right.
All three traversals start at the root and go down the left subtree before the right subtree. They differ only in when the root of each subtree is written down: before (pre), between (in) or after (post) its two subtrees.
Preorder, inorder and postorder rules
Each rule is recursive: to traverse a tree, you traverse its subtrees with the same rule, much as a recurrence relation defines each term from earlier ones. An empty subtree contributes nothing.
| Traversal | Order | Memory aid |
|---|---|---|
| Preorder | root, left subtree, right subtree | Root comes first. |
| Inorder | left subtree, root, right subtree | Root comes in the middle. |
| Postorder | left subtree, right subtree, root | Root comes last. |
| Level order | level 0, then level 1, then level 2, …, each level left to right | Read the tree like a page. |
Shortcut by hand: trace a path around the outside of the tree, starting at the root and going down its left side, hugging every vertex and edge. Write each vertex down the first time you pass it for preorder, when you pass underneath it for inorder, and the last time you pass it for postorder.
Preorder and postorder also work for ordered rooted trees in which a vertex can have more than two children: visit the children from left to right. Inorder needs a clear left and right, so it is used only for binary trees.
Tree traversal examples
Example 1: Preorder traversal
Find the preorder traversal of the tree in the first figure.
M
Visit the root M.
M, D
Go to the left subtree of M, whose root is D. Visit D.
M, D, B, A
Left subtree of D: visit B, then B’s left child A. A has no children.
M, D, B, A, H
B has no right child, so go back to D’s right subtree. Visit H.
M, D, B, A, H, F, K
Visit H’s left child F, then its right child K.
M, D, B, A, H, F, K, R
The left subtree of M is done. Visit the right child R.
M, D, B, A, H, F, K, R, T, S
R has no left child. Visit its right child T, then T’s left child S.
The preorder traversal is M, D, B, A, H, F, K, R, T, S. Check: all 10 vertices appear exactly once.
Example 2: Inorder and postorder traversals
Find the inorder and postorder traversals of the same tree.
\text{in}(D\text{'s subtree}),\; M,\; \text{in}(R\text{'s subtree})Inorder: the left subtree, then the root M, then the right subtree.
A, B, D, F, H, K
Inorder of D’s subtree: A, B (left of D), then D, then F, H, K.
R, S, T
Inorder of R’s subtree: R has no left subtree, so R comes first, then S, T.
A, B, D, F, H, K, M, R, S, T
Join the pieces with M in the middle.
\text{post}(D\text{'s subtree}),\; \text{post}(R\text{'s subtree}),\; MPostorder: both subtrees first, then the root.
A, B, F, K, H, D
Postorder of D’s subtree: A, B, then F, K, H, then D.
S, T, R
Postorder of R’s subtree: S, then T, then R.
A, B, F, K, H, D, S, T, R, M
Join the pieces and finish with M.
The inorder traversal is A, B, D, F, H, K, M, R, S, T, and the postorder traversal is A, B, F, K, H, D, S, T, R, M. The inorder list is alphabetical because this tree is a binary search tree: every vertex is later in the alphabet than everything in its left subtree and earlier than everything in its right subtree.
Example 3: An expression tree
The second figure shows the expression ((3 + x) \times (y - 2)) \div 4 as a tree. Each internal vertex is an operation, and each leaf is a number or a variable. Write the expression in prefix and postfix form, and evaluate it for x = 5 and y = 6.
\div\; \times\; +\; 3\; x\; -\; y\; 2\; 4
Preorder gives prefix (Polish) notation: each operator comes before its two operands.
3\; x\; +\; y\; 2\; -\; \times\; 4\; \div
Postorder gives postfix (reverse Polish) notation: each operator comes after its operands.
3 + x = 3 + 5 = 8
Evaluate the left branch of the multiplication with x = 5.
y - 2 = 6 - 2 = 4
Evaluate the right branch with y = 6.
8 \times 4 = 32
Multiply.
32 \div 4 = 8
Divide by the right child of the root.
Prefix: ÷ × + 3 x − y 2 4. Postfix: 3 x + y 2 − × 4 ÷. The value is 8. Neither prefix nor postfix needs parentheses, because the position of each operator already fixes the order of operations.
Example 4: Rebuilding a tree from two traversals
A binary tree has preorder P, Q, S, T, R, U and inorder S, Q, T, P, U, R. Draw the tree by describing each vertex’s children, and give its postorder traversal.
\text{root} = PThe first vertex in preorder is the root.
S, Q, T \;\mid\; P \;\mid\; U, R
Split the inorder list at P: left subtree {S, Q, T}, right subtree {U, R}.
Q, S, T
The left subtree’s preorder is the next 3 vertices: Q, S, T. So Q is the left child of P.
S \;\mid\; Q \;\mid\; T
Split the inorder list S, Q, T at Q: S is Q’s left child and T is Q’s right child.
R, U
The right subtree’s preorder is R, U. So R is the right child of P.
U \;\mid\; R
In inorder, U comes before R, so U is R’s left child.
S, T, Q, U, R, P
Postorder: S, T, Q, then U, R, then the root P.
P has children Q (left) and R (right); Q has children S and T; R has only a left child, U. The postorder traversal is S, T, Q, U, R, P. Preorder together with inorder always fixes a binary tree with distinct labels; preorder with postorder alone may not.
Example 5: Folder sizes (word problem)
A computer’s Home folder holds two folders, Docs and Photos. Docs holds a.txt (3 MB) and b.pdf (5 MB). Photos holds a folder Trip and the file e.png (2 MB), and Trip holds c.jpg (4 MB) and d.jpg (6 MB). In what order should a program visit the items so it can find every folder’s size, and what are the sizes?
\text{postorder}A folder’s size is the sum of its contents, so each folder must come after everything inside it.
a = 3,\; b = 5
Visit Docs’ files first.
\text{Docs} = 3 + 5 = 8Then Docs.
c = 4,\; d = 6
In Photos, visit Trip’s files.
\text{Trip} = 4 + 6 = 10Then Trip.
e = 2
Then Photos’ other item.
\text{Photos} = 10 + 2 = 12Then Photos.
\text{Home} = 8 + 12 = 20Finally the root, Home.
Postorder (a.txt, b.pdf, Docs, c.jpg, d.jpg, Trip, e.png, Photos, Home) gives Docs 8 MB, Trip 10 MB, Photos 12 MB and Home 20 MB. Printing the folder list as an indented outline would use preorder instead, because each folder name comes before its contents.
Common mistakes
| Mistake | Example of the error | Fix |
|---|---|---|
| Visiting the right subtree before the left | Preorder of the first figure written as M, R, T, S, D, … | All three traversals finish the whole left subtree before starting the right one. |
| Ignoring which side a single child is on | R’s only child T is treated as a left child, so inorder of R’s subtree is written S, T, R. | T is R’s right child, so in inorder R comes before T’s subtree: R, S, T. A left child comes before its parent, a right child after it. |
| Writing a subtree’s root at the wrong time | Inorder of D’s subtree written as D, A, B, F, H, K. | Apply the rule inside every subtree, not just at the top: in inorder, D goes between its left and right subtrees. |
| Confusing level order with preorder | Level order of the first figure written as M, D, B, A, …. | Level order lists by depth: M, D, R, B, H, T, A, F, K, S. |
| Popping operands in the wrong order in postfix | In 8 2 −, the answer is given as 2 − 8 = −6. | The first number popped is the right operand: 8 − 2 = 6. |
| Using inorder alone to rebuild a tree | Two different trees both have inorder A, B. | Use inorder together with preorder or postorder. |
Related ideas
Depth-first and breadth-first search. Preorder is a depth-first search: it goes as deep as possible before backing up. Level order is a breadth-first search. The same two ideas search general graphs, where they also build spanning trees like the ones in minimum spanning trees.
Evaluating postfix with a stack. Read a postfix expression from left to right. Push each number onto a stack; for each operator, pop two numbers, apply the operator (first popped on the right) and push the result. The one number left is the value. Calculators and compilers use this method.
Binary search trees. Because inorder lists a binary search tree in sorted order, these trees can store data so it can be searched quickly and printed in order. Building one and reading it in inorder is a sorting method called tree sort.
Practice problems
Problems 1–4 use the binary tree with root 1, where 1 has left child 2 and right child 3, vertex 2 has left child 4 and right child 5, and vertex 3 has only a right child, 6.
- Find the preorder traversal.
- Find the inorder traversal.
- Find the postorder traversal.
- Find the level-order traversal.
- Draw the expression tree for (a - b) \times c + d (multiplication first, then addition) and write it in prefix and postfix form.
- Evaluate the postfix expression 6 3 ÷ 4 2 − ×.
- Evaluate the prefix expression − × 3 4 + 1 5.
- A binary tree has postorder D, E, B, F, C, A and inorder D, B, E, A, F, C. Describe the tree and give its preorder traversal.
- The numbers 50, 30, 70, 20, 40, 60, 80 are inserted in this order into a binary search tree (smaller numbers go left, larger go right). Give the preorder and postorder traversals.
- A company chart has a CEO with two vice presidents, Ana and Ben. Ana manages Cal and Dee, and Ben manages Eli. List everyone in preorder (outline order) and in postorder.
Show answers
1.
1
Visit the root.
1, 2, 4, 5
Traverse the left subtree in preorder: 2, then 4, then 5.
1, 2, 4, 5, 3, 6
Traverse the right subtree: 3, then its right child 6.
Preorder: 1, 2, 4, 5, 3, 6.
2.
4, 2, 5
Inorder of the left subtree: 4, then 2, then 5.
4, 2, 5, 1
Then the root.
4, 2, 5, 1, 3, 6
Inorder of the right subtree: 3 has no left child, so 3, then 6.
Inorder: 4, 2, 5, 1, 3, 6.
3.
4, 5, 2
Postorder of the left subtree.
4, 5, 2, 6, 3
Postorder of the right subtree: 6, then 3.
4, 5, 2, 6, 3, 1
The root comes last.
Postorder: 4, 5, 2, 6, 3, 1.
4.
1 \;\mid\; 2, 3 \;\mid\; 4, 5, 6
List level 0, then level 1, then level 2, left to right.
Level order: 1, 2, 3, 4, 5, 6.
5.
\text{root} = +,\; \text{with children}\; \times \;\text{and}\; dThe last operation done is the addition, so it is the root.
\times \;\text{with children}\; - \;\text{and}\; cThe left child multiplies (a − b) by c.
-\; \text{with children}\; a \;\text{and}\; bThe subtraction is done first, so it is lowest.
\text{prefix: }\; +\; \times\; -\; a\; b\; c\; dPreorder gives the prefix form.
\text{postfix: }\; a\; b\; -\; c\; \times\; d\; +Postorder gives the postfix form.
Prefix: + × − a b c d. Postfix: a b − c × d +.
6.
[6,\, 3]
Push 6 and 3.
6 \div 3 = 2
÷: pop 3 and 6, push 2.
[2,\, 4,\, 2]
Push 4 and 2.
4 - 2 = 2
−: pop 2 and 4, push 2. The stack is [2, 2].
2 \times 2 = 4
×: pop both, push 4.
The value is 4.
7.
[5,\, 1]
Read prefix from right to left: push 5, then 1.
1 + 5 = 6
+: pop 1 (left) and 5 (right), push 6.
[6,\, 4,\, 3]
Push 4, then 3.
3 \times 4 = 12
×: pop 3 (left) and 4 (right), push 12.
12 - 6 = 6
−: pop 12 (left) and 6 (right), push 6.
The value is 6, from (3 × 4) − (1 + 5).
8.
\text{root} = AThe last vertex in postorder is the root.
D, B, E \;\mid\; A \;\mid\; F, C
Split the inorder list at A.
D, E, B
The left subtree’s postorder is the first 3 vertices; its root is B.
D \;\mid\; B \;\mid\; E
D is B’s left child and E is B’s right child.
F, C
The right subtree’s postorder is F, C; its root is C.
F \;\mid\; C
In inorder F comes before C, so F is C’s left child.
A, B, D, E, C, F
Preorder: root, then left subtree, then right subtree.
A has children B and C; B has children D and E; C has only a left child, F. Preorder: A, B, D, E, C, F.
9.
50
50 is the root.
30 \lt 50,\; 70 \gt 50
30 goes left of 50 and 70 goes right.
20 \lt 30,\; 40 \gt 30
20 and 40 become the children of 30.
60 \lt 70,\; 80 \gt 70
60 and 80 become the children of 70.
50, 30, 20, 40, 70, 60, 80
Preorder.
20, 40, 30, 60, 80, 70, 50
Postorder.
Preorder: 50, 30, 20, 40, 70, 60, 80. Postorder: 20, 40, 30, 60, 80, 70, 50.
10.
\text{CEO, Ana, Cal, Dee, Ben, Eli}Preorder: each manager before the people they manage.
\text{Cal, Dee, Ana, Eli, Ben, CEO}Postorder: each manager after the people they manage.
Preorder: CEO, Ana, Cal, Dee, Ben, Eli. Postorder: Cal, Dee, Ana, Eli, Ben, CEO.
Tree traversal FAQ
What is the difference between preorder, inorder and postorder?
They differ only in when the root of each subtree is visited. Preorder visits it before its subtrees, inorder between the left and right subtrees, and postorder after both.
Which traversal gives sorted order?
Inorder traversal of a binary search tree lists its values in increasing order.
Is preorder traversal the same as depth-first search?
Preorder is a depth-first search of a tree that records each vertex when it is first reached. Postorder is also depth-first, but it records each vertex when the search leaves it.
Can you rebuild a binary tree from its traversals?
Yes, if you know the inorder traversal and either the preorder or the postorder traversal, and the labels are all different. Preorder and postorder together are not enough in general.
Summary
- A traversal visits every vertex of a tree exactly once in a fixed order.
- Preorder: root, left, right. Inorder: left, root, right. Postorder: left, right, root.
- Level order visits the tree one level at a time, left to right.
- For an expression tree, preorder gives prefix notation, inorder gives the usual infix form and postorder gives postfix notation.
- Inorder of a binary search tree is sorted, and inorder plus preorder (or postorder) fixes the tree.
Previous lesson: Minimum Spanning Trees
Next lesson: Boolean Algebra
Back to the Discrete Math Index