Binary Search Trees in Lean Check a result Declarations Bibliography Lean source

Part IModels

Part I fixes what a binary search tree execution is and what it costs. The literature uses several models for this, each convenient for some arguments, and the book uses all of them. This introduction lists them and shows how they relate. Every model is defined in its own chapter, and Chapter 12 proves the main correspondences. The last chapter, on real-world implementations, charges what the BST models leave free: the time spent choosing the BST operations, and the memory used for it.

The models

Chapters Model An execution is Cost
1–3 Rotations A sequence of root accesses on a tree whose keys are its inorder ranks, changed by rotations. Each request rotates anywhere until the requested key is at the root. m+Rm + R: one per request, one per rotation
4 Labelled trees The same, on trees that store arbitrary ordered keys m+Rm + R
5 Online algorithms A rule that chooses each access from the requests so far. Its run is an execution of the rotation model. m+Rm + R
6 Insertions and deletions A sequence of accesses, insertions and deletions by rank, with balance data in the nodes. Data edits are counted separately. one per node of the search path, one per rotation
7 Pointer machine One cursor, which starts at the root for every request, moves along edges and rotates its node over its parent KK: one per request, one per instruction
8 Touched regions Each request touches a rooted subtree containing the requested key, and rearranges it the number of touched nodes
9 Geometric view An arborally satisfied set of points (key, time) containing the requests ∣E∣\lvert E\rvert: the number of points
10 Many fingers Several persistent fingers in one tree, each moving or rotating at its node one per request, one per instruction
11 Linked searches Static searches in a store of many trees joined by links, with a count of memory words. Not a BST. one per visited node and per followed link
13 Real-world machines A finite program with word registers and a fixed number of words per node, which moves and rotates one persistent finger and receives one request at a time running time: one per instruction; BST cost: one per move or rotation

The rotation model is the library’s basic one: “a legal BST algorithm” and “the offline optimum” in later theorems refer to it, unless a theorem says otherwise. The pointer machine is the standard model of the literature. Touched regions and the geometric view describe an execution by the keys it touches, which is what lower bounds and the geometric algorithms work with. The dictionary model of Chapter 6 adds updates, for the classical trees of Part III, and charges searches instead of forcing the requested key to the root. Two models serve particular results. Many fingers describe a static tree searched with a lazy finger (Chapter 15) and the multi-finger BSTs of the combination (Chapter 29). Linked searches are the model of the multiple-tree structure (Chapter 28), and are compared with nothing else. Real-world machines move one persistent finger, like a one-finger BST, but with bounded state and charged computation, and they compile to the general pointer machine of Tarjan (1979).

How the models relate

An arrow in the figure means that an execution in one model gives an execution in the other that serves the same requests, at the cost written on the arrow. The arrows between the rotation model, the pointer machine and the geometric view lose only constant factors, plus a term in nn when the initial tree is prescribed. So the offline optimum is the same in all three, up to constant factors. The bottom row does the same for online algorithms.

The table lists every correspondence, with mm requests, nn keys, RR rotations, KK pointer cost (requests plus instructions), ∣E∣\lvert E\rvert points and TT steps of a real-world machine.

From To Cost Trees Theorem
rotation execution dictionary execution of the same accesses main cost ≤m+2R\le m + 2R same final tree of_execution
shape executions labelled executions equal, both ways same up to labels execution_on_keys_iff
pointer execution rotation execution m+R+q≤3Km + R + q \le 3K final qq rotations away to_rotations
rotation execution pointer execution K≤m+2nRK \le m + 2nR same final tree to_pointer
rotation execution pointer execution K+32d≤32R+17mK + 32d \le 32R + 17m final dd rotations away to_pointer_lazy
online rotation algorithm online pointer algorithm K+32d≤32R+17mK + 32d \le 32R + 17m any initial tree simulation
one-finger lazy-finger execution pointer execution K≤464⋅costK \le 464 \cdot \text{cost} same tree same_tree
region execution point set equal, both ways initial tree chosen ass_iff_region_execution
rotation execution point set ∣E∣≤2R+m\lvert E\rvert \le 2R + m initial forgotten rotations_to_ass
pointer execution point set ∣E∣≤6K\lvert E\rvert \le 6K initial forgotten pointers_to_ass
point set pointer execution K≤33 ∣E∣K \le 33\,\lvert E\rvert initial chosen ass_to_pointers
point set rotation execution m+R≤2n+99 ∣E∣m + R \le 2n + 99\,\lvert E\rvert initial prescribed ass_to_rotations
point set pointer execution K≤64n+3168 ∣E∣K \le 64n + 3168\,\lvert E\rvert initial prescribed ass_to_pointers_from
online geometric algorithm online BST algorithm R≤6018 (points+n)R \le 6018\,(\text{points} + n) any initial tree online_bst
real-world machine run single-cursor trace from the same finger BST cost ≤T\le T same final tree physical
real-world machine execution compiled run on Tarjan’s pointer machine instructions ≤19T+3m\le 19T + 3m same final tree, as records; related initial state given, word width ≥1\ge 1 execution

Two consequences shape the rest of the book. Bounds can be stated in whichever model is most convenient and moved to the others at a constant cost: Part II states its sequence bounds for rotation-model algorithms, and Wilber’s lower bound in Chapter 17 is proved on point sets. And an algorithm can be designed in whichever model is most convenient: splaying (Section 24.1) is a rotation algorithm, GreedyASS (Section 25.1) is a geometric one, and Tango (Section 26.2) maintains a tree whose edges are marked by preferred paths.