Trees and traversals
A rooted tree assigns each non-root node one parent. Binary trees distinguish left and right child positions, even when one is empty. Fix height and fullness conventions before counting nodes or reconstructing a tree.
4.1 Tree vocabulary and height bounds
Past papers · CS 2014–2026 (23 papers): 1 question, 1 mark
Depth is the number of edges from the root; the root has depth zero. Height is the greatest depth in the tree, or the greatest downward distance from a specified node. A leaf has no children; an internal node has at least one. A subtree contains a node and all its descendants, not just an arbitrarily selected connected portion.
A strict or full binary tree gives each internal node exactly two children. A perfect binary tree is full with all leaves at the same depth. A complete binary tree fills each level, except possibly the last, and fills the last from left to right. These are different conditions.
A full but non-perfect binary tree beside a complete but non-full binary tree.
4.2 Leaves, internal nodes and empty links
Past papers · CS 2014–2026 (23 papers): 4 questions, 4 marks · DA 2024–2026 (3 papers): 1 question, 2 marks
4.3 Traversal order and subtree aggregation
Past papers · CS 2014–2026 (23 papers): 7 questions, 10 marks · DA 2024–2026 (3 papers): 1 question, 2 marks
| Traversal | Order | Typical purpose |
|---|---|---|
| Preorder | Node, left, right | Record roots before descendants; prefix expression |
| Inorder | Left, node, right | Read binary-search-tree keys in sorted order |
| Postorder | Left, right, node | Compute a parent after its children; postfix expression |
| Level order | Increasing depth, usually left to right | Queue-based traversal of levels |
A binary-tree DFS traversal takes time and recursive stack space. Level order uses time and queue space, where is maximum number of nodes at a level; the queue may temporarily contain parts of two adjacent levels but remains within a constant factor of .
For an arbitrary rooted tree, leftmost-child/right-sibling representation uses one pointer to the first child and another to the next sibling. A null first-child pointer identifies a leaf in the original tree; a null sibling pointer does not. Summing over siblings must not treat those siblings as descendants of the first one. There is no standard binary inorder for an arbitrary multiway tree without a convention.
4.4 Reconstruction and tree-shape counts
Past papers · CS 2014–2026 (23 papers): 1 question, 1 mark · DA 2024–2026 (3 papers): 2 questions, 2 marks
With distinct node labels, preorder plus inorder uniquely determines a binary tree: take the first preorder label as root, split inorder at that label, and recurse on the two lengths. Postorder plus inorder works with the last postorder label as root. The sequences must be consistent. Preorder plus postorder does not generally distinguish a lone left child from a lone right child, but does uniquely determine a full binary tree with distinct labels.
An expression tree uses operators as internal nodes and operands as leaves. Build it from postfix using a stack of tree roots; the second popped tree is the left child. A reverse-child traversal must reverse child visitation at every recursive call, not just at the root.
Last-minute recall
- Height counts edges; a leaf has height zero.
- Full, perfect and complete describe different restrictions.
- Leaves equal two-child nodes plus one, even with unary nodes.
- A nonempty binary tree has empty child links.
- Postorder combines child results before their parent.
- Inorder plus preorder or postorder reconstructs distinct-labelled binary trees.
- Preorder plus postorder needs additional shape restrictions for uniqueness.
- Left-child/right-sibling pointers encode a multiway tree, not its binary meaning.