Pre-order, in-order and post-order, drawn
Pre-order, in-order and post-order sound like three algorithms. They are one recursive walk, and the only difference is the line where you write the node down.
01 One walk, three moments
A recursive walk over a binary tree reaches every node three times: when the call starts, after the left subtree returns, and after the right subtree returns. The calls happen in the same order every time. What changes is which of those three moments you use to visit the node: print it, add it to a list, or work something out from it.
Put the visit at moment 1 and you get pre-order. At moment 2, in-order. At moment 3, post-order. The rest of the function does not change.
02 Three places to put the visit
Here are the three walks side by side. Read them line by line: only the position of out.append moves.
| Order | Visit happens | Pattern |
|---|---|---|
| Pre-order | before both subtrees | node, left, right |
| In-order | between the subtrees | left, node, right |
| Post-order | after both subtrees | left, right, node |
03 In-order, call by call
Take a five-node tree: A at the root, B and C below it, and D and E under B. Watch the call stack: a node is written only once its left call has come back.
- not called yet
- call paused
- call running
- just pushed or written
- written
The walk is about to start at the root A. No call has run, so the stack and the output are empty.
The stack never held more than three calls: one per level of the tree. The memory a recursive walk needs grows with the tree's height, not with how many nodes it has.
04 Every order on one tree
Run the same walk with the visit in each place, and every node gets three positions. The calls are identical; only the moment of writing differs.
A is 1st in pre-order, 4th in in-order and 5th (last) in post-order. A leaf such as D comes out early in all three, because nothing below it has to wait.
05 What each order is for
Pick the order by what the visit needs to know.
Maximum depth is a post-order walk. A node cannot know its depth until both children have reported theirs, so the work happens on the way out.
06 The same walk with your own stack
Recursion keeps the paused calls on the call stack for you. You can keep them yourself in a list. The loop below does exactly what the trace showed: dive left pushing nodes, pop one, write it, then turn right.
This version has no depth limit beyond memory, which matters on very deep trees. Pre-order is the easiest to write this way; post-order is the hardest, because a node has to wait for two subtrees, not one.
07 When it goes wrong
pre[::-1]Reversed pre-order is not post-orderOn our tree, reversed pre-order is C E D B A but post-order is D E B C A. What reverses into post-order is node, right, left: A C B E D reversed.RecursionErrorA deep tree hits the recursion limitCPython stops at a depth of about 1,000 calls by default (sys.getrecursionlimit()). A tree shaped like a long chain gets there. Use the explicit stack instead.node.left.valReading a child without checkingLeaves have None children. Check node is None at the top of the function, once, instead of before every child access.inorder(root)Expecting sorted output from any treeIn-order gives left-to-right position. It is sorted only when the tree is a binary search tree.