AQA 7517, section 4.3.2.1
Tree traversal means visiting every node of a tree exactly once. For a binary tree there are three standard depth-first orders. They differ only in when the root of each subtree is visited compared with its left and right subtrees.
| Traversal | Order |
|---|---|
| Pre-order | Root, then left subtree, then right subtree |
| In-order | Left subtree, then root, then right subtree |
| Post-order | Left subtree, then right subtree, then root |
Each one is recursive: the rule is applied to every subtree in the same way.
The example tree
Pre-order: root, left, right
Visit A, then do the whole left subtree (B, D, E), then the right subtree (C, F).
Result: A, B, D, E, C, F
In-order: left, root, right
Do the whole left subtree first (D, B, E), then visit A, then the right subtree (C, F).
Result: D, B, E, A, C, F
Post-order: left, right, root
Do the left subtree (D, E, B), then the right subtree (F, C), and visit A last.
Result: D, E, B, F, C, A
A quick way by hand
To check a traversal quickly, draw a line round the outside of the tree, starting at the top of the root and going anticlockwise (down the left-hand side first). Then put a dot next to every node:
- on its left for pre-order;
- underneath it for in-order;
- on its right for post-order.
Follow the line and write down each node as you pass its dot. Here is the method on a smaller tree:
The algorithms in Python
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def pre_order(node):
if node is not None:
print(node.value) # root first
pre_order(node.left)
pre_order(node.right)
def in_order(node):
if node is not None:
in_order(node.left)
print(node.value) # root in the middle
in_order(node.right)
def post_order(node):
if node is not None:
post_order(node.left)
post_order(node.right)
print(node.value) # root last
The only difference between the three is the position of the print line. The base case is an empty subtree (None), where nothing is done.
Uses of each traversal
| Traversal | Typical use |
|---|---|
| Pre-order | Copying a tree: the root is created before its children |
| In-order | In a binary search tree, outputs the contents in ascending order |
| Post-order | Emptying (deleting) a tree, because children are removed before their parent. Also converting infix to Reverse Polish and producing a postfix expression from an expression tree |
In-order on a binary search tree
In a binary search tree, everything in the left subtree is smaller than the root and everything in the right is larger. So visiting left, root, right gives the values in order.
In-order: 1, 3, 6, 8, 10, 14, which is ascending.
Post-order on an expression tree
An expression tree stores a calculation, with operators at the branches and numbers at the leaves. Here is :
Post-order visits 3, 4, +, 2, *. This is 3 4 + 2 *, the expression in Reverse Polish (postfix) form (see 4.3.3.1).
Worked example
Task: list the pre-order, in-order and post-order traversals of this tree.
- Pre-order (root, left, right): 1, then the left subtree (2, 4), then the right subtree (3, 5, 6). Result: 1, 2, 4, 3, 5, 6.
- In-order (left, root, right): left subtree (4, 2), then 1, then right subtree (5, 3, 6). Result: 4, 2, 1, 5, 3, 6.
- Post-order (left, right, root): left subtree (4, 2), right subtree (5, 6, 3), then 1. Result: 4, 2, 5, 6, 3, 1.
A quick check: the pre-order list always starts with the root, and the post-order list always ends with it.
Exam tip: in a trace, handle one subtree at a time and write down the order as you go. For each node ask "what do I do first: record this node, or go left?", and then repeat for every subtree. Remember the root is first in pre-order, in the middle (relative to its subtrees) in in-order, and last in post-order.
Check yourself
What is the post-order traversal of the example tree A, B, C, D, E, F above?
D, E, B, F, C, A.
Which traversal outputs a binary search tree in ascending order?
In-order.
Give a use of pre-order traversal and a use of post-order traversal
Pre-order: copying a tree. Post-order: emptying a tree, or producing a postfix (Reverse Polish) expression from an expression tree.
What is the base case in the recursive traversal algorithms?
An empty subtree (the node is None), where nothing is done and the function returns.