Tree traversals
List the nodes of a binary tree in preorder, inorder, postorder and level order, and give the time and extra space of each.
It opens on
List the nodes of this tree so that every node comes before everything below it, and the left side before the right side. Which node is fourth in the list?
Why it matters
Every program that prints, copies, deletes, searches or evaluates a tree is a traversal, and which order it uses decides whether a parent is handled before its children, between them or after them.
Good to know first
- Trees and binary trees: a node has data, left and right, a subtree is a smaller tree, and size recurses on both subtrees in Θ(n) time
- Recursion and the call stack: a call waits on the stack until the calls it makes return
- Queues: enqueue at the back and dequeue at the front in O(1), first in, first out
- Stack applications, postfix expressions: a postfix expression is evaluated with a stack, pushing numbers and applying each operator to the top two items
Step by step
Preorder
Trace preorder on a binary tree by its recursive calls, and give its list, its number of calls and its stack depth.
The call on + adds +, then calls on its left child ×, which adds ×, then on −, which adds −, then on 6, which adds 6 and makes two calls on None that return at once. Back in the call on −, the right child 2 is added, so the list is + × − 6 2.
A common mix-up
Preorder reads the tree top to bottom, one level at a time.
In fact: It goes all the way down the left side first, so the 6 comes before the 3 and before the ÷.
Inorder and postorder
Trace inorder and postorder on the same tree, say which of them gives the infix and the postfix of the expression, and evaluate the postfix with a stack.
Inorder on the node − with children 6 and 2 gives 6, then −, then 2. Inorder on × gives that list, then ×, then 3, which is 6 − 2 × 3.
A common mix-up
Inorder gives back the original expression.
In fact: It gives the operands and operators in the same left to right order, but with no parentheses, so the meaning can change.
Level order with a queue
Trace level order with a queue on the same tree, and compare its time and space with the recursive orders.
The queue starts as the root +. Taking + adds + to the list and puts × and ÷ at the back. Taking × adds × and puts its children − and 3 at the back, so the queue is ÷, −, 3.
A common mix-up
Level order is just preorder with a different name.
In fact: Preorder follows one path down before it goes across, and level order visits all of depth 1 before any of depth 2, which needs a queue.
Try it
What is the preorder of the tree with root 6, whose left child 3 has the children 2 and 5, and whose right child is 8?Answer
6, 3, 2, 5, 8
Hint: Add 6, then do all of the subtree under 3, then 8
Give the three orders of the tree for 5 − 3 × 2, where × has the children 3 and 2 and − has 5 and ×.Answer
Preorder − 5 × 3 2, inorder 5 − 3 × 2, postorder 5 3 2 × −
Hint: × is the right child of −
What is the level order of the tree with root 3, whose left child 1 has the right child 2, and whose right child is 6?Answer
3, 1, 6, 2
Hint: Depth 0 is 3, depth 1 is 1 and 6, depth 2 is the 2
The call on − has returned to the call on ×. Which node is added next, and what is the list then?Answer
The 3, the right child of ×, so the list is + × − 6 2 3.
What list does inorder give for the whole tree?Answer
6 − 2 × 3 + 8 ÷ 4.
