Chapter 3Accesses and executions
In the usual pointer model a search walks from the root to the requested key, and restructuring is whatever the algorithm does along the way. The library’s basic model is the root-access rotation model instead, in the style of Wilber. To serve a request for , the algorithm performs any rotations it likes, anywhere in the tree, and must finish with at the root. Its cost is the number of rotations. Chapter 7 proves that this model and a conventional pointer machine agree up to constant factors.
This chapter defines the model: a root access Access serves one request, and an execution Execution serves a whole sequence. These two definitions are what “a legal BST algorithm” and “the offline optimum” mean in every later theorem. The chapter then shows that the model is not too restrictive. Every existing key can be brought to the root with fewer than rotations, and only existing keys can be. Any shape can be turned into any other shape of the same size with at most rotations, which is why the initial tree matters only additively.
Let and be binary trees with the same number of nodes. Then some sequence of at most rotations turns into .
In Lean, Geometry.reset concludes ∃ k ≤ 2 * s.size, Rotations s t k, where Rotations s t k says that exactly k rotations turn s into t.
The Lean statement
/-- Any two shapes on the same number of keys are connected by at most `2n`rotations. The initial and final shapes are prescribed, including when empty. -/theorem reset (s t : Tree) (h : s.size = t.size) : ∃ k, k ≤ 2 * s.size ∧ Rotations s t k := … reset_linear s t hModels/Rotation/Connectivity/Abstract.lean and its imports3.1 Root accesses
/-- Access rank `x` from `s`, finish at `t`, using `k` rotations. -/def Access (s t : Tree) (x k : Nat) : Prop := Rotations s t k ∧ match t with | .nil => False | .node l _ => l.size = xAccess s t x k holds when k rotations turn s into t and the root of t has rank x. The root of node l r has rank l.size (Chapter 1), so “x is at the root” is written l.size = x. An empty final tree has no root, so nil gives False.
- The rotations are unrestricted. They need not lie on the search path, and they may touch parts of the tree the search never visits. This is deliberate:
Accessdescribes everything an algorithm is allowed to do. Particular algorithms, such as splaying, satisfy it with specific rotation sequences, and the simulation theorems show that the freedom does not make the model stronger than a pointer machine by more than a constant factor. - The cost is rotations only. A request whose key is already at the root can be served by
Access s s x 0. This is why the conventional total cost in Chapter 5 adds one unit per request. - The definition is a
Prop, not a function: it says which outcomes are legal, not how to compute one.
An access can only request an existing key.
Theorem. If rotations turn into a tree whose root has rank , then .
The library proves this as Access.in_range: rotations preserve size, and the root of a tree of size has rank below .
3.2 Executions
An offline execution serves a whole request sequence, one root access after another:
/-- An offline execution serving an entire request sequence. -/inductive Execution : Tree → List Nat → Tree → Nat → Prop where | nil {t : Tree} : Execution t [] t 0 | cons {s t : Tree} {x k : Nat} {xs : List Nat} {u : Tree} {m : Nat} : Access s t x k → Execution t xs u m → Execution s (x :: xs) u (k + m)Execution t xs u k holds when the requests xs can be served in order, starting from t and ending at u, with k rotations in total. Each request is served by an Access from the tree left by the previous one, and the costs add. Since the strategy is existentially quantified, Execution describes every legal offline strategy at once. In particular the offline optimum for t and xs is the least k for which some Execution t xs u k exists. Chapter 5 compares online algorithms against all executions directly, rather than defining this minimum.
Executions also preserve size.
Theorem. If an execution serves a sequence from and ends at , then and have the same number of nodes.
This is Execution.size_eq, by induction on the execution. Both lemmas are proof-level facts, in Models/Rotation/Basic.lean; no theorem statement mentions them.
3.3 Which requests can be served
Every existing key can be brought to the root, and only existing keys can.
Theorem (accessibility). Let have nodes. Rank can be brought to the root of with fewer than rotations if and only if .
/-- Every existing key can be brought to the root in fewer than `size` rotations;no nonexistent key can be served. This includes the empty-tree case. -/theorem access_exists_iff (t : Tree) (x : Nat) : (∃ u k, Access t u x k ∧ k < t.size) ↔ x < t.size := … AccessBridge.exists_iff t xModels/Rotation/Existence/Abstract.lean and its importsThe bound k < t.size is the depth of in the worst case. The proof is constructive. It runs moveToRoot, which rotates over each of its ancestors in turn, deepest first, and so uses exactly as many rotations as has ancestors. The statement mentions only the model; move-to-root appears in its proof alone. Chapter 23 presents it as an algorithm, and explains why it is a good witness but a poor strategy.
Lifting access_exists_iff to sequences gives the exact executability condition. Sizes never change, so a sequence can be served exactly when every requested rank is below the initial size.
Theorem (executability). A request sequence can be served from by some execution if and only if every request in is less than .
/-- A request sequence is executable exactly when all its keys exist initially. -/theorem execution_exists_iff (t : Tree) (xs : List Nat) : (∃ u k, Execution t xs u k) ↔ ∀ x ∈ xs, x < t.size := … AccessBridge.execution_iff t xsModels/Rotation/Existence/Abstract.lean and its importsThe hypothesis ∀ x ∈ xs, x < t.size appears in nearly every theorem about request sequences in later chapters. It is precisely this condition.
3.4 Any shape to any shape
Rotations connect all shapes of a given size, and linearly many suffice.
Theorem (reset). If and both have nodes, then at most rotations turn into .
/-- Any two shapes on the same number of keys are connected by at most `2n`rotations. The initial and final shapes are prescribed, including when empty. -/theorem reset (s t : Tree) (h : s.size = t.size) : ∃ k, k ≤ 2 * s.size ∧ Rotations s t k := … reset_linear s t hModels/Rotation/Connectivity/Abstract.lean and its importsThe proof rotates both shapes into the right spine .node .nil (.node .nil …) with at most rotations each, then reverses the second sequence; rotations are reversible (Rotations.symm). The bound is the classical one, up to the additive constant in the of Sleator, Tarjan and Thurston (1988), which the library does not attempt.
An access may therefore be preceded by a reset to any shape, at an extra cost of at most .
Theorem (reset before an access). Let and have nodes. If rotations access in and end at , then at most rotations access in and end at the same .
/-- A reset may be charged before an access while retaining its requested root. -/theorem reset_before_access (s t u : Tree) (h : s.size = t.size) (ha : Access t u x k) : ∃ j, j ≤ 2 * s.size ∧ Access s u x (j + k) := … reset_then_access s t u h haModels/Rotation/Connectivity/Abstract.lean and its importsFor offline executions, the initial shape therefore matters only additively, by at most rotations. Online algorithms cannot simply reset, and the bounds of Chapter 14 instead charge an explicit startup term of the form .
The orange node is the request. Rotate until it is at the root. The panel tracks the derivation of Rotations s t k and checks the access condition l.size = x on the current tree. Move-to-root uses exactly the depth of the requested node; try to finish in that many rotations.