Binary Search Trees in Lean
This book is a guided reading of lean-bst, a Lean 4 library of checked knowledge about binary search trees. It formalizes the standard models of BST computation and the equivalences between them, the classical bounds on request sequences together with Wilber's lower bound, and the main algorithms with their guarantees: splay trees with the theorems of Sleator and Tarjan's 1985 paper, the geometric GreedyASS with its online simulation by a BST and its weighted dynamic finger bound, the $O(\log\log n)$-competitive Tango trees, static trees searched with a lazy finger: their characterization, their place among the other bounds, and an algorithm that computes the best one; and two general transformations, one that combines two BST algorithms into one as good as the better of them on every window, and one that removes amortization, giving $O(\log n)$ worst-case time per access. Together they give one BST with the weighted dynamic finger bound, $O(\log\log n)$-competitiveness and $O(\log n)$ worst-case time at once.
You know what a binary search tree is, and what a rotation does. The question this book helps you answer is narrower: do the Lean definitions say what you think they say, and do the theorems say what the paper says? A machine-checked proof guarantees that the statement follows from the definitions. It cannot tell you whether those are the right definitions and the right statement. Only a reader can check that, and the reading is easier with explanations next to the code.
So each definition and theorem statement in the library’s public interface appears below as it was compiled. The prose between them explains what each piece models, why it is phrased the way it is, and where the formal choices differ from textbook conventions. Proofs are not explained: Lean has checked them, and you can open any of them from its code block.
If you only want to check one result, you do not need to read the book in order. Every chapter or section that proves something opens with a box that states its main result in English and in Lean. Checking a result lists all of them, marks the key results that the formalized papers announce, and computes, for any selection of results, the few definitions you must read to trust them. The book then highlights those definitions wherever they are explained.
How to read the Lean blocks
Here is a block, taken from the first chapter:
- Hover over any identifier, such as
TreeorNat, to see its signature and docstring, printed by Lean. The data comes from the compiler’s own reference tables (the.ileanfiles), so the link points to the constant Lean actually resolved, not to a guess based on the name. - Click an identifier to jump to where this book explains it. For library internals not covered here, the link opens the source on GitHub. For Lean, Std and Mathlib names, it opens the Mathlib documentation.
- The badge lists the kernel axioms the declaration depends on, as computed by
collectAxioms. “No axioms” means fully constructive. Most analytic results use Lean’s three standard axioms:propext,Classical.choiceandQuot.sound. AsorryAxdependency, meaning an unfinished proof, would show up in red; none does. - Theorems from the library’s reading interface carry a “statement reads only” line, explained below.
- show proof expands a collapsed proof, when there is one. In the reading interface most theorems end with
:= Bridge.foo …: the statement is spelled out in the readable file and the proof lives elsewhere.
The library and this book are built from the same sources. The build fails if a name mentioned here no longer exists, or if the compiled reference data is out of date with respect to the sources.
The reading interface
The library is laid out as a textbook rather than by source paper: Models/ holds the BST models and their equivalences, Bounds/ the sequence bounds and their relations, Classical/ the classical search trees, Algorithms/ one directory per adaptive algorithm with its definition followed by its bounds, and Structures/ the supporting data structures. The parts of this book follow the same order, with one exception: the tools for replaying and mirroring pointer computations live mostly under Models/, but they serve only the transformations at the end of Part IV and the structures of Part V, so they are explained in Chapter 36.
Within each directory the library follows the abstractlean convention. Every topic has a short file called Abstract.lean containing only definitions and theorem statements; proofs are imported through a single AbstractBridge.lean module, and internal lemma contracts and definitions of a proof are named Results.lean and Defs.lean. Each statements file ends with a command like
#check_bst_abstract RotationBST.Splay.Results allowing [
RotationBST.Models.Rotation.Abstract, RotationBST.Models.Online.Abstract, …]
This command fails to compile unless every library definition mentioned in a theorem statement, directly or transitively, comes from one of the listed modules. The proofs are exempt: they may use anything. So to trust a theorem you need to read its statement and the definitions in the listed modules, and nothing else from the library. This book shows that list under every theorem, linked to the chapters where those modules are explained. Appendix A describes the checks in more detail and summarizes them for all theorems.
Conventions at a glance
These conventions hold throughout the library. Each is justified where it first appears.
| Convention | |
|---|---|
| Keys | A tree on keys stores no keys at all. Its keys are the inorder ranks (Chapter 1); arbitrary ordered keys are recovered exactly in Chapter 4. |
| Access | Serving request means rotating until is at the root (Chapter 3). |
| Rotation cost | The number of rotations, at any depth. No pointer movement is charged. |
| Total cost | for requests (Chapter 5). Without the , a request for the current root would be free. |
| Pointer cost | One per cursor move or rotation, plus one per request (Chapter 7). Within constant factors this is the same as . |
| Logarithms | Base 2. Real.logb 2 on reals, with Mathlib’s convention ; Nat.log2 (floor) on naturals. |
| Geometric cost | One per point of an arborally satisfied set; the request points are included (Chapter 9). Up to constant factors and an additive , this is the same as (Chapter 12). |
| Startup | Bounds that hold for every initial tree include an explicit additive term for it, usually . |
Interactive figures
The figures let you rotate, search and splay, run move-to-root and GreedyASS, and watch preferred paths change. They do not imitate the algorithms: they run JavaScript transcriptions of the executable Lean definitions, one equation at a time and with the same names. The book’s test suite compares these transcriptions against values computed by the Lean functions themselves, on exhaustive small cases, such as all shapes with at most six nodes, and on larger pseudo-random ones. Here is one; click any node that is not the root.
Map of the book
The book has five parts, and each opens with an introduction that surveys it.
Part I · Models fixes what a BST execution is and what it costs. The basic model has unlabelled shapes, rotations, and root accesses charged by the number of rotations. Labelled trees on arbitrary keys correspond to it exactly (Chapter 4), and online algorithms and dynamic optimality are defined on top of it. A model with insertions and deletions serves the dictionaries of Part III. The standard models of the literature follow: a single-pointer machine, the touched-region model, and the geometric view, in which an execution is a set of points in the plane. Two more models serve later results: a BST with several fingers, and static linked searches in an explicit pointer memory. Chapter 12 proves that the models agree, with explicit constants, and the introduction to the part draws them on one map. The part ends with real-world implementations: machines with bounded memory whose running time is charged, compiled to the general pointer machine.
Part II · Bounds studies request sequences without reference to an algorithm. Chapter 14 defines the classical benchmarks (working set, static optimality, static and dynamic finger, the unified charge, a fixed comparison tree) and proves the implications between them. The next two chapters study the lazy finger, a static tree in which each search starts where the previous one ended: its characterization by the weighted dynamic finger bound, and its place between static trees and dynamic BSTs. The part ends with two limits: Wilber’s interleave bound, a lower bound on every execution, and a conditional-entropy benchmark that no BST can meet. The introduction to the part maps all the benchmarks, the relations between them, and the algorithms that meet them.
Part III · Classical search trees collects the trees that do not adapt to the requests, which are the baseline for everything after: static trees, including entropy trees and Knuth’s optimal trees, rank-balanced trees (AVL, red-black, WAVL), weight-balanced trees (BB[α], scapegoat) and random trees (random BSTs, treaps). Most of it is formalized; the chapters state the rest, deletions among them, in boxes marked “Not yet formalized”, and the library’s roadmap lists them.
Part IV · Adaptive algorithms defines each algorithm, then proves its bounds. An algorithm with several analyses is one chapter with one section per analysis. Move-to-root is the simplest rule. Splay trees get the theorems of Sleator and Tarjan’s 1985 paper. GreedyASS, the greedy algorithm of the geometric view, becomes an online BST and is as good as the best static lazy-finger tree. Tango trees are -competitive. Two static structures follow: a dynamic program that computes the optimal lazy-finger tree, and multiple trees that trade space for conditional-entropy query time. The part ends with two transformations and their combination. Combining search trees makes one BST as good as the better of two on every window. Deamortization gives any online BST algorithm worst-case cost per access, and gives a deamortized splay tree. The last chapter applies both to GreedyASS and Tango: Theorem 4.3 of Iacono and Langerman.
Part V · Structures contains the data structures used inside proofs and algorithms: treaps, split trees, prefix sums and weighted search trees, the simulation of several fingers by one, the replay tools that treat pointer computations as data, pop-tarts and heavy paths, which hold the source tree of the deamortization at logarithmic depth, and the tree buffers and snapshots with which the combination saves and restores its initial tree.
Appendices. Appendix A describes what Lean checks and what it cannot, and lists every theorem of the reading interface with its axioms. Appendix B indexes every declaration shown. Appendix C lists the works cited, with links to their published versions and preprints.
The main sources are the splay tree paper (Sleator and Tarjan 1985); move-to-root (Allen and Munro 1978); Wilber’s lower bounds (Wilber 1989); the greedy algorithm (Lucas 1988; Munro 2000); Tango trees (Demaine et al. 2007); the geometric view, GreedyASS and the online simulation (Demaine et al. 2009); the comparison of BST models in Derryberry’s thesis (Derryberry 2009); the lazy finger (Bose et al. 2016); the weighted dynamic finger (Iacono and Langerman 2016); the multi-finger simulation, ComboBST, tree buffers and snapshots (Demaine et al. 2013); pop-tarts, heavy-path simulation and deamortization (Bose et al. 2012a); and the potential used in the scanning proof (Kakimura and Terai 2026). The bibliography lists every work cited, and each part ends with bibliographic notes on who proved what, and when.