Order-Statistic Trees and Augmented Nodes
Augment a balanced search tree with subtree sizes to look up ranks and maintain the counts through rotations.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 2
- Reading time
- 1 min
In this article2 chapters
Descend by counting the left side
Assume distinct keys and store subtree size \(s(v)=1+s(v_l)+s(v_r)\), with size zero for an empty child. The root’s zero-based rank inside its subtree is \(s(v_l)\). For rank \(k\), descend left if \(k<s(v_l)\), return the root at equality, and otherwise descend right with \(k-s(v_l)-1\). For keys \(2,5,8,11,12\) with root \(8\), rank \(3\) reduces to rank \(0\) in the right subtree and returns \(11\).
Recompute metadata in dependency order
After a rotation, update the former root’s size before its new parent’s, since the parent now depends on it. This works because the metadata is computed from constant-size summaries of children. Balanced search trees bound the visited path by \(O(\log n)\). A Fenwick tree represents counts over fixed numeric positions rather than an evolving ordered set of keys.[1]