Sub topic 6
Augmenting a Tree with Extra Information
This chapter assumes you can read a recursive BST insert in Python and know that operations cost O(h) for a tree of height h. Here you will learn the general technique of storing extra data in each node. The interval tree is built on it.
The Idea
A plain BST orders nodes by one key. Some questions need more than that, such as "how many keys are in this subtree?" or "what is the largest high endpoint below this node?". Recomputing these by walking the subtree costs O(n).
Augmentation fixes this by storing the answer in each node as an extra field. The field is computed from the node's own data and from the same field in its children. A query can then read one field instead of walking a subtree.
A Small Example: Subtree Size
Each node stores size, the number of nodes in the subtree rooted at it. The rule is size = 1 + size(left) + size(right). An empty child counts as 0.
class Node:
def __init__(self, key):
self.key = key
self.size = 1 # the augmented field
self.left = None
self.right = None
def size(node):
return node.size if node else 0
def insert(node, key):
if node is None:
return Node(key)
if key < node.key:
node.left = insert(node.left, key)
else:
node.right = insert(node.right, key)
node.size = 1 + size(node.left) + size(node.right) # fix on the way back up
return node
root = None
for k in [20, 10, 30, 5, 15]:
root = insert(root, k)
print(root.size) # 5
print(root.left.size) # 3The update line runs after the recursive call returns. At that point both children are already correct, so the node can be fixed from them. The update touches only the nodes on the insertion path, which adds O(h) work to an insert that already costs O(h).
When Augmentation Is Safe
Subtree size satisfies this condition. So does "maximum high endpoint in the subtree", which is max(node.hi, left.max, right.max). That is the field the interval tree uses.
Common Errors
- Updating before recursing. The children's fields are still stale, so the parent gets a wrong value.
- Forgetting to update on one path. Every operation that changes the tree shape must refresh the fields of the affected nodes.
- Stale fields after rotations. A rotation moves a subtree to a new parent. Recompute the lower node first, then the node above it.
- Stale fields after deletion. Removing or splicing out a node changes its ancestors' subtrees. Refresh each ancestor on the way back up.
- Forgetting empty children. Treat a missing child as the identity value, which is 0 for sizes and negative infinity for maximums.
Exercise
Suppose each node stores height, the height of its subtree, with an empty subtree having height 0. Write the update rule for a node, and say whether this field can be maintained under the "own data plus children's fields" condition.
Show answer
The rule is node.height = 1 + max(height(node.left), height(node.right)), where height(None) is 0. The value depends only on the two children's heights, so the condition holds and the field can be maintained during insertion by updating the nodes on the insertion path.