Documentation

CslibFrontier.DataStructure.SplayTree.BSTModel

Pointer-and-Rotation BST Access Model, Touched Subtrees, and Dynamic Optimality #

This module formalizes the standard pointer-and-rotation binary search tree (BST) access model of [ST85b] and [Wil89] directly over Mathlib's BinaryTree α, reusing Cslib.DataStructure.SearchTree.Ordered, Cslib.DataStructure.SearchTree.elements, and the zipper structures Frame, plug, and Focus from Cslib.DataStructure.SplayTree.Basic.

Execution models and cost measures #

  1. Atomic pointer-and-rotation model (BSTStep, BSTAccess, BSTExecution): To serve a successful access x ∈ elements t starting from an ordered tree t, a pointer starts at the root (Focus.ofTree? t) and executes a finite sequence of unit-cost atomic steps: moving to a left child (moveLeft), right child (moveRight), or parent (moveUp), or rotating the focused node with its parent (rotateUp). The pointer must visit x during the step sequence (x ∈ touched). Two natural cost measures are recorded:

    • stepCost: steps.length + 1 per access (one unit for starting at the root plus one per atomic pointer move or rotation).
    • touchedCost: touched.card per access (number of distinct nodes visited by the pointer). Every execution satisfies xs.length ≤ e.touchedCost ≤ e.stepCost.
  2. Touched-subtree model (UntouchedPreserved, TouchedStep, TouchedExecution): Abstracting the atomic pointer trace, each access chooses an upward-closed connected top subtree τ (containing the root and the accessed key x) in both t and t', and restructures only within τ while leaving every untouched node's subtree (u ∉ τ) identical. Every BSTExecution induces a TouchedExecution of cost e.touchedCost (toTouchedExecution), so lower bounds proved for TouchedExecution (such as Wilber's first and second lower bounds) hold simultaneously for both touchedCost and stepCost of arbitrary pointer-and-rotation executions.

  3. Splay trees as an online BST strategy (OnlineBSTStrategy, splayStrategy): Bottom-up splaying (SplayTree.splay) is compiled explicitly to atomic BSTStep sequences (splaySteps), producing a computable stateless OnlineBSTStrategy whose final tree agrees identically with splaySeq and whose touchedCost and stepCost lie within explicit constant factors (1..2 and 1..4) of Sleator–Tarjan's rotation-plus-one access cost splaySeqCost.

  4. Dynamic Optimality Conjecture (IsDynamicallyOptimal, DynamicOptimalityConjecture): States Sleator and Tarjan's conjectural dynamic optimality of splay trees against all offline pointer-and-rotation executions up to a universal constant factor and O(|t₀|) additive term.

Main definitions #

Main results #

References #

Subtrees, Ancestry, Upper Sets, and Order-Convex topNode #

The subtree of t rooted at key x (found by BST search), or .nil if x is not found.

theorem Cslib.DataStructure.SplayTree.subtreeAt_node_of_lt {α : Type u_1} [LinearOrder α] {x a : α} (h : x < a) (l r : BinaryTree α) :
theorem Cslib.DataStructure.SplayTree.subtreeAt_node_of_gt {α : Type u_1} [LinearOrder α] {x a : α} (h : a < x) (l r : BinaryTree α) :

Every key in subtreeAt x t belongs to elements t.

subtreeAt x t preserves the strict BST invariant Ordered.

In an Ordered tree, subtreeAt x t is nonempty at root x iff x ∈ elements t.

In an Ordered tree, subtreeAt x t = .nil iff x ∉ elements t.

In an Ordered tree, x is the root of subtreeAt x t whenever x ∈ elements t.

def Cslib.DataStructure.SplayTree.Ancestor {α : Type u_1} [LinearOrder α] (t : BinaryTree α) (u v : α) :

Ancestry relation in a BST: u is an ancestor of v in t iff v lies in subtreeAt u t. Note that this requires u ∈ elements t (otherwise subtreeAt u t = .nil).

theorem Cslib.DataStructure.SplayTree.subtreeAt_subtreeAt {α : Type u_1} [LinearOrder α] {t : BinaryTree α} (ht : SearchTree.Ordered t) {u v : α} (h : Ancestor t u v) :

If v lies in subtreeAt u t, then searching for v inside subtreeAt u t yields the same subtree as searching in t.

theorem Cslib.DataStructure.SplayTree.Ancestor.trans {α : Type u_1} [LinearOrder α] {t : BinaryTree α} (ht : SearchTree.Ordered t) {u v w : α} (huv : Ancestor t u v) (hvw : Ancestor t v w) :
Ancestor t u w
theorem Cslib.DataStructure.SplayTree.Ancestor.antisymm {α : Type u_1} [LinearOrder α] {t : BinaryTree α} (ht : SearchTree.Ordered t) {u v : α} (huv : Ancestor t u v) (hvu : Ancestor t v u) :
u = v
theorem Cslib.DataStructure.SplayTree.ancestor_total_of_common_desc {α : Type u_1} [LinearOrder α] {t : BinaryTree α} (ht : SearchTree.Ordered t) {u v z : α} (hu : Ancestor t u z) (hv : Ancestor t v z) :
Ancestor t u v ∨ Ancestor t v u

Two ancestors of a common node in an Ordered BST are comparable in the ancestor order.

A subset τ of keys is upward-closed in t if every ancestor of a key in τ is also in τ.

A subset A is order-convex in a key set s if every key of s lying strictly between two keys of A also belongs to A.

Every subtree of an Ordered BST has an order-convex element set inside elements t.

Highest (closest-to-root) node of t whose key lies in A. On an Ordered tree and an order-convex nonempty subset A ⊆ elements t, this is the unique lowest common ancestor of A.

theorem Cslib.DataStructure.SplayTree.topNode_eq_some_of_orderConvex {α : Type u_1} [LinearOrder α] {t : BinaryTree α} (ht : SearchTree.Ordered t) {A : Finset α} (hsub : A ⊆ SearchTree.elements t) (hne : A.Nonempty) (hconv : OrderConvex (SearchTree.elements t) A) :
∃ v ∈ A, topNode A t = some v ∧ ∀ y ∈ A, Ancestor t v y

In an Ordered BST, any nonempty order-convex subset A ⊆ elements t has a unique highest node v ∈ A (topNode A t = some v), which is an ancestor of every key in A.

theorem Cslib.DataStructure.SplayTree.topNode_eq_some_of_ancestor {α : Type u_1} [LinearOrder α] {t : BinaryTree α} (ht : SearchTree.Ordered t) {A : Finset α} {v : α} (hvA : v ∈ A) (hanc : ∀ y ∈ A, Ancestor t v y) :
topNode A t = some v

Uniqueness characterization of topNode: if v ∈ A is an ancestor of every key of A in an Ordered BST t, then topNode A t = some v.

Zipper-Context Keys and Subtree Invariance under plug #

Atomic Pointer-and-Rotation BST Model and Touched-Subtree Model #

Atomic unit-cost steps of a pointer-and-rotation BST machine ([ST85b], [Wil89]): move the pointer to its left child, right child, or parent, or rotate the focused node with its parent (keeping the pointer on the rotated node).

  • moveLeft : BSTStep

    Move the pointer to the left child of the focused node.

  • moveRight : BSTStep

    Move the pointer to the right child of the focused node.

  • moveUp : BSTStep

    Move the pointer to the parent of the focused node.

  • rotateUp : BSTStep

    Rotate the edge between the focused node and its parent, keeping the pointer on the focused node.

Instances For

    Initialize a focus at the root of a non-empty BinaryTree α.

    Apply a single atomic BSTStep to a Focus α, returning none if the step is inapplicable (e.g., moving into an empty child or moving/rotating above the root).

    def Cslib.DataStructure.SplayTree.runSteps {α : Type u_1} [DecidableEq α] (f : Focus α) (touched : Finset α) :

    Execute a sequence of atomic BSTSteps from (f, touched), recording every key visited by the pointer in touched.

    @[simp]
    theorem Cslib.DataStructure.SplayTree.runSteps_nil {α : Type u_1} [DecidableEq α] (f : Focus α) (touched : Finset α) :
    runSteps f touched [] = some (f, touched)
    theorem Cslib.DataStructure.SplayTree.runSteps_append {α : Type u_1} [DecidableEq α] {f f₁ f₂ : Focus α} {τ τ₁ τ₂ : Finset α} {s₁ s₂ : List BSTStep} (h₁ : runSteps f τ s₁ = some (f₁, τ₁)) (h₂ : runSteps f₁ τ₁ s₂ = some (f₂, τ₂)) :
    runSteps f τ (s₁ ++ s₂) = some (f₂, τ₂)
    theorem Cslib.DataStructure.SplayTree.runSteps_subset_and_card {α : Type u_1} [DecidableEq α] {f f' : Focus α} {τ τ' : Finset α} {steps : List BSTStep} (h : runSteps f τ steps = some (f', τ')) :
    τ ⊆ τ' ∧ τ'.card ≤ τ.card + steps.length

    The touched set only grows during runSteps, by at most one node per atomic step.

    Touched-subtree preservation invariant between an initial BST t and a restructured BST t' with touched key set τ: the key set is unchanged, τ is upward-closed in both t and t', and every untouched key (u ∉ τ) retains its exact subtree.

    theorem Cslib.DataStructure.SplayTree.runSteps_ofTree?_spec {α : Type u_1} [LinearOrder α] {t : BinaryTree α} (ht : SearchTree.Ordered t) {f₀ f' : Focus α} {τ : Finset α} {steps : List BSTStep} (hstart : Focus.ofTree? t = some f₀) (hrun : runSteps f₀ {f₀.key} steps = some (f', τ)) :

    Every sequence of atomic BSTSteps starting from the root of an Ordered tree t preserves Ordered and satisfies UntouchedPreserved t f'.toTree τ.

    structure Cslib.DataStructure.SplayTree.BSTAccess {α : Type u_1} [LinearOrder α] (t : BinaryTree α) (x : α) (t' : BinaryTree α) :
    Type u_1

    A valid single-access transition from t to t' serving key x in the atomic pointer-and-rotation BST model: starting at the root, executing steps visits x (x ∈ touched) and produces t'.

    def Cslib.DataStructure.SplayTree.BSTAccess.stepCost {α : Type u_1} [LinearOrder α] {t t' : BinaryTree α} {x : α} (a : BSTAccess t x t') :

    Atomic step cost of a single access: 1 for initializing the pointer at the root plus one per atomic move or rotation.

    def Cslib.DataStructure.SplayTree.BSTAccess.touchedCost {α : Type u_1} [LinearOrder α] {t t' : BinaryTree α} {x : α} (a : BSTAccess t x t') :

    Touched-node cost of a single access: the number of distinct nodes visited by the pointer.

    inductive Cslib.DataStructure.SplayTree.BSTExecution {α : Type u_1} [LinearOrder α] :
    BinaryTree α → List α → Type u_1

    A valid pointer-and-rotation BST execution serving an access sequence xs from initial tree t.

    Total atomic step cost across a BSTExecution.

    Total touched-node cost across a BSTExecution.

    structure Cslib.DataStructure.SplayTree.TouchedStep {α : Type u_1} [LinearOrder α] (t : BinaryTree α) (x : α) (t' : BinaryTree α) :
    Type u_1

    Single step in the touched-subtree BST model: an upward-closed set touched containing x is restructured arbitrarily while leaving all untouched subtrees identical.

    Sequence of touched-subtree steps serving xs from t.

    Total touched-node cost of a TouchedExecution.

    The BST before step i (and the final tree at i = xs.length) along a TouchedExecution.

    The touched key set τ_i at step i of a TouchedExecution.

    Every atomic BSTAccess induces a TouchedStep with the exact same touched node set.

    Online BST Strategies, Splay-Tree Compilation, and Dynamic Optimality #

    An online BST strategy maintaining internal memory Mem and serving each request x with an atomic BSTAccess depending only on the current tree, internal memory, and x.

    def Cslib.DataStructure.SplayTree.OnlineBSTStrategy.executeFrom {α : Type u_1} [LinearOrder α] (S : OnlineBSTStrategy α) {t : BinaryTree α} (m : S.Mem) :
    SearchTree.Ordered t → S.Inv t m → (xs : List α) → (∀ x ∈ xs, x ∈ SearchTree.elements t) → BSTExecution t xs

    Run an OnlineBSTStrategy from a given internal state on an access sequence xs.

    def Cslib.DataStructure.SplayTree.OnlineBSTStrategy.execute {α : Type u_1} [LinearOrder α] (S : OnlineBSTStrategy α) {t₀ : BinaryTree α} (ht₀ : SearchTree.Ordered t₀) {xs : List α} (hxs : ∀ x ∈ xs, x ∈ SearchTree.elements t₀) :
    BSTExecution t₀ xs

    Execute an OnlineBSTStrategy from an ordered initial tree t₀.

    Downward pointer moves following the search path of focusSearch x t [].

    Upward pointer moves and rotations implementing bottom-up splayUp (zig, zig-zig, and zig-zag) in the atomic BSTStep machine.

    Full atomic BSTStep sequence implementing SplayTree.splay x t.

    Compile a single bottom-up SplayTree.splay x t on a present key x ∈ elements t into an explicit atomic BSTAccess t x (splay x t).tree.

    splayAccess executes the exact atomic step sequence produced by splaySteps.

    Stateless computable online BST strategy implementing Sleator and Tarjan's bottom-up splay trees ([ST85b]).

    theorem Cslib.DataStructure.SplayTree.splayStrategy_finalTree {α : Type u_1} [LinearOrder α] {t₀ : BinaryTree α} (ht₀ : SearchTree.Ordered t₀) {xs : List α} (hxs : ∀ x ∈ xs, x ∈ SearchTree.elements t₀) :
    (splayStrategy.execute ht₀ hxs).finalTree = (splaySeq xs t₀).1

    Executing splayStrategy on xs produces the exact final tree of SplayTree.splaySeq.

    splayStrategy's touched-node cost is lower-bounded by Sleator–Tarjan's rotation-plus-one access cost splaySeqCost.

    theorem Cslib.DataStructure.SplayTree.splayStrategy_touchedCost_le {α : Type u_1} [LinearOrder α] {t₀ : BinaryTree α} (ht₀ : SearchTree.Ordered t₀) {xs : List α} (hxs : ∀ x ∈ xs, x ∈ SearchTree.elements t₀) :

    splayStrategy's touched-node cost is at most 2 * splaySeqCost.

    theorem Cslib.DataStructure.SplayTree.splaySeqCost_le_splayStrategy_stepCost {α : Type u_1} [LinearOrder α] {t₀ : BinaryTree α} (ht₀ : SearchTree.Ordered t₀) {xs : List α} (hxs : ∀ x ∈ xs, x ∈ SearchTree.elements t₀) :

    splayStrategy's atomic step cost is lower-bounded by splaySeqCost.

    theorem Cslib.DataStructure.SplayTree.splayStrategy_stepCost_le {α : Type u_1} [LinearOrder α] {t₀ : BinaryTree α} (ht₀ : SearchTree.Ordered t₀) {xs : List α} (hxs : ∀ x ∈ xs, x ∈ SearchTree.elements t₀) :

    splayStrategy's atomic step cost is at most 4 * splaySeqCost.

    Dynamic Optimality Conjecture #

    An online BST strategy family is dynamically optimal if there exists a universal constant c such that on every linearly ordered key type α, every ordered initial BST t₀, every successful access sequence xs of keys in t₀, and every valid (possibly offline) pointer-and-rotation execution e serving xs from any ordered initial tree t₀' storing the same key set, the strategy's step cost is at most c * (e.stepCost + (elements t₀).card).

    Dynamic Optimality Conjecture for splay trees ([ST85b]): bottom-up splay trees (splayStrategy) are dynamically optimal among all pointer-and-rotation BST executions. By splaySeqCost_le_splayStrategy_stepCost and splayStrategy_stepCost_le, this is equivalent up to a factor of 4 to comparing Sleator–Tarjan's rotation-plus-one access cost splaySeqCost xs t₀ against e.stepCost + (elements t₀).card.