Sub topic 6
Rebuilding a Tree from Two Traversals
Assume you know recursion, arrays, object references, and the meanings of preorder and inorder traversal. This chapter uses binary trees with distinct keys. Distinct keys matter because a key must identify exactly one position in the inorder sequence.
Why two traversals are needed
A single traversal usually does not determine one unique binary tree. For example, several different trees can have the same preorder sequence. A suitable pair gives enough information to split the tree repeatedly.
The useful pair is:
- Preorder and inorder: determines the tree when keys are distinct.
- Inorder and postorder: also determines the tree when keys are distinct.
- Preorder and postorder: does not always determine a unique tree.
For preorder, the first item is the root. In inorder, everything before the root belongs to the left subtree, and everything after it belongs to the right subtree.
Recursive reconstruction
Suppose:
- preorder is
[A, B, D, E, C, F] - inorder is
[D, B, E, A, F, C]
The first preorder item, A, is the root. In inorder, A is at index 3. Therefore:
- left subtree keys are
[D, B, E] - right subtree keys are
[F, C]
The next preorder items must be divided using the size of the left subtree. The left subtree has three keys, so its preorder sequence is [B, D, E]. The right subtree sequence is [C, F].
The same process continues until a sequence is empty.
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
def build(preorder_seq, inorder_seq):
if not preorder_seq:
return None
root_key = preorder_seq[0]
root = Node(root_key)
split = inorder_seq.index(root_key)
left_count = split
root.left = build(
preorder_seq[1:1 + left_count],
inorder_seq[:split]
)
root.right = build(
preorder_seq[1 + left_count:],
inorder_seq[split + 1:]
)
return rootThe recursive base case is an empty sequence. The split index gives the number of nodes in the left subtree. That number determines where the preorder sequence must be divided.
Efficiency and common errors
The slicing version is easy to understand, but it copies arrays at every recursive call. Searching with inorder_seq.index also scans the sequence. A more efficient implementation stores each inorder key's index in a dictionary and passes index ranges instead of creating slices.
Common errors include:
- Taking the first inorder item as the root. The root comes from preorder.
- Forgetting that preorder includes the root before the left subtree.
- Using the root's inorder index as the number of right-subtree nodes. It is the number of left-subtree nodes.
- Assuming duplicate keys are harmless. Without unique identifiers, the split can be ambiguous.
- Treating preorder plus postorder as always unique. A node with only one child can often be attached in two ways.
Exercise
Given preorder [M, J, H, K, R] and inorder [H, J, K, M, R], which key is the root, and which keys belong to its left subtree?
Show answer
M is the root. In inorder, H, J, and K occur before M, so they form the left subtree. R forms the right subtree.