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

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 xx, the algorithm performs any rotations it likes, anywhere in the tree, and must finish with xx 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 nn rotations, and only existing keys can be. Any shape can be turned into any other shape of the same size with at most 2n2n rotations, which is why the initial tree matters only additively.

Main result
Any tree can be rotated into any other with at most 2n2n rotations

Let ss and tt be binary trees with the same number nn of nodes. Then some sequence of at most 2n2n rotations turns ss into tt.

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
theoremGeometry.resetpropext · Classical.choice · Quot.soundModels/Rotation/Connectivity/Abstract.lean:17
/-- 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 h
Statement reads only Models/Rotation/Connectivity/Abstract.lean and its imports
4 definitions in 2 chapters · reading list

3.1 Root accesses

defAccessno axiomsModels/Rotation/Abstract.lean:56
/-- 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 = x

Access 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.

Reading the Lean
  • 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: Access describes 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 kk rotations turn ss into a tree whose root has rank xx, then x<∣s∣x < |s|.

The library proves this as Access.in_range: rotations preserve size, and the root of a tree of size nn has rank below nn.

3.2 Executions

An offline execution serves a whole request sequence, one root access after another:

inductiveExecutionno axiomsModels/Rotation/Abstract.lean:62
/-- 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 ss and ends at tt, then ss and tt 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 tt have nn nodes. Rank xx can be brought to the root of tt with fewer than nn rotations if and only if x<nx < n.

theoremaccess_exists_iffpropext · Classical.choice · Quot.soundModels/Rotation/Existence/Abstract.lean:14
/-- 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 x
Statement reads only Models/Rotation/Existence/Abstract.lean and its imports

The bound k < t.size is the depth of xx in the worst case. The proof is constructive. It runs moveToRoot, which rotates xx over each of its ancestors in turn, deepest first, and so uses exactly as many rotations as xx 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 XX can be served from tt by some execution if and only if every request in XX is less than ∣t∣|t|.

theoremexecution_exists_iffpropext · Classical.choice · Quot.soundModels/Rotation/Existence/Abstract.lean:20
/-- 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 xs
Statement reads only Models/Rotation/Existence/Abstract.lean and its imports

The 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 ss and tt both have nn nodes, then at most 2n2n rotations turn ss into tt.

theoremGeometry.resetpropext · Classical.choice · Quot.soundModels/Rotation/Connectivity/Abstract.lean:17
/-- 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 h
Statement reads only Models/Rotation/Connectivity/Abstract.lean and its imports

The proof rotates both shapes into the right spine .node .nil (.node .nil …) with at most nn rotations each, then reverses the second sequence; rotations are reversible (Rotations.symm). The bound 2n2n is the classical one, up to the additive constant in the 2n−62n-6 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 2n2n.

Theorem (reset before an access). Let ss and tt have nn nodes. If kk rotations access xx in tt and end at uu, then at most 2n+k2n + k rotations access xx in ss and end at the same uu.

theoremGeometry.reset_before_accesspropext · Classical.choice · Quot.soundModels/Rotation/Connectivity/Abstract.lean:23
/-- 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 ha
Statement reads only Models/Rotation/Connectivity/Abstract.lean and its imports

For offline executions, the initial shape therefore matters only additively, by at most 2n2n rotations. Online algorithms cannot simply reset, and the bounds of Chapter 14 instead charge an explicit startup term of the form D⋅n(1+⌊log⁡2n⌋)D \cdot n(1+\lfloor \log_2 n\rfloor).

Bring a key to the root

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.