Documentation

CslibFrontier.DataStructure.SplayTree.Impl

Self-Adjusting Stateful Ordered-Set Implementation by Splay Trees #

This module realizes the shared Cslib.DataStructure.OrderedSet.spec α interface (contains, insert, delete, minimum, successor) using annotation-free bottom-up splay trees ([ST85b], Section 3, pp. 660–665), reusing the shared Cslib.DataStructure.SearchTree.Rep representation relation (Ordered t ∧ elements t = s) and SearchTree.rep_empty directly on BinaryTree α.

Self-adjustment and amortized vs. worst-case guarantees #

Unlike Cslib.DataStructure.AVLTree.impl, splay trees store no balance metadata and restructure the concrete BinaryTree α even during read queries (contains, minimum, successor). The Cslib.DataStructure.StateTransitionImpl framework accommodates this directly: read queries leave the abstract Finset α state unchanged while transitioning t to a newly splayed tree t' satisfying SearchTree.Rep s t'.

Because a single operation on an unbalanced initial tree can perform up to s.card - 1 rotations, no logarithmic per-operation worst-case Cslib.DataStructure.Bound holds on arbitrary trees. Instead, by telescoping unitPotential across any adaptive client execution (Executes Exec), this module proves a single-tree OrderedSet.spec α dictionary specialization/analogue of Sleator and Tarjan's Balance Theorem with Updates ([ST85b], Section 3, Theorem 6, p. 665; whereas Theorem 6 treats multi-tree sequences of access, insert, delete, join, and split, here we instantiate single-tree contains, insert, delete, minimum, and successor and count exact edge rotations, with unit-weight split/join bounds proved in AccessLemma.lean):

Cost model and limitations #

Native traces record exact binary-tree edge rotations per operation (0 on empty trees). A single splay or splayMin descent of a non-empty tree visits rotations + 1 nodes (rotations_splay_add_one_eq_searchVisits, rotations_splayMin_add_one_eq_minimumVisits). delete and successor may make a second descent (splayMax inside join, or splayMin on the right subtree) and then record the rotations of both descents, plus one final rotation in successor; no visits identity is proved for splayMax. Ancestor-stack allocation, node allocation, key-comparison implementation, and pure client continuations are not charged as RAM or Turing-machine instructions.

Main definitions #

Main results #

References #

Execute one OrderedSet.Op α on a splay tree, splaying on queries as well as updates. The trace is the exact number of binary-tree edge rotations executed.

@[reducible, inline]

Native execution relation for OrderedSet.spec α over self-adjusting BinaryTree α, recording exact rotation counts.

Self-adjusting semantic realization of OrderedSet.spec α by BinaryTree α, reusing Cslib.DataStructure.SearchTree.Rep.

Amortized Rotation Bounds Across Adaptive Clients #

noncomputable def Cslib.DataStructure.SplayTree.realOpCharge {α : Type} (s : Finset α) :

Continuous per-operation amortized rotation charge 7 log₂(|s| + 1) + 3.

Sum of realOpCharge along an abstract OrderedSet.spec α operation history.

Discrete ℕ-valued per-operation amortized rotation budget 7 * Nat.log 2 (s.card + 1) + 10, upper-bounding realOpCharge s o via ⌊log₂(n + 1)⌋ + 1.

theorem Cslib.DataStructure.SplayTree.exec_unitPotential_amortized_le {α : Type} [LinearOrder α] {s : Finset α} {t t' : BinaryTree α} {o : OrderedSet.Op α} {a : (OrderedSet.spec α).Result o} {n : ℕ} (hr : SearchTree.Rep s t) (he : Exec t o a t' n) :

Uniform unit-weight amortized rotation bound across all five OrderedSet.Op α operations, combining the unit-weight specialization of [ST85b], Section 3, Lemma 2, pp. 663–664 for contains, insert, and delete with the companion Access-Lemma bounds for minimum and successor.

theorem Cslib.DataStructure.SplayTree.executes_amortized_real_le {α : Type} [LinearOrder α] {β : Type} {p : (OrderedSet.spec α).Client β} {s : Finset α} {t t' : BinaryTree α} {a : β} {ns : List ℕ} (hr : SearchTree.Rep s t) (he : Executes Exec p t a t' ns) :
match (OrderedSet.spec α).runClient s p with | (fst, fst_1, history) => ↑(total id ns) + unitPotential t' - unitPotential t ≤ realHistoryCost s history

Single-tree OrderedSet.spec α dictionary analogue of the Balance Theorem with Updates ([ST85b], Section 3, Theorem 6, p. 665; real-valued telescoping form): for any adaptive client p executed from SearchTree.Rep s t, the total rotation count plus potential change is bounded by realHistoryCost along the induced abstract history.

theorem Cslib.DataStructure.SplayTree.executes_rotations_le_realHistoryCost_of_empty {α : Type} [LinearOrder α] {β : Type} {p : (OrderedSet.spec α).Client β} {t' : BinaryTree α} {a : β} {ns : List ℕ} (he : Executes Exec p BinaryTree.nil a t' ns) :
match (OrderedSet.spec α).runClient ∅ p with | (fst, fst_1, history) => ↑(total id ns) ≤ realHistoryCost ∅ history

Starting from .nil (SearchTree.rep_empty), the real-valued total rotation count of any adaptive OrderedSet.spec α client p is bounded by realHistoryCost along its abstract history (single-tree dictionary analogue of [ST85b], Section 3, Theorem 6, p. 665).

theorem Cslib.DataStructure.SplayTree.executes_total_le {α : Type} [LinearOrder α] {β : Type} {p : (OrderedSet.spec α).Client β} {s : Finset α} {t t' : BinaryTree α} {a : β} {ns : List ℕ} (hr : SearchTree.Rep s t) (he : Executes Exec p t a t' ns) :
match (OrderedSet.spec α).runClient s p with | (fst, fst_1, history) => total id ns ≤ (OrderedSet.spec α).totalCost amortizedCharge s history + s.card * (Nat.log 2 (s.card + 1) + 1)

Discrete ℕ-valued total rotation bound from an arbitrary initial tree SearchTree.Rep s t, charging amortizedCharge per operation plus the initial potential allowance s.card * (Nat.log 2 (s.card + 1) + 1).

theorem Cslib.DataStructure.SplayTree.executes_total_le_of_empty {α : Type} [LinearOrder α] {β : Type} {p : (OrderedSet.spec α).Client β} {t' : BinaryTree α} {a : β} {ns : List ℕ} (he : Executes Exec p BinaryTree.nil a t' ns) :
match (OrderedSet.spec α).runClient ∅ p with | (fst, fst_1, history) => total id ns ≤ (OrderedSet.spec α).totalCost amortizedCharge ∅ history

Single-tree OrderedSet.spec α dictionary analogue of the Balance Theorem with Updates ([ST85b], Section 3, Theorem 6, p. 665; ℕ-valued StateTransitionSpec.totalCost form): starting from .nil, any adaptive client p executes at most (spec α).totalCost amortizedCharge ∅ history total rotations.

Executable Adaptive Client Runner #

@[reducible, inline]

Execute an adaptive OrderedSet client concretely on a splay tree, recording exact rotation counts per operation.

theorem Cslib.DataStructure.SplayTree.run_correct {α : Type} [LinearOrder α] {β : Type} (p : (OrderedSet.spec α).Client β) {s : Finset α} {t : BinaryTree α} (hr : SearchTree.Rep s t) :
match (OrderedSet.spec α).runClient s p with | (value, finalState, history) => (run t p).value = value ∧ SearchTree.Rep finalState (run t p).state ∧ total id (run t p).traces ≤ (OrderedSet.spec α).totalCost amortizedCharge s history + s.card * (Nat.log 2 (s.card + 1) + 1)

Running the concrete splay-tree interpreter from SearchTree.Rep s t agrees with abstract Finset α semantics, preserves SearchTree.Rep, and satisfies the amortized logarithmic rotation bound.

Running an adaptive OrderedSet client starting from .nil agrees with Finset α semantics, preserves SearchTree.Rep, and satisfies total id (run .nil p).traces ≤ totalCost amortizedCharge with zero initial potential overhead.