Definition · Size-augmented search treeA search tree in which every node x stores size[x] = size[left(x)] + size[right(x)] + 1, the number of nodes in the subtree rooted at x. An empty subtree has size 0.
The field costs one machine word per node and is maintained by insertion, deletion and rotation. Nothing else about the tree changes.
select(i), the i-th smallest key, counting from 1. At a node, let L be the size of its left subtree. If i = L + 1 the answer is this node’s key, because exactly L keys in this subtree are smaller. If i ≤ L the answer is in the left subtree at the same rank. Otherwise it is in the right subtree at rank i − (L + 1), because the left subtree and this node account for L + 1 of the keys below it.
rank(k), how many keys are at most k. Walk toward k from the root, keeping a running count. Each time the walk goes right, every key in the left subtree and the node itself are below k, so add L + 1. Each time it goes left, add nothing. When k is found, add L + 1 one last time.
tree from 50 25 75 12 37 62 87 6 18 sizes in brackets
50 [9]
25 [5] 75 [3]
12 [3] 37 [1] 62 [1] 87 [1]
6 [1] 18 [1]
in-order 6 12 18 25 37 50 62 75 87
rank 1 2 3 4 5 6 7 8 9
select(3): at 50, L = 5, 3 ≤ 5 go left
at 25, L = 3, 3 ≤ 3 go left
at 12, L = 1, 3 > 1+1 → i = 1 go right
at 18, L = 0, 1 = 0+1 ANSWER 18
4 nodes visited
rank(62): at 50, 62 > 50 → add 5+1 = 6, go right
at 75, 62 < 75 → add 0, go left
at 62, found → add 0+1 = 1, ANSWER 7
3 nodes visited
Four nodes for select(3) and three for rank(62), on a tree of height 3. Both walks are one path and nothing else, which is the whole claim: O(height), and O(log n) once the tree is balanced. Without the size field, select(3) would need an in-order traversal counting as it went — O(n) — and rank would need the same. The 3 and the 4 are counts on the tree drawn, whose height is 3 because that insertion order was kind; the claim is O(height), and on the sorted order of the shape-problem lesson the identical queries would walk n. The augmentation buys O(log n) only on top of something that keeps the tree short, which is why an order-statistic tree is an AVL tree with a size field rather than a search tree with one.
Counting a range, as two walks
How many keys lie in [20, 70]? Count the keys at most 70 and subtract the keys below 20. Each count is a rank-style walk adding a subtree size at every right turn, so the query is two paths rather than a scan: the lab reports 4 — 25, 37, 50, 62 — from 7 nodes visited across both walks. Listing the keys would of course cost the number of keys listed; counting them does not.
What can be augmented, and what cannot
Theorem · The augmentation criterionAn attribute f can be maintained on a search tree at constant extra cost per structural change if f(x) is computable in constant time from the key at x and the values f(left(x)) and f(right(x)).
Insertion and deletion change f only on one root-to-leaf path, and a rotation changes it only at the two nodes it rearranges; in each case recomputing bottom-up from the criterion costs a constant per node touched. Size satisfies it with 1 + left + right, sum with key + left + right, min with min(key, left). A median does not: the median of a set is not a constant-time function of one key and the medians of two subsets, so the field would have to be rebuilt and the technique buys nothing.
That criterion is the reusable part of this lesson. It is why an interval tree can store the maximum endpoint in a subtree, why an order-statistic tree stores sizes, and why nobody stores a median. The question to ask about any proposed field is not whether it is useful but whether a node could recompute it from its two children without looking further down.
The rotation that has to update the field
Rotate left at 25 in the tree above. The node 37 rises, 25 becomes its left child, and the sizes must be recomputed at 25 first and then at 37:
before after rotating LEFT at 25
50 [9] 50 [9]
25 [5] 75 [3] 37 [5] 75 [3]
12 [3] 37 [1] 25 [4]
12 [3]
25: 3 + 0 + 1 = 4 recomputed first, from its own children
37: 4 + 0 + 1 = 5 then the new subtree root
50: 5 + 3 + 1 = 9 unchanged, as it must be
The order matters: recompute the node that descended before the node that rose, because the riser’s value depends on it. The lab checks every size in the tree against a fresh recount after the rotation and reports whether they all agree — which is the kind of check worth having, because a stale size field produces wrong answers from a structurally perfect tree and no error anywhere.