Documentation

CslibFrontier.DataStructure.AVLTree.Impl

Stateful ordered-set realization by height-balanced AVL trees #

This module realizes the shared Cslib.DataStructure.OrderedSet.spec α interface (contains, insert, delete, minimum, successor) using height-cached AVL trees in the same internal-node-visit cost model as Cslib.DataStructure.SearchTree, upgrading SearchTree.nodeVisitBound's worst-case linear s.card bound to the representation-independent logarithmic bound 2 * Nat.log 2 (s.card + 1).

Cost model and limitations #

As in Cslib.DataStructure.SearchTree, native operation traces count internal nodes visited along the downward search (and splitMin) path (0 on an empty tree, at most t.cachedHeight on any height-consistent tree), specified by SearchTree's path-visit functions on t.toBinaryTree. At each visited ancestor during insert or erase, balance performs non-recursive O(1) reads of off-path sibling/nephew cached heights and at most a single or double rotation; those constant-work rebalancing steps, toBinaryTree trace-specification projection, node allocations, order-comparison implementations, and pure client continuations are not charged as separate RAM or Turing-machine instructions.

Main definitions #

Main results #

References #

Execute a single OrderedSet.Op α on an AVL tree, returning the updated tree, answer, and internal-node-visit count.

@[reducible, inline]

Native execution relation for OrderedSet.spec α over AVLTree α.

Representation relation coupling a mathematical Finset α with a valid AVL tree t.

theorem Cslib.DataStructure.AVLTree.Rep.valid {α : Type} [LinearOrder α] {s : Finset α} {t : AVLTree α} (hr : Rep s t) :

Extract AVL validity from Rep.

theorem Cslib.DataStructure.AVLTree.Rep.elements_eq {α : Type} [LinearOrder α] {s : Finset α} {t : AVLTree α} (hr : Rep s t) :

Extract the key-set equality from Rep.

The empty AVL tree represents the empty Finset α.

Semantic realization of OrderedSet.spec α by AVLTree α.

@[simp]

impl.rep is definitionally Rep.

theorem Cslib.DataStructure.AVLTree.exec_visits_le_cachedHeight {α : Type} [LinearOrder α] {t t' : AVLTree α} {o : OrderedSet.Op α} {a : (OrderedSet.spec α).Result o} {n : ℕ} (hc : t.HeightConsistent) (he : t.Exec o a t' n) :

Every native AVL operation visits at most t.cachedHeight internal path nodes.

Representation-independent logarithmic node-visit budget (0 on ∅, 2 * log₂(|s| + 1) in general).

Logarithmic per-operation node-visit certificate for AVLTree.impl.

theorem Cslib.DataStructure.AVLTree.realized_client {α : Type} [LinearOrder α] {β : Type} (p : (OrderedSet.spec α).Client β) {s : Finset α} {t : AVLTree α} (hr : Rep s t) :
match (OrderedSet.spec α).runClient s p with | (value, finalState, history) => ∃ (t' : AVLTree α) (ns : List ℕ), Executes Exec p t value t' ns ∧ Rep finalState t' ∧ total id ns ≤ (OrderedSet.spec α).totalCost charge s history

Every adaptive OrderedSet client executes on AVLTree with finite-set correctness and additive 2 * Nat.log 2 (s.card + 1) node-visit bounds along its chosen history.

@[reducible, inline]
abbrev Cslib.DataStructure.AVLTree.run {α : Type} [LinearOrder α] {β : Type} (t : AVLTree α) (p : (OrderedSet.spec α).Client β) :

Execute an OrderedSet client concretely on an AVL tree, recording exact node visits per operation.

theorem Cslib.DataStructure.AVLTree.run_correct {α : Type} [LinearOrder α] {β : Type} (p : (OrderedSet.spec α).Client β) {s : Finset α} {t : AVLTree α} (hr : Rep s t) :
match (OrderedSet.spec α).runClient s p with | (value, finalState, history) => (t.run p).value = value ∧ Rep finalState (t.run p).state ∧ total id (t.run p).traces ≤ (OrderedSet.spec α).totalCost charge s history

Running the concrete AVL interpreter agrees with abstract Finset α semantics, preserves Rep, and satisfies the additive logarithmic node-visit bound.