Part IIIClassical search trees
Part I and Part II fixed the models and the benchmarks. Part III collects the classical binary search trees, which do not adapt to the requests. Their shape depends on the set of keys, on the history of insertions and deletions, and for random trees on random choices. It never depends on which keys are searched. They are the baseline that the adaptive algorithms of Part IV are measured against.
The baseline is simple to state. A search visits at most as many nodes as the height of the tree, counted in nodes, and a balanced tree has height . So searches cost at most . Every benchmark of Part II is at most this much, up to constant factors: it is the bottom box of the map of the bounds. In general it cannot be beaten. On the bit-reversal sequence every BST pays per request (Chapter 18). What a classical tree cannot do is exploit an easy sequence. On a sequential scan, a balanced tree still pays per request, while splay trees pay a constant amortized (Section 24.3.4).
The part has four chapters, one per idea for keeping a tree shallow. Each chapter presents one theory, and the classical structures as its instances, rather than one analysis per structure:
- Chapter 19: static trees never change. They are built once, from the number of keys or from access frequencies. The theory is the cost of a fixed tree, its entropy bounds, and the optimal tree by dynamic programming.
- Chapter 20: rank-balanced trees store an integer rank in each node and keep the rank differences within fixed rules. AVL, red-black and WAVL trees are three choices of rules.
- Chapter 21: weight-balanced trees keep every child’s subtree a bounded fraction of its parent’s. They restore the invariant either with rotations (BB[α] trees) or by rebuilding a subtree (scapegoat trees).
- Chapter 22: random trees leave the shape to chance. Random insertion orders, treaps and randomized BSTs all draw from the same distribution.
Dynamic trees change their key set, so they need a model with insertions and deletions. It is described in Chapter 6.
The structures at a glance
The table lists the guarantees that the chapters state. Here is the number of keys, heights count nodes, and a rotation count is per insertion or deletion. Every operation also pays for its search path, at most the height.
| Structure | Height | Rotations per update | Status | Chapter |
|---|---|---|---|---|
| Balanced static tree | , the minimum | none (static) | formalized | 19 |
| Entropy tree for given frequencies | key at depth | none (static) | formalized | 19 |
| Optimal static tree (Knuth) | depends on the frequencies | none (static); its cost computed in steps | formalized | 19 |
| AVL tree | insertion: one single or double rotation; deletion: | height and insertion formalized; deletion not yet | 20 | |
| Red-black tree | insertion , deletion | height formalized; updates not yet | 20 | |
| WAVL tree | , and after insertions | ; amortized rank changes from empty | height and insertion formalized; deletion not yet | 20 |
| BB[α] tree, | , for any | amortized | height formalized; rebalancing not yet | 21 |
| Scapegoat tree, | depth | amortized cost, by rebuilding | formalized | 21 |
| Random BST, treap | expected depth | treap: fewer than 2 expected | expected depth formalized; treap updates not yet | 22 |
What is formalized
The library proves most of this part:
- Static trees (Chapter 19): the balanced tree is as shallow as possible; the logarithmic baseline is met by the balanced tree, and paid by every static tree on some sequence; the entropy bounds; the optimal static tree by the recurrence, Yao’s quadrangle inequality, and Knuth’s algorithm.
- Rank-balanced trees (Chapter 20): the height bounds of AVL, red-black and WAVL trees, and WAVL insertion, which is also AVL insertion, with at most two rotations, and amortized rank changes for insertions from the empty tree.
- Weight-balanced trees (Chapter 21): the height bound for any balance parameter, rebuilding with two rotations per key, and scapegoat trees with logarithmic depth and amortized cost per update.
- Random trees (Chapter 22): the exact expected depth in a random BST, and the fact that insertion in a given order builds the treap of that order.
- The update model (Chapter 6), shared by all of them, which charges each operation its search path and its rotations.
What is missing is in dashed boxes marked “Not yet formalized”: deletion and red-black insertion, BB[α] rebalancing, treap updates by rotations, and the other samplers of the random BST. The library’s roadmap gives a target Lean statement for each of them, for a later round of proofs. The results that are hard to formalize are listed there as stretch goals.