The Hong Kong Polytechnic University · FACULTY OF COMPUTER SCIENCE

COMP5112 Chap.5 Tree Structure and Traversal

- one subject, every graph, every model, every mark
6 Chapters4-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 5 of 11 · COMP5112

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.

In this chapter

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

Q [9 marks]. A has children B and C; B has D and E; C has left child F. Give all depth-first orders.
The three ribbons place the current node before, between or after the two recursive subtree visits, which creates the traversal order.The three ribbons place the current node before, between or after the two recursive subtree visits, which creates the traversal order.preordernode, left, rightinorderleft, node, rightpostorderleft, right, node
The three ribbons place the current node before, between or after the two recursive subtree visits, which creates the traversal order.
  • 3Produce preorder.
  • 3Produce inorder.
  • 3Produce postorder.
Preorder A B D E C F; inorder D B E A F C; postorder D E B F C A.
Sia tip — AskSia-authored practice allocation, not an official course marking scheme. Finish one subtree completely before moving to its sibling.
Glossary

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.
FAQ

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.

Study strategy

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.

A+Everything unlocked
Unlocks this Bible + all 16 of your The Hong Kong Polytechnic University subjects - and 1,000+ Bibles across every Australian university.
Sia - your COMP5112 tutor, unlimited, worked the way the exam marks it
The full 4-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works