Sub topic 15
Rank and Select with Augmented Trees
This chapter assumes you know how a balanced binary search tree (BST) works, that its operations cost O(log n), and what floor and ceiling do on an ordered map. Standard libraries answer "what is the next key?" well. They do not answer "what is the 1,000th smallest key?" quickly. Augmented trees fill that gap.
Two questions an ordered map cannot answer fast
- Rank: how many keys are smaller than key
k? - Select: which key has rank
r, counting from zero?
With a plain TreeMap, both take O(n), because you must walk the keys in order and count. A leaderboard that must show "you are 4,812th" on every request cannot afford that.
The idea: store a subtree size in each node
An augmented tree keeps ordinary BST ordering and adds one extra field per node: size, the number of nodes in the subtree rooted there. A leaf has size 1. For any node:
size = size(left) + 1 + size(right)
The size of the left subtree tells you how many keys sit before the node's own key. That is enough to steer a search by count instead of by key.
Select: find the key at rank r
Start at the root. Let L be the size of the left subtree.
- If
r < L, the answer is in the left subtree. Go left with the samer. - If
r == L, the current node is the answer. - If
r > L, go right and search for rankr - L - 1.
In the tree above, select rank 3 starts at 50, where L = 2. Since 3 > 2, go right with r = 0. At 70, L = 0, so 70 is the answer. Sorted order is 20, 30, 50, 70, 80, and index 3 is 70.
Rank: count keys smaller than k
Walk down as in a normal lookup and keep a running count:
- If
kis less than the node's key, go left. - If
kis greater, addsize(left) + 1to the count and go right. - If equal, add
size(left)and stop.
Both operations visit one root-to-leaf path, so they cost O(log n) in a balanced tree.
A runnable sketch
This version skips balancing to stay short. In real code the same fields live inside an AVL or red-black tree.
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
self.size = 1
def size(n):
return n.size if n else 0
def insert(n, key):
if n is None:
return Node(key)
if key < n.key:
n.left = insert(n.left, key)
elif key > n.key:
n.right = insert(n.right, key)
n.size = size(n.left) + 1 + size(n.right)
return n
def select(n, r):
left = size(n.left)
if r < left:
return select(n.left, r)
if r == left:
return n.key
return select(n.right, r - left - 1)
def rank(n, key):
count = 0
while n:
if key <= n.key:
n = n.left
else:
count += size(n.left) + 1
n = n.right
return count
root = None
for k in [50, 30, 70, 20, 80]:
root = insert(root, k)
print(select(root, 3)) # 70
print(rank(root, 70)) # 3Here rank counts keys strictly smaller than key, so it works even if key is absent.
Costs and gotchas
- Every insert and delete must update
sizealong the path. Rotations must recompute the sizes of the rotated nodes, bottom one first. - Memory grows by one integer per node.
- Java's
TreeMapand C++'sstd::mapdo not expose rank or select. Python'sbisectgives rank on a sorted list in O(log n), but insertion there is O(n). For tree-based rank in C++, GCC offers__gnu_pbdstree policies. Otherwise, write the tree yourself.
Exercise
In the tree from the diagram, what does rank(root, 60) return, and why does it work even though 60 is not in the tree?
Show answer
It returns 3. The search goes right of 50 (adding size(left)+1 = 3), then left of 70, then stops at an empty child. Three keys (20, 30, 50) are smaller than 60. Rank counts smaller keys, so the key does not need to be present.