Documentation

CslibFrontier.DataStructure.SplayTree.AccessLemma

Sleator–Tarjan's Access Lemma, Static Optimality, and Update Bounds #

This module formalizes the potential-method amortized analysis of self-adjusting splay trees due to Sleator and Tarjan ([ST85b], Sections 2–3, pp. 656–665).

Given any positive weight assignment w : α → ℝ (∀ x, 0 < w x — a purely analytical parameter not stored in the tree), each subtree t has size subtreeWeight w t, binary-logarithm rank nodeRank w t = Real.logb 2 (subtreeWeight w t) ([ST85b], Section 2, p. 658, Footnote 1), and tree potential potential w t summing nodeRank w over all internal subtrees of t. Following Sleator and Tarjan's rotation-count BST access model ([ST85b], Section 2, p. 658), the rotation-plus-one access cost splayCost res = max res.rotations 1 charges one unit when no rotations occur (for the root access) and the exact edge-rotation count otherwise; this is a structural BST rotation cost, not a Word-RAM or Turing-machine running time.

Main definitions #

Main results #

References #

Subtree Weights, Node Ranks, and Rank-Sum Potential #

noncomputable def Cslib.DataStructure.SplayTree.subtreeWeight {α : Type u_1} (w : α → ℝ) :

Sum of item weights w over all internal nodes of a BinaryTree α (s(x) in [ST85b], Section 2, p. 658).

@[simp]
theorem Cslib.DataStructure.SplayTree.subtreeWeight_node {α : Type u_1} (w : α → ℝ) (a : α) (l r : BinaryTree α) :
theorem Cslib.DataStructure.SplayTree.subtreeWeight_nonneg {α : Type u_1} {w : α → ℝ} (hw : ∀ (x : α), 0 ≤ w x) (t : BinaryTree α) :
theorem Cslib.DataStructure.SplayTree.subtreeWeight_pos {α : Type u_1} {w : α → ℝ} (hw : ∀ (x : α), 0 < w x) {t : BinaryTree α} (ht : t ≠ BinaryTree.nil) :
theorem Cslib.DataStructure.SplayTree.subtreeWeight_plug_congr {α : Type u_1} (w : α → ℝ) {t₁ t₂ : BinaryTree α} (ctx : List (Frame α)) (h : subtreeWeight w t₁ = subtreeWeight w t₂) :
subtreeWeight w (plug t₁ ctx) = subtreeWeight w (plug t₂ ctx)
theorem Cslib.DataStructure.SplayTree.le_subtreeWeight_plug {α : Type u_1} {w : α → ℝ} (hw : ∀ (x : α), 0 ≤ w x) (t : BinaryTree α) (ctx : List (Frame α)) :
theorem Cslib.DataStructure.SplayTree.subtreeWeight_splayUp {α : Type u_1} (w : α → ℝ) (x : α) (l r : BinaryTree α) (ctx : List (Frame α)) :
noncomputable def Cslib.DataStructure.SplayTree.nodeRank {α : Type u_1} (w : α → ℝ) (t : BinaryTree α) :

Base-2 logarithmic rank r(x) = log₂ s(x) of a subtree ([ST85b], Section 2, p. 658, Footnote 1).

theorem Cslib.DataStructure.SplayTree.nodeRank_congr {α : Type u_1} {w : α → ℝ} {t₁ t₂ : BinaryTree α} (h : subtreeWeight w t₁ = subtreeWeight w t₂) :
nodeRank w t₁ = nodeRank w t₂
theorem Cslib.DataStructure.SplayTree.nodeRank_mono {α : Type u_1} {w : α → ℝ} {t₁ t₂ : BinaryTree α} (hpos : 0 < subtreeWeight w t₁) (hle : subtreeWeight w t₁ ≤ subtreeWeight w t₂) :
nodeRank w t₁ ≤ nodeRank w t₂
noncomputable def Cslib.DataStructure.SplayTree.potential {α : Type u_1} (w : α → ℝ) :

Sleator–Tarjan potential Φ(t) = ∑_x r(x) summing nodeRank w over all internal subtrees of t ([ST85b], Section 2, p. 658).

@[simp]
theorem Cslib.DataStructure.SplayTree.potential_node {α : Type u_1} (w : α → ℝ) (a : α) (l r : BinaryTree α) :
theorem Cslib.DataStructure.SplayTree.potential_plug_sub_eq {α : Type u_1} (w : α → ℝ) {t₁ t₂ : BinaryTree α} (ctx : List (Frame α)) (hsw : subtreeWeight w t₁ = subtreeWeight w t₂) :
potential w (plug t₁ ctx) - potential w (plug t₂ ctx) = potential w t₁ - potential w t₂

Local restructurings that preserve subtreeWeight leave the ranks of all proper ancestors in ctx unchanged, so the global potential difference equals the local potential difference.

Sleator–Tarjan rotation-plus-one access cost for a single splay ([ST85b], Section 2, p. 658): the number of edge rotations performed, or 1 when zero rotations are performed. Counts BST rotations (plus 1 at 0), not machine instructions.

Elementary AM-GM Binary-Logarithm Inequality and Bottom-Up Access Lemma #

theorem Cslib.DataStructure.SplayTree.splayUp_amortized_le {α : Type u_1} {w : α → ℝ} (hw : ∀ (y : α), 0 < w y) (x : α) (l r : BinaryTree α) (ctx : List (Frame α)) :
↑(splayUp x l r ctx).rotations + potential w (splayUp x l r ctx).tree - potential w (plug (BinaryTree.node x l r) ctx) ≤ 3 * (nodeRank w (plug (BinaryTree.node x l r) ctx) - nodeRank w (BinaryTree.node x l r)) + if ctx.isEmpty = true then 0 else 1

Bottom-Up Access Lemma ([ST85b], Section 2, Lemma 1, p. 658): for any positive weights w, bottom-up splaying of (x, l, r) through ctx has amortized rotation cost at most 3 (r(root) - r(u)) when ctx = [] and at most 3 (r(root) - r(u)) + 1 when ctx ≠ [].

theorem Cslib.DataStructure.SplayTree.splayUp_splayCost_amortized_le {α : Type u_1} {w : α → ℝ} (hw : ∀ (y : α), 0 < w y) (x : α) (l r : BinaryTree α) (ctx : List (Frame α)) :
↑(splayCost (splayUp x l r ctx)) + potential w (splayUp x l r ctx).tree - potential w (plug (BinaryTree.node x l r) ctx) ≤ 3 * (nodeRank w (plug (BinaryTree.node x l r) ctx) - nodeRank w (BinaryTree.node x l r)) + 1

Sleator–Tarjan rotation-plus-one (splayCost) Access Lemma on splayUp ([ST85b], Section 2, Lemma 1, p. 658).

noncomputable def Cslib.DataStructure.SplayTree.unitWeight {α : Type u_1} :
α → ℝ

Unit weight w(x) = 1 ([ST85b], Section 2, Theorem 1, p. 659; Section 3, Theorem 6, p. 665).

noncomputable def Cslib.DataStructure.SplayTree.unitPotential {α : Type u_1} (t : BinaryTree α) :

Rank-sum potential under unit weights (unitWeight).

Access Lemma on Search Trees, Balance Theorem, and Static Optimality #

@[simp]
theorem Cslib.DataStructure.SplayTree.subtreeWeight_splay {α : Type u_1} [LinearOrder α] (w : α → ℝ) (x : α) (t : BinaryTree α) :

On an Ordered search tree, subtreeWeight w t equals the Finset sum of w over elements t.

theorem Cslib.DataStructure.SplayTree.le_subtreeWeight_of_mem {α : Type u_1} [LinearOrder α] {w : α → ℝ} (hw : ∀ (y : α), 0 ≤ w y) {t : BinaryTree α} (ht : SearchTree.Ordered t) {x : α} (hx : x ∈ SearchTree.elements t) :
theorem Cslib.DataStructure.SplayTree.splay_splayCost_amortized_le {α : Type u_1} [LinearOrder α] {w : α → ℝ} (hw : ∀ (y : α), 0 < w y) {x : α} {t : BinaryTree α} {f : Focus α} (hf : focusSearch x t [] = some f) :
↑(splayCost (splay x t)) + potential w (splay x t).tree - potential w t ≤ 3 * (nodeRank w t - nodeRank w f.sub) + 1

Access Lemma (focused-subtree form) ([ST85b], Section 2, Lemma 1, p. 658): if focusSearch x t [] = some f, splaying t at x has amortized rotation-plus-one cost (splayCost) at most 3 (r(t) - r(f.sub)) + 1.

theorem Cslib.DataStructure.SplayTree.exists_mem_splay_splayCost_amortized_le {α : Type u_1} [LinearOrder α] {w : α → ℝ} (hw : ∀ (y : α), 0 < w y) {t : BinaryTree α} (ht : SearchTree.Ordered t) (hne : t ≠ BinaryTree.nil) (x : α) :
∃ u ∈ SearchTree.elements t, (x ∈ SearchTree.elements t → u = x) ∧ ↑(splayCost (splay x t)) + potential w (splay x t).tree - potential w t ≤ 3 * Real.logb 2 (subtreeWeight w t / w u) + 1

Access Lemma (general key-weight form) ([ST85b], Section 2, Lemma 1, p. 658; Section 3, p. 664): on any non-empty Ordered tree t and any query x (present or absent), there exists a key u ∈ elements t (equal to x if x ∈ elements t, and otherwise unspecified) such that the amortized rotation-plus-one cost (splayCost) of splay x t is at most 3 log₂(W / w(u)) + 1.

theorem Cslib.DataStructure.SplayTree.splay_splayCost_amortized_le_of_mem {α : Type u_1} [LinearOrder α] {w : α → ℝ} (hw : ∀ (y : α), 0 < w y) {t : BinaryTree α} (ht : SearchTree.Ordered t) {x : α} (hx : x ∈ SearchTree.elements t) :
↑(splayCost (splay x t)) + potential w (splay x t).tree - potential w t ≤ 3 * Real.logb 2 (subtreeWeight w t / w x) + 1

Access Lemma for present keys ([ST85b], Section 2, Lemma 1, p. 658): when x ∈ elements t in an Ordered tree t, splaying t at x has amortized rotation-plus-one cost (splayCost) at most 3 log₂(W / w(x)) + 1.

theorem Cslib.DataStructure.SplayTree.splay_rotations_amortized_le_of_mem {α : Type u_1} [LinearOrder α] {w : α → ℝ} (hw : ∀ (y : α), 0 < w y) {t : BinaryTree α} (ht : SearchTree.Ordered t) {x : α} (hx : x ∈ SearchTree.elements t) :
↑(splay x t).rotations + potential w (splay x t).tree - potential w t ≤ 3 * Real.logb 2 (subtreeWeight w t / w x) + 1

Rotation-count corollary of splay_splayCost_amortized_le_of_mem.

theorem Cslib.DataStructure.SplayTree.splayMin_rotations_amortized_le {α : Type u_1} [LinearOrder α] {w : α → ℝ} (hw : ∀ (y : α), 0 < w y) {t : BinaryTree α} (hne : t ≠ BinaryTree.nil) :

Amortized rotation bound for splayMin on a non-empty tree.

theorem Cslib.DataStructure.SplayTree.splayMax_rotations_amortized_le {α : Type u_1} [LinearOrder α] {w : α → ℝ} (hw : ∀ (y : α), 0 < w y) {t : BinaryTree α} (hne : t ≠ BinaryTree.nil) :

Amortized rotation bound for splayMax on a non-empty tree.

Fixed-Weight Potential Drop, Balance Theorem, and Static Optimality #

theorem Cslib.DataStructure.SplayTree.sum_logb_weight_le_potential {α : Type u_1} [LinearOrder α] {w : α → ℝ} (hw : ∀ (y : α), 0 < w y) {t : BinaryTree α} (ht : SearchTree.Ordered t) :
∑ y ∈ SearchTree.elements t, Real.logb 2 (w y) ≤ potential w t

Lower potential bound: each node's rank is at least the logarithm of its own key's weight, so ∑ y ∈ t, log₂ w(y) ≤ Φ(t) ([ST85b], Section 2, p. 659).

theorem Cslib.DataStructure.SplayTree.potential_le_sum_logb_of_le {α : Type u_1} [LinearOrder α] {w : α → ℝ} (hw : ∀ (y : α), 0 < w y) {t : BinaryTree α} (ht : SearchTree.Ordered t) {W : ℝ} (hW : subtreeWeight w t ≤ W) :

Upper potential bound: if the total weight is at most W, every rank is at most log₂ W, so Φ(t) ≤ ∑ y ∈ t, log₂ W ([ST85b], Section 2, p. 659).

theorem Cslib.DataStructure.SplayTree.potential_sub_potential_le_sum_logb {α : Type u_1} [LinearOrder α] {w : α → ℝ} (hw : ∀ (y : α), 0 < w y) {t₁ t₂ : BinaryTree α} (ht₁ : SearchTree.Ordered t₁) (ht₂ : SearchTree.Ordered t₂) (helem : SearchTree.elements t₁ = SearchTree.elements t₂) {W : ℝ} (hWpos : 0 < W) (hW : subtreeWeight w t₁ ≤ W) :
potential w t₁ - potential w t₂ ≤ ∑ y ∈ SearchTree.elements t₁, Real.logb 2 (W / w y)

Fixed-Weight Potential Drop Bound ([ST85b], Section 2, p. 659): between any two Ordered trees t₁ and t₂ representing the same set of keys, if 0 < W and subtreeWeight w t₁ ≤ W, the net potential decrease satisfies potential w t₁ - potential w t₂ ≤ ∑ y ∈ elements t₁, log₂(W / w(y)).

Execute a sequence of splay operations xs starting from t, returning the final tree and the list of per-operation SplayResults.

Total Sleator–Tarjan rotation-plus-one access cost (∑ max rotations 1) across an access sequence xs ([ST85b], Section 2, p. 658).

Total edge rotations across an access sequence xs.

@[simp]
theorem Cslib.DataStructure.SplayTree.splaySeqCost_cons {α : Type u_1} [LinearOrder α] (x : α) (xs : List α) (t : BinaryTree α) :
@[simp]
theorem Cslib.DataStructure.SplayTree.subtreeWeight_splaySeq {α : Type u_1} [LinearOrder α] (w : α → ℝ) (xs : List α) (t : BinaryTree α) :

Balance Theorem ([ST85b], Section 2, Theorem 1, p. 659): for any non-empty Ordered tree t of n = (elements t).card elements and any sequence xs of m = xs.length accesses (whether successful or unsuccessful), the total rotation-plus-one access cost satisfies splaySeqCost xs t ≤ (3m + n) log₂ n + m.

theorem Cslib.DataStructure.SplayTree.splaySeqCost_le_static_optimality {α : Type u_1} [LinearOrder α] (xs : List α) {t : BinaryTree α} (ht : SearchTree.Ordered t) (hxs : ∀ x ∈ xs, x ∈ SearchTree.elements t) (hq : ∀ y ∈ SearchTree.elements t, 1 ≤ List.count y xs) :
↑(splaySeqCost xs t) ≤ ↑xs.length + 4 * ∑ y ∈ SearchTree.elements t, ↑(List.count y xs) * Real.logb 2 (↑xs.length / ↑(List.count y xs))

Static Optimality Theorem ([ST85b], Section 2, Theorem 2, p. 659): for any Ordered splay tree t and any access sequence xs of present keys (∀ x ∈ xs, x ∈ t) in which every key in t is accessed at least once (∀ y ∈ elements t, 1 ≤ xs.count y), the total rotation-plus-one access cost is bounded by the empirical Shannon access-frequency sum: splaySeqCost xs t ≤ m + 4 ∑ y ∈ elements t, q(y) log₂(m / q(y)), where m = xs.length and q(y) = xs.count y.

theorem Cslib.DataStructure.SplayTree.splaySeqCost_le_fixed_weights {α : Type u_1} [LinearOrder α] {w : α → ℝ} (hw : ∀ (y : α), 0 < w y) (xs : List α) {t : BinaryTree α} (ht : SearchTree.Ordered t) (hxs : ∀ x ∈ xs, x ∈ SearchTree.elements t) {W : ℝ} (hW : subtreeWeight w t ≤ W) :
↑(splaySeqCost xs t) ≤ (List.map (fun (x : α) => 3 * Real.logb 2 (W / w x) + 1) xs).sum + ∑ y ∈ SearchTree.elements t, Real.logb 2 (W / w y)

Fixed-weight access-sequence bound ([ST85b], Section 2, p. 659; the common argument behind Theorems 1–3): for any positive weights w with total tree weight at most W, a sequence xs of accesses to present keys has rotation-plus-one access cost at most the summed Access Lemma bounds plus the net potential drop, splaySeqCost xs t ≤ ∑ⱼ (3 log₂(W / w(xⱼ)) + 1) + ∑ y ∈ t, log₂(W / w(y)).

Weight Changes #

The weights in the Access Lemma are analysis parameters, so a proof may change them between operations at the cost of the resulting potential change ([ST85b], Section 3, Lemma 2, pp. 663–664: decreasing the weight of an item does not increase the potential). The Working Set Theorem ([ST85b], Section 2, Theorem 4, pp. 659–660) reassigns weights after each access while the accessed item is at the root; potential_reweight_root_le bounds that potential change.

theorem Cslib.DataStructure.SplayTree.subtreeWeight_le_subtreeWeight {α : Type u_1} [LinearOrder α] {w w' : α → ℝ} {t : BinaryTree α} (hle : ∀ y ∈ SearchTree.elements t, w' y ≤ w y) :

Pointwise smaller weights on the keys of t give a smaller total weight.

theorem Cslib.DataStructure.SplayTree.potential_le_potential_of_le {α : Type u_1} [LinearOrder α] {w w' : α → ℝ} (hw' : ∀ (y : α), 0 < w' y) {t : BinaryTree α} (hle : ∀ y ∈ SearchTree.elements t, w' y ≤ w y) :

Decreasing weights never increases the potential ([ST85b], Section 3, Lemma 2, pp. 663–664): if 0 < w' y ≤ w y for every key y of t, then Φ_{w'}(t) ≤ Φ_w(t).

theorem Cslib.DataStructure.SplayTree.potential_reweight_root_le {α : Type u_1} [LinearOrder α] {w w' : α → ℝ} (hw' : ∀ (y : α), 0 < w' y) {t : BinaryTree α} (ht : SearchTree.Ordered t) {x : α} (hroot : rootKey t = some x) (hW : subtreeWeight w' t = subtreeWeight w t) (hle : ∀ y ∈ SearchTree.elements t, y ≠ x → w' y ≤ w y) :

Root weight reassignment ([ST85b], Section 2, proof of Theorem 4, p. 660): if the key x is at the root of an Ordered tree t, and new positive weights w' keep the total weight of t unchanged while not increasing the weight of any other key of t, then the potential does not increase. The root's size is unchanged, and every other node's subtree avoids x, so its size can only decrease.

Unit-Weight Update Lemma Specialization and OrderedSet Companion Bounds #

Sleator and Tarjan's Update Lemma ([ST85b], Section 3, Lemma 2, pp. 663–664) states general positive-weight bounds for access, join, split, insert (assuming x ∉ t), and delete (assuming x ∈ t) under a multi-tree potential incorporating ∑_{i ∉ trees} log₂ w(i). At unit weights (unitWeight, where log₂ 1 = 0), that external-item potential term vanishes. Below we prove this unit-weight specialization for contains, split, join, and total set-idempotent insert and erase, together with companion unit-weight bounds for minimum and successor derived directly from the Access Lemma (Lemma 1, p. 658).

Unit-weight (w = 1) specialization of the access bound in [ST85b], Section 3, Lemma 2, pp. 663–664, for contains.

Companion unit-weight amortized rotation bound for minimum, derived from the Access Lemma ([ST85b], Section 2, Lemma 1, p. 658) via splayMin_rotations_amortized_le (minimum is not part of Section 3, Lemma 2).

Unit-weight (w = 1) specialization of the split bound in [ST85b], Section 3, Lemma 2, pp. 663–664.

theorem Cslib.DataStructure.SplayTree.join_unitPotential_amortized_le {α : Type u_1} [LinearOrder α] {t₁ t₂ : BinaryTree α} (ht₁ : SearchTree.Ordered t₁) (ht₂ : SearchTree.Ordered t₂) :
↑(join t₁ t₂).rotations + unitPotential (join t₁ t₂).tree - (unitPotential t₁ + unitPotential t₂) ≤ 3 * Real.logb 2 (↑(SearchTree.elements t₁).card + ↑(SearchTree.elements t₂).card + 1) + 1

Unit-weight (w = 1) specialization of the join bound in [ST85b], Section 3, Lemma 2, pp. 663–664.

Unit-weight (w = 1) specialization of the insert bound in [ST85b], Section 3, Lemma 2, pp. 663–664, extended from x ∉ elements t to total set-idempotent insert.

Unit-weight (w = 1) specialization of the delete bound in [ST85b], Section 3, Lemma 2, pp. 663–664, extended from x ∈ elements t to total set-idempotent erase.

Companion unit-weight amortized rotation bound for successor (splaying x, followed by splayMin on the right subtree when root ≤ x), derived from the Access Lemma ([ST85b], Section 2, Lemma 1, p. 658; successor is not part of Section 3, Lemma 2).