COMP5112 Chap.5 Tree Structure and Traversal
Tree Structure and Traversal
Recursive structure controls the visit order
A rooted tree is built from nodes and subtrees, with depth measured from the root and height measured down to a leaf. The chapter defines preorder, inorder and postorder by the position of the current-node action around recursive subtree visits.
It then uses paired traversal sequences to reconstruct trees and shows why inorder gives sorted keys only when a separate binary-search-tree property holds.
Tree vocabulary describes relationships rather than drawing positions. The root has no parent, a leaf has no children, and siblings share a parent. A node together with all descendants forms a subtree.
Depth counts edges from the root under the course convention, while height counts the longest downward edge path to a leaf. State whether an empty tree or single node has height minus one or zero when a problem uses another convention.
Preorder processes the current node before its subtrees, inorder places the current-node action between left and right, and postorder waits until both subtrees finish.
These definitions are recursive. A visual row does not replace the rule: complete the required order inside one subtree before moving to its sibling. Annotating pending recursive calls reveals why the output appears in sequence and connects traversal to an implicit call stack.
For a binary tree with n nodes, a complete traversal visits each node once and performs constant local work, giving linear time.
Additional recursive space follows tree height rather than node count. A balanced shape uses logarithmic stack depth, while a chain can use linear depth. An iterative implementation moves pending work into an explicit stack or queue, but the same nodes still need visits and the same structural order must be preserved.
Two traversal sequences can identify a tree when keys are distinct and one sequence is inorder.
Preorder supplies the root first; postorder supplies it last. Locate that root in inorder to divide left and right subtree keys, take matching-sized portions from the other sequence, and repeat. Regenerate both supplied traversals from the result to verify every split.
Missing keys, inconsistent partitions, or duplicates without a policy make reconstruction ambiguous or impossible.
Traversal order alone does not create key order. Inorder produces sorted output only when the tree also satisfies the binary-search-tree property. For an arbitrary binary tree, inorder is simply left subtree, node, right subtree. Separate shape facts from key-order facts in explanations.
This prevents a familiar sorted example from becoming an unsupported claim about every binary tree.
Use the traversal that matches the task. Preorder can record a root before descendants for copying or prefix structure; postorder can process children before deleting or aggregating a parent; inorder exposes the left-node-right relationship; breadth-first order uses a queue to visit by depth.
The correct justification names the timing requirement, not merely a memorised application label.
What this chapter covers
- 01
Root, parent, child and leaf
- 02
Depth and height
- 03
Recursive subtrees
- 04
Preorder traversal
- 05
Inorder traversal
- 06
Postorder and reconstruction
Worked example · free
Write three traversal sequences
- 3Produce preorder.
- 3Produce inorder.
- 3Produce postorder.
Key terms
- Root
- The unique node with no parent.
- Leaf
- A node with no children.
- Depth
- The number of edges from the root to a node.
- Height
- The longest downward edge path from a node to a leaf.
Tree Structure and Traversal FAQ
What do preorder and postorder names refer to?
They refer to whether the current node is processed before or after its recursive subtree visits. They do not describe a left-to-right sweep of a finished drawing.
Why do height answers differ by one across sources?
Some conventions count edges and others count nodes. State the base case and follow the convention used in the question; under edge counting a leaf has height zero exactly.
Can one traversal sequence determine a unique tree?
Usually not. Inorder paired with preorder or postorder can reconstruct a binary tree with distinct keys because one sequence identifies roots and inorder splits left keys from right keys recursively.
Exam move
Write the three recursive templates from memory and apply them to irregular trees with missing children. Check that each output lists every node exactly once. Annotate the pending call stack for one difficult trace, then reconstruct a tree from inorder paired with preorder and verify both sequences from the result.
Maintain a separate vocabulary sketch for parent, sibling, ancestor, depth, height and subtree so visual placement never substitutes for the formal relationship.
Working through Tree Structure and Traversal in COMP5112? Sia is AskSia’s AI Computer Science tutor — ask any COMP5112 Tree Structure and Traversal question and get a clear, step-by-step explanation grounded in how COMP5112 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.