Pre-, in- and post-order
After this: Move the visit and write out all three orders.
On this page · 10 sections · 3 checks
In the last lesson, the walk visited a node first and then walked its two subtrees. But the visit does not have to come first. It can sit between the two subtrees, or after them. That gives two more orders, and each one is useful for a different job.
The three names are often confused, so we take them one at a time on the same tiny tree.
One question: when do I visit?
At every node, a walk does three things: visit the node, walk left, walk right. Left always comes before right. The only thing that changes is when the node is visited.
To remember the three orders, imagine that you are the node, and call yourself "me".
| Name | At every node | How to remember it |
|---|---|---|
| Preorder | me, left, right | "Pre" means before: me before my children |
| left, me, right | "In" means in between: me between my children | |
| left, right, me | "Post" means after: me after my children |
Preorder: me first
- At node 1, visit it first. Visited: 1.
- Walk left. At node 2, visit it. It has no children. Visited: 1, 2.
- Walk right. At node 3, visit it. It has no children. Visited: 1, 2, 3.
The result is 1 2 3. The root comes first.
Inorder: me in the middle
- At node 1, do not visit yet. Walk left first.
- At node 2, the left side is empty, so visit it. The right side is empty too. Visited: 2.
- The left side of node 1 is finished, so now visit node 1. Visited: 2, 1.
- Walk right. At node 3, the left side is empty, so visit it. Visited: 2, 1, 3.
The result is 2 1 3. The root comes in the middle.
Postorder: me last
- At node 1, do not visit yet. Walk left first.
- At node 2, both sides are empty, so visit it. Visited: 2.
- Still not time for node 1. Walk right.
- At node 3, both sides are empty, so visit it. Visited: 2, 3.
- Both sides of node 1 are finished, so now visit it. Visited: 2, 3, 1.
The result is 2 3 1. The root comes last.
When the root has two children, here is a quick way to check your answer. Look at where the root is in the list. First means preorder, somewhere between means inorder, and last means postorder.
The same rule on the bigger tree
The rule applies at every node. So "walk left" means: use the same order on the whole left subtree. Here are the three orders on the example tree.
Preorder gives 1 2 4 5 3 6.
- Visit the root, 1.
- Do the left subtree in preorder: 2, 4, 5.
- Do the right subtree in preorder: 3, 6.
Inorder gives 4 2 5 1 3 6.
- Do the left subtree in inorder: 4, 2, 5.
- Visit the root, 1.
- Do the right subtree in inorder. Node 3 has no left child, so 3 comes straight away, and then 6.
Postorder gives 4 5 2 6 3 1.
- Do the left subtree in postorder: 4, 5, 2.
- Do the right subtree in postorder: 6, 3.
- Visit the root, 1.
The code
The three functions differ in one line: where the visit sits.
def preorder(node, out): if node is None: return out.append(node.value) # visit first preorder(node.left, out) preorder(node.right, out)
def inorder(node, out): if node is None: return inorder(node.left, out) out.append(node.value) # visit between the children inorder(node.right, out)
def postorder(node, out): if node is None: return postorder(node.left, out) postorder(node.right, out) out.append(node.value) # visit laststatic void preorder(Node node, List<Integer> out) { if (node == null) return; out.add(node.value); // visit first preorder(node.left, out); preorder(node.right, out);}
static void inorder(Node node, List<Integer> out) { if (node == null) return; inorder(node.left, out); out.add(node.value); // visit between the children inorder(node.right, out);}
static void postorder(Node node, List<Integer> out) { if (node == null) return; postorder(node.left, out); postorder(node.right, out); out.add(node.value); // visit last}Read the highlighted lines. In preorder, the visit is above both recursive calls. In inorder, it is between them. In postorder, it is below them. Everything else is the same.
Here are all three on the example tree.
for walk in (preorder, inorder, postorder): out = [] walk(root, out) print(walk.__name__, out)preorder [1, 2, 4, 5, 3, 6] inorder [4, 2, 5, 1, 3, 6] postorder [4, 5, 2, 6, 3, 1]
List<Integer> pre = new ArrayList<>(), in = new ArrayList<>(), post = new ArrayList<>();preorder(root, pre);inorder(root, in);postorder(root, post);System.out.println("preorder " + pre);System.out.println("inorder " + in);System.out.println("postorder " + post);preorder [1, 2, 4, 5, 3, 6] inorder [4, 2, 5, 1, 3, 6] postorder [4, 5, 2, 6, 3, 1]
In the Python version, the loop runs each function in turn, and walk.__name__ is the function's name. In the Java version, each list collects the output of one walk, and then the three lists are printed.
The recursive calls happen in the same order all three times, so the frames go on and off the stack in the same order too. Only the moment of the visit changes.
What each one is for
Each order suits a different job. The question to ask is what the visit needs.
- Preorder is for work that must happen at a node before its children. Examples are copying a tree from the top down, or listing a folder before what is inside it. It is also how you write a tree out so that it can be rebuilt, because the root has to be read first. You must also write a marker for each missing child.
- Postorder is for work that needs the children finished first. Examples are deleting a tree without losing the links to nodes you have not reached, and working out a sum such as (2 + 3) × 4, where the × needs both of its parts first. A later lesson uses it to calculate size and height.
- Inorder is for trees where left and right mean smaller and larger. In such a tree, inorder gives the values in sorted order. That tree is called a binary search tree, and it is the next tutorial.
Watch it happen
Run all three. Watch when the root's value appears in the Visited strip, and notice that the stack rises and falls in the same way each time. Before the postorder run, the task asks which node is visited third. Tap your answer in the picture, and then watch the walk to see if you were right.
- Preorder traversalPreorder: me, left, right. A node is visited as soon as the walk enters it, so the root is first in the strip.
- Inorder traversal
- Postorder traversal
Run an operation to begin.
Check yourself
Here is a different tree, drawn below. The root is 7, with children 3 and 9. Node 3 has a right child 5 and no left child. What is its postorder?
What is the inorder of the example tree?
You are deleting every node of a tree, one at a time. A node must not be deleted while it still has a child, because the link to that child would be lost. Which order do you use?
Connect
- Write out preorder (me, left, right), inorder (left, me, right) and postorder (left, right, me) for a small tree.
- Say what each order is for: preorder works from the top down, postorder finishes the children first, and inorder reads a search tree in sorted order.