The name says when the root is visited
Pre: before the subtrees. In: between them. Post: after both. Everything else is the same recursion.
A rooted tree is visited in one of three canonical orders, distinguished by when the root is handled. Each is a structural induction, and each is the right one for a different job.
Core ideas
Three orders, differing only in when the root is visited, and each is right for a different task.
Pre: before the subtrees. In: between them. Post: after both. Everything else is the same recursion.
The recursive definition of a binary tree from “Recursive Definitions” in Induction and Recursion gives the recursion directly.
That is the defining property of a binary search tree, and it is why inorder matters.
The material
The three orders, their uses, and the expression-tree example.
Each is the recursive definition of a binary tree with the visit inserted at a different point, so each is a structural induction in the sense of “Structural Induction” in Induction and Recursion. Every node is visited exactly once and the cost is Θ(n).
An arithmetic expression is a binary tree: operators are internal nodes and operands are leaves. The three traversals produce the three standard notations.
Postfix is what stack-based evaluators consume: read left to right, push operands, and on an operator pop two and push the result. No parentheses and no precedence rules are needed, because the tree structure is already encoded in the order.
Infix is the notation people read and the only one of the three that is ambiguous without parentheses or precedence conventions — which is the whole reason “Logical Connectives” in Logic and Proof had to state a precedence order.
A binary tree in which every node's key exceeds all keys in its left subtree and is less than all keys in its right subtree.
An inorder traversal of a binary search tree visits the keys in increasing order.
By structural induction. The empty tree is vacuous.
For a node with subtrees L and R: by the hypothesis the traversal visits L's keys in increasing order, all less than the root; then the root; then R's keys in increasing order, all greater. The concatenation is increasing.
□
Searching a balanced binary search tree costs Θ(log n), one comparison per level, which is the binary search of “Searching and Sorting”, in Algorithms and Complexity, reorganised as a data structure. If the tree is unbalanced — a path — it degenerates to Θ(n), which is why balancing schemes exist.
Level order visits by depth and is exactly breadth-first search from the root, using a queue rather than recursion. It is the traversal that prints a tree row by row.
For non-binary rooted trees, preorder and postorder generalise directly — visit the children in order — while inorder does not, since there is no single place for the root among three or more children.
Interactive
Your graph, the course's algorithms
Method
Pick the order from the task.
The root is emitted before its subtrees, so the structure can be rebuilt as it is read.
It produces the keys in sorted order, which is what the structure exists for.
Children are finished before the parent, which is what evaluation and safe deallocation both require.
Breadth-first from the root. It is the traversal that shows depth and balance.
A binary search tree containing 2, 3, 5, 6, 7, 8.
Preorder alone determines the tree when it is a binary search tree, because the ordering constraint says where each subsequent key belongs. For a general binary tree it does not: preorder plus inorder is needed to reconstruct the shape.
Build a binary search tree by inserting 5, 3, 8, 1, 4, 7, 9 in that order, then write all three traversals. The inorder must come out sorted, which is a check on the insertion: preorder 5 3 1 4 8 7 9, inorder 1 3 4 5 7 8 9, postorder 1 4 3 7 9 8 5.
Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.
No recommendations yet for this lesson.