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):
- In
ℝ(executes_amortized_real_le), any adaptive client execution fromSearchTree.Rep s tsatisfies(total id ns : ℝ) + unitPotential t' - unitPotential t ≤ realHistoryCost s history, where each step charges7 log₂(|s_j| + 1) + 3. - In
ℕ(executes_total_le_of_empty,run_correct_empty), starting from.nil(SearchTree.rep_empty), total rotations satisfytotal id ns ≤ (spec α).totalCost amortizedCharge ∅ historywithamortizedCharge s _ = 7 * Nat.log 2 (s.card + 1) + 10, matching the discreteStateTransitionSpec.totalCostidiom ofSearchTreeandAVLTree.
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 #
step,Exec: self-adjusting single-operation execution and its rotation-count trace relation, built withCslib.DataStructure.Execution.ofStep.impl:Cslib.DataStructure.StateTransitionImplofCslib.DataStructure.OrderedSet.spec αreusingCslib.DataStructure.SearchTree.Rep.realOpCharge,realHistoryCost,amortizedCharge: continuous (Real.logb 2) and discrete (Nat.log 2) per-operation amortized rotation budgets.run: executable adaptive client runner (Cslib.DataStructure.Execution.run) returning the client value, final tree and per-operation rotation counts.
Main results #
exec_unitPotential_amortized_le: uniform unit-weight single-step rotation bound across all fiveOrderedSet.Op αoperations (combining the unit-weight specialization of [ST85b], Section 3, Lemma 2, pp. 663–664 forcontains,insert, anddeletewith the Access-Lemma companion bounds forminimumandsuccessor).executes_amortized_real_le,executes_rotations_le_realHistoryCost_of_empty: real-valued telescoping amortized bounds across arbitrary adaptiveOrderedSet.specclients (single-tree dictionary analogue of [ST85b], Section 3, Theorem 6, p. 665).executes_total_le,executes_total_le_of_empty,run_correct,run_correct_empty: discreteStateTransitionSpec.totalCostrotation bounds and full semantic + amortized correctness forrun.
References #
- [ST85b] Daniel Dominic Sleator and Robert Endre Tarjan, Self-Adjusting Binary Search Trees, Journal of the ACM 32(3), Section 3, pp. 660–665, 1985.
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.
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 #
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.
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.
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.
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).
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).
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 #
Execute an adaptive OrderedSet client concretely on a splay tree, recording exact rotation
counts per operation.
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.