Order Statistic Tree in C
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
An order statistic tree is a balanced binary search tree augmented with subtree sizes. That extra size field makes it possible to answer "what is the k-th smallest element" and "what is the rank of this key" in logarithmic time, provided the tree stays balanced.
What makes the tree special
A normal binary search tree stores keys and pointer relationships. An order statistic tree stores one more piece of data on every node: the size of the subtree rooted at that node.
That single value enables two important operations:
- '
select(k): return the k-th smallest element' - '
rank(x): return how many elements are smaller thanx'
The idea is simple. If the left subtree has size L, then:
- the current node is the
(L + 1)-th smallest element - anything smaller must be in the left subtree
- anything larger must account for the whole left subtree plus the current node
Minimal node structure in C++
Here is a small node definition that demonstrates the augmentation:
This is enough to illustrate the data structure even before adding full balancing logic.
Implement select
To find the k-th smallest element, compare k with the size of the left subtree.
If the left subtree contains 3 nodes, then the root is the 4th smallest value in the subtree rooted at that node.
Implement rank
The rank of a key is the number of values smaller than it. Again, subtree sizes do most of the work.
This version returns a zero-based rank. If you want one-based ranking, add 1 to the final answer.
Maintain sizes during insertion
The augmentation is only useful if every insert, delete, and rotation updates the size field correctly.
This sample is an ordinary binary search tree, not a balanced one. It demonstrates the size bookkeeping, but it does not guarantee logarithmic height.
Why balancing matters
Order statistic queries are only fast when the underlying tree remains balanced. If inserts arrive in sorted order and the tree degenerates into a linked list, select and rank become O(n).
That is why real order statistic trees are usually built on top of:
- red-black trees
- AVL trees
- policy-based balanced trees provided by some toolchains
When rotations happen, node sizes must be recomputed immediately after pointers change. That detail is where many bugs hide.
Example usage
With the values above, the 3rd smallest element is 15, and the zero-based rank of 25 is 4.
Common Pitfalls
The most common mistake is forgetting to update size after structural changes. Inserts, deletes, and especially rotations all change subtree membership.
Another issue is assuming the augmentation alone guarantees logarithmic performance. It does not. Balance is a separate property that must be preserved by the underlying tree algorithm.
You also need a clear convention for duplicates. If duplicate keys are allowed, define whether they go left, right, or are counted in a multiplicity field. Without that policy, rank and select become inconsistent.
Finally, be explicit about indexing. Some code treats the smallest element as rank 0, while other code treats it as rank 1. Pick one convention and keep it consistent across the API.
Summary
- An order statistic tree is a balanced search tree with subtree sizes stored on each node.
- The size field enables efficient
select(k)andrank(x)operations. - Every structural update must also maintain the size metadata.
- Balance still matters; without it, the tree loses logarithmic performance.
- Clear duplicate handling and rank indexing conventions are part of a correct implementation.

