A search tree answers “is this key here?” But suppose we also want “what is the fifth-smallest key?” We could walk through the first five keys each time. Or we could store one extra fact at each node: the number of nodes in its subtree.
Background for this page. You need the binary search-tree rule. A node’s subtree size counts itself and every descendant. Rank here starts at one. Related explanations: Binary search trees: search, insertion, and deletion · Red-black trees: balance, colors, and rotations.
If a node’s left subtree has three nodes, the node itself is fourth within its subtree. To find rank five, we can go right and look for rank one there. Each decision follows one tree path, so an order-statistic tree based on a balanced search tree can answer rank and selection questions in O(log n) time. The extra field must stay correct whenever nodes are inserted, deleted, or rotated. A fast query is only useful if updates preserve the stored fact.
The same idea helps with intervals. Suppose each record is a time interval [start, end], and we want to find one interval that overlaps a requested interval. Store intervals in a search tree ordered by their starts. At each node, also store the largest end value anywhere in its subtree. If a left subtree’s maximum end is before the query begins, that whole subtree cannot contain an overlap and can be skipped. In a balanced interval tree, finding one overlap takes O(log n) time under the usual assumptions; reporting all overlaps also takes time proportional to the number reported.
# Each node records the size of its subtree.
node.size = 1 + size(node.left) + size(node.right)
# A rotation changes the relevant subtrees.
after a rotation, recompute sizes of the moved nodes
An augmentation is not a new tree from scratch. It is a base structure plus extra information with a rule for updating that information locally. To design one, ask: What answer do I need? What fact about a subtree would let me skip work? Can that fact be recomputed from a node and its children after every structural change?
There is a tradeoff. The extra fields cost memory and make updates more delicate. If one missed update leaves a wrong subtree size or maximum end, later queries can be wrong even though the basic search-tree links are still valid. Tests should cover insertion, deletion, and rotations, not just static examples.
Update the extra information after a change
Suppose a node has subtree size eight. An insertion below it changes that size to nine, and the change propagates through every ancestor up to the root. Updating only the inserted node would leave later rank queries using stale counts.
A rotation also changes which nodes belong to which local subtrees. After a left rotation at x with right child y, recompute x’s size from its new children, then recompute y’s size from its children. Updating y first could read x’s old size. Each local recomputation takes constant time because size equals one plus the two child sizes.
The augmentation stays efficient when the underlying tree stays balanced and the added information can be maintained during its existing updates.
Practice. A node has a left subtree of size 4 and a right subtree of size 3. What is the node’s subtree size? What rank does the node have within that subtree?
Answer. Its subtree contains 1 + 4 + 3 = 8 nodes. The node has rank 5 because four keys come before it in its left subtree.
Try another case. What size should an empty child contribute? Why does that choice help?
Reasoning. Zero. Then the same formula, one plus the two child sizes, works for leaves and internal nodes without a separate leaf case.