Binary Search Trees in Lean Check a result Declarations Bibliography Lean source

Chapter 14Adaptive bounds

Dynamic optimality is a comparison with an unknown optimum. The classical results about splay trees instead compare an algorithm with explicit functions of the request sequence. Each function measures one kind of locality: recency (working set), frequency (static optimality) and spatial proximity to a fixed key (static finger). This chapter defines these functions and the corresponding properties of an Algorithm. It also proves the implications between them, which hold for every algorithm. Whether splaying has these properties is the subject of Section 24.2.

The chapter has two main results. The first is that the working-set bound is the strongest of the three: an algorithm that has it also has the other two, and therefore competes with every fixed BST. The second is the classical entropy lower bound for fixed trees, which makes “static optimality” mean what its name says. The introduction to Part II maps these benchmarks together with those of the later chapters, the relations between them, and the algorithms that meet them.

The working-set bound implies static optimality and the static finger bound

Let AA be any online BST algorithm. Suppose there are constants C,DC, D such that, from every initial tree on nn keys and for every sequence XX of mm valid requests, AA performs at most C∑j(1+⌊log⁡2wj⌋)+D n(1+⌊log⁡2n⌋)C \sum_j \bigl(1 + \lfloor\log_2 w_j\rfloor\bigr) + D\,n(1 + \lfloor\log_2 n\rfloor) rotations, where the working-set number wjw_j is one plus the number of distinct keys requested since the previous request for xjx_j, or nn if xjx_j has not been requested before (the working-set bound). Then, for new constants C′,D′C', D', the total cost of AA (rotations plus mm) is at most

  • C′(m+∑xqxlog⁡2(m/qx))+D′ n(1+⌊log⁡2n⌋)C' \bigl(m + \sum_x q_x \log_2(m/q_x)\bigr) + D'\,n(1 + \lfloor\log_2 n\rfloor), where qxq_x is the number of requests for xx (static optimality);
  • C′∑j(1+log⁡2(∣xj−f∣+1))+D′ n(1+⌊log⁡2n⌋)C' \sum_j \bigl(1 + \log_2(|x_j - f| + 1)\bigr) + D'\,n(1 + \lfloor\log_2 n\rfloor), for every finger ff at once (static finger);
  • C′⋅costu(X)+D′ n(1+⌊log⁡2n⌋)C' \cdot \mathrm{cost}_u(X) + D'\,n(1 + \lfloor\log_2 n\rfloor), where costu(X)\mathrm{cost}_u(X) is the cost of serving XX in any fixed BST uu on the same keys, one unit per request plus the depth of the requested key.

In Lean, the three bounds are the properties HasWorkingSetBound, HasStaticOptimalityBound and HasStaticFingerBound of an Algorithm, and the fixed-tree cost is StaticTree.cost. Nothing about any specific algorithm is involved.

The Lean statements
theoremBounds.workingSet_implies_staticOptimalitypropext · Classical.choice · Quot.soundBounds/Implications/Abstract.lean:14
theorem workingSet_implies_staticOptimality (a : Algorithm) (h : HasWorkingSetBound a) :    HasStaticOptimalityBound a := …  h.staticOptimality a
theoremBounds.staticOptimality_implies_staticFingerpropext · Classical.choice · Quot.soundBounds/Implications/Abstract.lean:18
theorem staticOptimality_implies_staticFinger (a : Algorithm) (h : HasStaticOptimalityBound a) :    HasStaticFingerBound a := …  h.staticFinger a
theoremBounds.workingSet_implies_staticFingerpropext · Classical.choice · Quot.soundBounds/Implications/Abstract.lean:22
theorem workingSet_implies_staticFinger (a : Algorithm) (h : HasWorkingSetBound a) :    HasStaticFingerBound a := …  h.staticFinger a
theoremStaticTree.Results.workingSet_competespropext · Classical.choice · Quot.soundBounds/StaticTree/Abstract.lean:22
/-- Every algorithm with a working-set bound competes with every fixed BST onthe same ordered key universe, up to uniform constants and initialization. -/theorem workingSet_competes (a : Algorithm) (workingSet : HasWorkingSetBound a) :    ∃ C D : Nat, ∀ t u xs, t.size = u.size → (∀ x ∈ xs, x < t.size) →      (a.totalCost t xs : ℝ) ≤ (C : ℝ) * StaticTree.cost u xs +        (D : ℝ) * t.size * (1 + (t.size.log2 : ℝ)) := …  (workingSet.staticOptimality a).fixedTree a
18 definitions in 5 chapters · reading list
Main result · Mehlhorn 1975
Every fixed BST pays at least a third of the entropy bound

For every binary search tree tt on nn keys and every sequence XX of mm requests to its keys, let qxq_x be the number of requests for xx. Serving XX in tt without restructuring, at one unit per request plus the depth of the requested key, costs at least a third of the entropy bound:

m+∑xqxlog⁡2mqx  ≤  3 (m+∑jdeptht(xj)).m + \sum_x q_x \log_2 \frac{m}{q_x} \;\le\; 3\,\Bigl(m + \sum_j \mathrm{depth}_t(x_j)\Bigr).

In Lean, the left side is Bounds.staticCost and the right side is StaticTree.cost.

The Lean statement
theoremStaticTree.Results.entropy_lower_boundpropext · Classical.choice · Quot.soundBounds/StaticTree/Abstract.lean:13
theorem entropy_lower_bound (t : Tree) (xs : List Nat) (valid : ∀ x ∈ xs, x < t.size) :    Bounds.staticCost t.size xs ≤ 3 * (StaticTree.cost t xs : ℝ) := …  entropy_le_cost t xs valid
5 definitions in 2 chapters · reading list

14.1 Conventions

All logarithms are base 2. The sequence functions use Nat.log2 (the floor of log⁡2\log_2, with Nat.log2 0 = 0) where the original definitions are natural-number valued, and Real.logb 2 (with log⁡0=0\log 0 = 0) where they are real-valued. Every property has the same shape:

∃ C,D∈N. ∀t,xs valid.cost(a,t,xs)  ≤  C⋅f(xs)  +  D⋅n (1+⌊log⁡2n⌋),\exists\, C, D \in \mathbb N.\ \forall t, xs \text{ valid}.\quad \mathrm{cost}(a, t, xs) \;\le\; C \cdot f(xs) \;+\; D \cdot n\,(1 + \lfloor \log_2 n \rfloor),

where nn = t.size. The second term is a startup allowance: it pays for an arbitrary initial tree, and it plays the role of the O(nlog⁡n)O(n \log n) term in Sleator and Tarjan’s theorems.

14.2 The working-set bound

The working-set number of a request measures how recently its key was last requested, counted in distinct keys:

defworkingSetNumberpropext · Quot.soundBounds/WorkingSet/Abstract.lean:9
/-- First access has working-set number `n`; otherwise count distinct keyssince its last access, plus one. -/def workingSetNumber (n : Nat) (past : List Nat) (x : Nat) : Nat :=  if x ∈ past then 1 + (past.reverse.takeWhile (· != x)).eraseDups.length else n

For a request x with history past (oldest first):

  • If x has not been requested before, its number is n, the number of keys in the tree.
  • Otherwise, past.reverse.takeWhile (· != x) is the list of requests made since the most recent request for x, most recent first. eraseDups removes repetitions, so its length is the number of distinct keys requested since then. The working-set number is one more than that.

An immediately repeated request therefore has number 11, and in general the number is at most nn.

Reading the Lean
  • != is the Boolean inequality given by BEq Nat, which agrees with ≠ on natural numbers. takeWhile stops at the most recent occurrence of x and does not include it.
  • The +1+1 counts x itself. With it the number equals Sleator and Tarjan’s t(j)+1t(j) + 1, where t(j)t(j) is the number of distinct keys accessed since the last access of xjx_j. Iacono’s convention, the size of the working set including xjx_j, is the same quantity.
  • First accesses. Sleator and Tarjan let t(j)t(j) be the number of distinct keys accessed before jj when xjx_j is new; the library uses nn instead. This charges at most 1+⌊log⁡2n⌋1 + \lfloor\log_2 n\rfloor for each of at most nn first accesses, a total that the startup allowance D n(1+⌊log⁡2n⌋)D\,n(1+\lfloor\log_2 n\rfloor) already absorbs. The two conventions give the same property.
defworkingSetSumpropext · Quot.soundBounds/WorkingSet/Abstract.lean:14
def workingSetSum (n : Nat) (past : List Nat) : List Nat → Nat  | [] => 0  | x :: xs => 1 + (workingSetNumber n past x).log2 +      workingSetSum n (past ++ [x]) xs

The working-set sum charges each request 1+⌊log⁡2w⌋1 + \lfloor \log_2 w \rfloor, where ww is its working-set number. The 1+1+ is needed: an immediate repeat has log⁡21=0\log_2 1 = 0 but still costs a comparison. The floor is harmless, since 1+⌊log⁡2w⌋1 + \lfloor\log_2 w\rfloor and log⁡2(w+1)\log_2(w+1) are within a factor of 22 of each other for w≥1w \ge 1. Like runFrom, the recursion appends each request to the history after charging it.

The property:

defHasWorkingSetBoundpropext · Quot.soundBounds/WorkingSet/Abstract.lean:19
/-- Total working-set bound, with an initial-tree allowance. -/def HasWorkingSetBound (a : Algorithm) : Prop :=  ∃ C D : Nat, ∀ t xs, (∀ x ∈ xs, x < t.size) →    a.cost t xs ≤ C * workingSetSum t.size [] xs +      D * t.size * (1 + t.size.log2)

Read: there are natural constants C,DC, D such that for every initial tree and every valid sequence, the rotation count of the algorithm is at most C⋅C \cdot{}workingSetSum plus the startup allowance. This is Theorem 4 of Sleator and Tarjan (1985), the working-set theorem, as a property of an arbitrary algorithm, with the constants left existential.

Reading the Lean
  • The cost is a.cost (rotations), not a.totalCost. Since every request contributes at least 11 to workingSetSum, the two versions are equivalent; see the next theorem.
  • The constants are uniform over all initial trees, sizes and sequences. The working-set sum is computed with the size of the initial tree, t.size, which never changes.
  • Validity ∀ x ∈ xs, x < t.size is a hypothesis, so the property says nothing about invalid requests.

Adding the request count does not change which algorithms have the property.

Theorem (rotations or total cost). An algorithm has the working-set bound if and only if there are constants C,DC, D such that its total cost, rotations plus one per request, is at most C⋅workingSetSum+D n(1+⌊log⁡2n⌋)C \cdot \mathtt{workingSetSum} + D\,n(1 + \lfloor\log_2 n\rfloor) for every initial tree and every valid sequence.

theoremworkingSet_totalCost_iffpropext · Quot.soundBounds/Normalization/Abstract.lean:16
/-- Charging one unit per request does not change which algorithms satisfy ourworking-set bound. Constants are uniform over initial trees and valid sequences. -/theorem workingSet_totalCost_iff (a : Algorithm) :    HasWorkingSetBound a ↔      ∃ C D : Nat, ∀ t xs, (∀ x ∈ xs, x < t.size) →        a.totalCost t xs ≤ C * workingSetSum t.size [] xs +          D * t.size * (1 + t.size.log2) :=  BoundsBridge.normalized_workingSet_iff a
Statement reads only Bounds/Normalization/Abstract.lean and its imports

The proof uses xs.length ≤ workingSetSum n past xs, which holds because every term of the sum is at least 11. Going from rotations to total cost changes CC to C+1C+1; the other direction keeps the constants. In contrast with dynamic optimality, the normalization is harmless here because the benchmark already charges every request.

14.3 Static optimality

Let qxq_x be the number of times key xx occurs in a sequence of length mm. A static tree in which xx has depth about log⁡2(m/qx)\log_2(m/q_x) serves the sequence in time proportional to mm plus the empirical entropy of the access frequencies. The library defines this benchmark directly:

defBounds.staticCostpropext · Classical.choice · Quot.soundBounds/Abstract.lean:13
/-- Frequency entropy, with one unit per request. Zero-frequency terms vanish. -/noncomputable def Bounds.staticCost (n : Nat) (xs : List Nat) : ℝ :=  (xs.length : ℝ) + ∑ x ∈ Finset.range n,    (xs.count x : ℝ) * Real.logb 2 ((xs.length : ℝ) / xs.count x)
staticCost(n,xs)  =  m+∑x<nqxlog⁡2mqx.\mathtt{staticCost}(n, xs) \;=\; m + \sum_{x < n} q_x \log_2 \frac{m}{q_x}.
Reading the Lean

Zero frequencies. For a key that never occurs, xs.count x = 0, so the term is 0 * Real.logb 2 (m / 0). In Lean, m / 0 = 0 and Real.logb 2 0 = 0, and the term is 00 either way. This matches the usual convention 0log⁡(1/0)=00 \log(1/0) = 0, as the docstring says. The sum ranges over all nn keys, but only keys that occur contribute.

defHasStaticOptimalityBoundpropext · Classical.choice · Quot.soundBounds/Abstract.lean:18
/-- Static optimality in the frequency-entropy formulation, with the same`n(1 + floor(log₂ n))` initialization allowance as the working-set property. -/def HasStaticOptimalityBound (a : Algorithm) : Prop :=  ∃ C D : Nat, ∀ t xs, (∀ x ∈ xs, x < t.size) →    (a.totalCost t xs : ℝ) ≤ (C : ℝ) * Bounds.staticCost t.size xs +      (D : ℝ) * t.size * (1 + (t.size.log2 : ℝ))

The property bounds the total cost T=m+RT = m + R, with the same startup allowance as the working-set property. The statement is in R\mathbb R; the natural-number constants and t.size.log2 are cast.

Compared with the paper

Sleator and Tarjan’s Theorem 2 assumes that every key is accessed at least once, and has no startup term: T=O(m+∑xqxlog⁡(m/qx))T = O(m + \sum_x q_x \log(m/q_x)). The property above allows unused keys, and pays for them, and for the initial tree, with the startup allowance. For splay, Section 24.2 proves both this property and the paper’s form without startup, the latter under the explicit hypothesis that every key occurs.

14.4 Static finger

Fix a key ff, the finger. Requests close to ff in rank should be cheap:

defBounds.fingerCostpropext · Classical.choice · Quot.soundBounds/Abstract.lean:25
/-- Static finger cost for the fixed inorder rank `f`, including one unit per request. -/noncomputable def Bounds.fingerCost (f : Nat) (xs : List Nat) : ℝ :=  (xs.map fun x : Nat => 1 + Real.logb 2 (Nat.dist x f + 1 : Nat)).sum
defHasStaticFingerBoundpropext · Classical.choice · Quot.soundBounds/Abstract.lean:29
/-- The constant factors are uniform over the initial tree, request sequence,and choice of fixed finger. -/def HasStaticFingerBound (a : Algorithm) : Prop :=  ∃ C D : Nat, ∀ t xs f, f < t.size → (∀ x ∈ xs, x < t.size) →    (a.totalCost t xs : ℝ) ≤ (C : ℝ) * Bounds.fingerCost f xs +      (D : ℝ) * t.size * (1 + (t.size.log2 : ℝ))

Each request costs 1+log⁡2(∣x−f∣+1)1 + \log_2(|x - f| + 1), where Nat.dist x f is ∣x−f∣|x - f| on natural numbers. Distance is rank distance, the number of keys between xx and ff, not a difference of key values. With the implicit-key model this is the only meaningful choice, and it is the distance used in Sleator and Tarjan’s Theorem 3, where items are numbered 1,…,n1, \dots, n in symmetric order.

Reading the Lean

The quantifier order matters. The constants C,DC, D come first, and then the bound holds for every finger f < t.size. A single algorithm with fixed constants competes with every finger simultaneously, as in the paper. The algorithm itself never learns f.

14.5 Dynamic finger

The dynamic finger bound replaces the fixed finger by the previous request: a request is cheap when it is close in rank to the one before it. The library defines the benchmark as a sequence function:

defBounds.dynamicFingerCostFrompropext · Classical.choice · Quot.soundBounds/DynamicFinger/Definitions/Abstract.lean:10
/-- Dynamic-finger charges for consecutive requests, including a unit perrequest. The predecessor of the first request is supplied explicitly. -/noncomputable def dynamicFingerCostFrom : Nat → List Nat → ℝ  | _, [] => 0  | p, x :: xs => 1 + Real.logb 2 (Nat.dist p x + 1 : Nat) + dynamicFingerCostFrom x xs

dynamicFingerCostFrom p xs charges each request xix_i the amount 1+log⁡2(∣xi−xi−1∣+1)1 + \log_2(|x_i - x_{i-1}| + 1), with a real base-two logarithm, where x0=px_0 = p is an explicitly supplied predecessor of the first request. The unit makes an immediately repeated request cost 11, as in every other benchmark of this chapter. The bound was proved for splay trees by Cole and coauthors (Cole et al. 2000; Cole 2000); that proof is not in the library. The library proves it instead for GreedyASS (Section 25.3), and for the optimal lazy-finger tree (Chapter 16), where it is the unit-weight case of the weighted dynamic finger bound of Chapter 15.

The property has the same shape as the others, with the start key quantified like the finger:

defHasDynamicFingerBoundpropext · Classical.choice · Quot.soundBounds/DynamicFinger/Definitions/Abstract.lean:20
/-- The dynamic-finger property. The bound holds for every start key `p`; thechoice of `p` changes the sum by at most `1 + log₂ n`, which the startup termabsorbs, so fixing `p` instead would give an equivalent property. -/def HasDynamicFingerBound (a : Algorithm) : Prop :=  ∃ C D : Nat, ∀ t xs p, p < t.size → (∀ x ∈ xs, x < t.size) →    (a.totalCost t xs : ℝ) ≤ (C : ℝ) * Bounds.dynamicFingerCostFrom p xs +      (D : ℝ) * t.size * (1 + (t.size.log2 : ℝ))

The bound must hold for every start key p < t.size. Changing p changes the sum by at most 1+log⁡2n1 + \log_2 n, which the startup term absorbs, so fixing p would give an equivalent property. No algorithm in the library is proved to have this property as stated: the GreedyASS theorem of Section 25.3 gives one algorithm for each number of keys nn, while the property asks for one algorithm for all nn.

Dynamic finger is stronger than static finger. A step from xx to yy is at most the step from xx to ff and back from ff to yy, because ∣x−y∣+1≤(∣x−f∣+1)(∣y−f∣+1)|x - y| + 1 \le (|x - f| + 1)(|y - f| + 1). Summing, each request’s finger term is counted at most twice:

Theorem (dynamic finger and static finger). For every finger ff, start key pp and sequence, the dynamic finger sum is at most twice the static finger sum plus log⁡2(∣p−f∣+1)\log_2(|p - f| + 1). Starting at the finger, the extra term vanishes.

theoremBounds.dynamicFinger_le_fingerCostpropext · Classical.choice · Quot.soundBounds/DynamicFinger/Abstract.lean:13
/-- For every finger `f`, start key `p` and sequence, the dynamic-finger sum isat most twice the static-finger sum, plus the start key's own finger term. -/theorem dynamicFinger_le_fingerCost (f p : Nat) (xs : List Nat) :    dynamicFingerCostFrom p xs ≤      2 * fingerCost f xs + Real.logb 2 (Nat.dist p f + 1 : Nat) :=  dynamicFingerCostFrom_le_fingerCost f p xs
theoremBounds.dynamicFinger_le_fingerCost_selfpropext · Classical.choice · Quot.soundBounds/DynamicFinger/Abstract.lean:20
/-- Starting at the finger, the start term vanishes. -/theorem dynamicFinger_le_fingerCost_self (f : Nat) (xs : List Nat) :    dynamicFingerCostFrom f xs ≤ 2 * fingerCost f xs :=  dynamicFingerCostFrom_finger_le f xs

14.6 The logarithmic baseline

Every bound above is at most what a balanced tree pays, about log⁡2n\log_2 n per request whatever the sequence. The library names this baseline, and the corresponding property:

defBounds.logCostpropext · Classical.choice · Quot.soundBounds/Logarithmic/Abstract.lean:13
/-- One unit plus `log₂ n` per request. -/noncomputable def Bounds.logCost (n : Nat) (xs : List Nat) : ℝ :=  (xs.length : ℝ) * (1 + Real.logb 2 n)
defHasLogarithmicBoundpropext · Classical.choice · Quot.soundBounds/Logarithmic/Abstract.lean:17
/-- Logarithmic amortized cost per request, with the usual startup allowance. -/def HasLogarithmicBound (a : Algorithm) : Prop :=  ∃ C D : Nat, ∀ t xs, (∀ x ∈ xs, x < t.size) →    (a.totalCost t xs : ℝ) ≤ (C : ℝ) * Bounds.logCost t.size xs +      (D : ℝ) * t.size * (1 + (t.size.log2 : ℝ))

logCost n xs is m(1+log⁡2n)m(1 + \log_2 n). An algorithm has the logarithmic bound when it pays O(1+log⁡n)O(1 + \log n) amortized per request, after the usual startup allowance. The balanced static tree of Chapter 19 meets the baseline on every sequence, and every static tree pays it on some sequence. The dynamic trees of Part III have logarithmic height under updates; a search-cost statement for them against logCost is not formalized.

14.7 The unified charge

Sleator and Tarjan’s Theorem 5 combines the three localities: each access is charged the smallest of its three logarithmic costs. The library defines this benchmark in Bounds/Unified/Abstract.lean, next to the others, so that the bound is a property of a sequence and not of splaying:

defBounds.unifiedChargepropext · Classical.choice · Quot.soundBounds/Unified/Abstract.lean:9
/-- One request's best frequency, static-finger, or working-set logarithm. -/noncomputable def unifiedCharge (n f : Nat) (original past : List Nat) (x : Nat) : ℝ :=  1 + min (Real.logb 2 (workingSetNumber n past x))    (min (Real.logb 2 ((original.length : ℝ) / original.count x))      (Real.logb 2 (Nat.dist x f + 1 : Nat)))
defBounds.unifiedSumpropext · Classical.choice · Quot.soundBounds/Unified/Abstract.lean:15
/-- Frequencies use the entire sequence, while recency uses only the prefix. -/noncomputable def unifiedSum (n f : Nat) (original : List Nat) : List Nat → List Nat → ℝ  | _, [] => 0  | past, x :: xs => unifiedCharge n f original past x + unifiedSum n f original (past ++ [x]) xs

For a request xx with history past, unifiedCharge is $1+min⁡(log⁡2workingSetNumber,  log⁡2mqx,  log⁡2(∣x−f∣+1)).1 + \min\Bigl(\log_2 \texttt{workingSetNumber},\; \log_2\frac{m}{q_x},\; \log_2(|x - f| + 1)\Bigr).$ Frequencies come from the whole sequence original, so mm is its length and qxq_x the number of occurrences of xx in it. Recency comes from the prefix past, which unifiedSum extends one request at a time while keeping original fixed. This mixes offline information (frequencies) with online information (recency), as the paper’s bound does. An algorithm measured against it still sees only the past; the frequencies are used by the analysis alone.

Reading the Lean
  • These logarithms are real: Real.logb 2 (workingSetNumber …) is not the floor used in workingSetSum.
  • If xx did not occur in original, then qx=0q_x = 0, m / 0 = 0 in Lean, and logb 2 0 = 0, so the minimum could collapse to 00. Theorems apply it as unifiedSum n f xs [] xs, so original is the very sequence being summed over, and every xx occurs in it.
  • Since the minimum is taken per access, unifiedSum is at most each of the three earlier sums up to constants, and can be much smaller than all of them: a sequence that alternates between a hot spot near ff and a small working set far away is cheap for one charge on one half and for another on the other half.

Splaying satisfies this bound (Results.unified, Section 24.2). The library defines no Has…Bound property for it, because no implication involving it is proved.

14.8 Comparison with a fixed tree

Static optimality is usually explained as “as good as the best fixed BST for the access frequencies”. The library makes the comparison literal. First, a fixed tree’s search cost:

defStaticTree.costno axiomsBounds/StaticTree/Definitions/Abstract.lean:9
/-- Search a fixed tree from its root each time, charging one per request andone per downward pointer move (the rank's `Tree.depth`). No rotations orreturn-to-root moves occur. -/def cost (t : Tree) (xs : List Nat) : Nat :=  (xs.map fun x => t.depth x + 1).sum

cost charges each request one unit plus its depth, the number of edges from the root to the requested rank (Tree.depth, defined in Chapter 1).

This benchmark is exactly the cost of an actual pointer-machine execution that never restructures.

Theorem (a fixed tree is a pointer execution). For every tree t and every valid sequence xs, there is a pointer-machine execution that serves xs from t, ends in t, and costs exactly StaticTree.cost t xs.

theoremStaticTree.Results.pointer_executionpropext · Quot.soundBounds/StaticTree/Abstract.lean:17
/-- This benchmark is exactly realized by searches that never rotate the tree. -/theorem pointer_execution (t : Tree) (xs : List Nat) (valid : ∀ x ∈ xs, x < t.size) :    Pointer.Execution t xs t (StaticTree.cost t xs) :=  execution t xs valid

Each request is served by walking down depth t x edges, visiting x, and stopping. That is Pointer.Serves t t x (depth t x + 1) in the pointer model, with the unit request charge. The next search starts again at the root, and returning there is free in that model. The final tree is t itself.

Entropy is a lower bound on the cost of every fixed tree, up to a factor of 33.

Theorem (entropy lower bound). For every tree t and every valid sequence xs, staticCost(n,xs)≤3⋅cost(t,xs)\mathtt{staticCost}(n, xs) \le 3 \cdot \mathtt{cost}(t, xs), where nn is the size of t.

theoremStaticTree.Results.entropy_lower_boundpropext · Classical.choice · Quot.soundBounds/StaticTree/Abstract.lean:13
theorem entropy_lower_bound (t : Tree) (xs : List Nat) (valid : ∀ x ∈ xs, x < t.size) :    Bounds.staticCost t.size xs ≤ 3 * (StaticTree.cost t xs : ℝ) :=  entropy_le_cost t xs valid

The proof is the prefix-code argument. Give key xx the weight 4−(depth(x)+1)4^{-(\mathrm{depth}(x)+1)}, two bits for each comparison step, including the final one. These weights sum to at most 11 in every shape. Gibbs’ inequality bounds the entropy by ∑xqxlog⁡2(1/wx)=2⋅cost\sum_x q_x \log_2(1/w_x) = 2 \cdot \mathtt{cost}, so staticCost≤m+2 cost≤3 cost\mathtt{staticCost} \le m + 2\,\mathtt{cost} \le 3\,\mathtt{cost}.

The bound is tight up to that factor: some fixed tree achieves the entropy bound itself.

Theorem (entropy upper bound). For every nn and every sequence xs of requests less than nn, there is a tree t on nn keys with cost(t,xs)≤staticCost(n,xs)\mathtt{cost}(t, xs) \le \mathtt{staticCost}(n, xs).

theoremStaticTree.Results.entropy_upper_boundpropext · Classical.choice · Quot.soundBounds/StaticTree/Abstract.lean:30
/-- Conversely, some fixed tree achieves normalized frequency entropy withconstant one. Unobserved keys are retained and empty sequences are included. -/theorem entropy_upper_bound (n : Nat) (xs : List Nat) (valid : ∀ x ∈ xs, x < n) :    ∃ t : Tree, t.size = n ∧ (StaticTree.cost t xs : ℝ) ≤ Bounds.staticCost n xs :=  entropy_tree n xs valid

The constant is one, and there is no additive term. The tree has all nn keys, including those never requested, and the statement includes n=0n = 0 and the empty sequence, where both sides are 00. Combined with the lower bound, the best fixed tree for xs costs between 13 staticCost\tfrac13\,\mathtt{staticCost} and staticCost\mathtt{staticCost}, which is the classical statement that optimal static trees cost Θ(m+∑xqxlog⁡2(m/qx))\Theta(m + \sum_x q_x \log_2(m/q_x)). The optimal static tree itself, and this two-sided bound for it, are in Chapter 19 (Results.optCost_entropy).

Consequently, a working-set algorithm competes with every fixed BST.

Theorem (working set beats every fixed tree). If an algorithm has the working-set bound, then there are constants C,DC, D such that, for every initial tree t, every tree u of the same size and every valid sequence xs, the algorithm’s total cost from t is at most C⋅StaticTree.cost(u,xs)+D n(1+⌊log⁡2n⌋)C \cdot \mathtt{StaticTree.cost}(u, xs) + D\,n(1 + \lfloor\log_2 n\rfloor).

theoremStaticTree.Results.workingSet_competespropext · Classical.choice · Quot.soundBounds/StaticTree/Abstract.lean:22
/-- Every algorithm with a working-set bound competes with every fixed BST onthe same ordered key universe, up to uniform constants and initialization. -/theorem workingSet_competes (a : Algorithm) (workingSet : HasWorkingSetBound a) :    ∃ C D : Nat, ∀ t u xs, t.size = u.size → (∀ x ∈ xs, x < t.size) →      (a.totalCost t xs : ℝ) ≤ (C : ℝ) * StaticTree.cost u xs +        (D : ℝ) * t.size * (1 + (t.size.log2 : ℝ)) :=  (workingSet.staticOptimality a).fixedTree a

Here t is the algorithm’s initial tree and u is any comparison tree of the same size; they need not be equal. The constants are uniform over t, u and xs. The proof goes through static optimality (below) and entropy_lower_bound, which turns CC into 3C3C.

14.9 Implications between the bounds

The following implications hold for every algorithm and use nothing about splaying. They are inequalities between sequence functions, lifted to properties.

Theorem (implications). For every algorithm, the working-set bound implies static optimality, and static optimality implies the static finger bound. Hence the working-set bound implies the static finger bound. The dynamic finger bound also implies the static finger bound, and the static finger bound implies the logarithmic bound.

theoremBounds.workingSet_implies_staticOptimalitypropext · Classical.choice · Quot.soundBounds/Implications/Abstract.lean:14
theorem workingSet_implies_staticOptimality (a : Algorithm) (h : HasWorkingSetBound a) :    HasStaticOptimalityBound a :=  h.staticOptimality a
theoremBounds.staticOptimality_implies_staticFingerpropext · Classical.choice · Quot.soundBounds/Implications/Abstract.lean:18
theorem staticOptimality_implies_staticFinger (a : Algorithm) (h : HasStaticOptimalityBound a) :    HasStaticFingerBound a :=  h.staticFinger a
theoremBounds.workingSet_implies_staticFingerpropext · Classical.choice · Quot.soundBounds/Implications/Abstract.lean:22
theorem workingSet_implies_staticFinger (a : Algorithm) (h : HasWorkingSetBound a) :    HasStaticFingerBound a :=  h.staticFinger a
theoremBounds.dynamicFinger_implies_staticFingerpropext · Classical.choice · Quot.soundBounds/Implications/Abstract.lean:26
theorem dynamicFinger_implies_staticFinger (a : Algorithm) (h : HasDynamicFingerBound a) :    HasStaticFingerBound a :=  h.staticFinger a
theoremBounds.fingerCost_le_logpropext · Classical.choice · Quot.soundBounds/Implications/Abstract.lean:30
/-- Every distance between two of `n` keys is less than `n`, so thestatic-finger sum is at most the logarithmic baseline. -/theorem fingerCost_le_log (n f : Nat) (xs : List Nat) (hf : f < n) (valid : ∀ x ∈ xs, x < n) :    fingerCost f xs ≤ logCost n xs :=  fingerCost_le_logCost n f xs hf valid
theoremBounds.staticFinger_implies_logarithmicpropext · Classical.choice · Quot.soundBounds/Implications/Abstract.lean:36
theorem staticFinger_implies_logarithmic (a : Algorithm) (h : HasStaticFingerBound a) :    HasLogarithmicBound a :=  h.logarithmic a
  • Working set ⇒ static optimality. The key inequality is $workingSetSum(n,[],xs)  ≤  staticCost(n,xs)+n (1+⌊log⁡2n⌋).\mathtt{workingSetSum}(n, [], xs) \;\le\; \mathtt{staticCost}(n, xs) + n\,(1 + \lfloor\log_2 n\rfloor).$ The proof groups the charges by key. Between consecutive accesses to xx, the working-set number is at most the time elapsed (the gap). The gaps of key xx sum to at most mm, and a finite Jensen inequality bounds ∑log⁡2(gap)\sum \log_2(\text{gap}) by qxlog⁡2(m/qx)q_x \log_2(m/q_x). First accesses are charged to the startup term. The constants become (C+1,C+D)(C + 1, C + D).
  • Static optimality ⇒ static finger. Apply the weighted entropy inequality with weights 1/(∣x−f∣+1)21/(|x-f|+1)^2, which sum to at most 44 on both sides of ff together. This gives staticCost≤3 fingerCost(f)\mathtt{staticCost} \le 3\,\mathtt{fingerCost}(f) for every ff, and the constants become (3C,D)(3C, D).
  • Working set ⇒ static finger is the composition.
  • Dynamic finger ⇒ static finger follows from the inequality of the previous section, taking the start key equal to the finger. The constants become (2C,D)(2C, D).
  • Static finger ⇒ logarithmic. Two keys among nn are less than nn apart, so each static-finger term is at most 1+log⁡2n1 + \log_2 n, and the constants do not change.
Why these are separate theorems

In Sleator and Tarjan’s paper, Theorems 2 and 3 are proved directly from the access lemma, with suitably chosen weights. The library instead proves the working-set theorem for splay once and derives static optimality and static finger through these algorithm-independent implications. The splay-specific sharper forms, without startup when every key occurs, are proved separately in Section 24.2.

14.10 Transfer to the pointer machine

Working-set guarantees proved in the rotation model also hold for the causal pointer simulation of Section 7.6.

Theorem (working set on the pointer machine). If an algorithm has the working-set bound in the rotation model, then there are constants C,DC, D such that its online pointer-machine implementation costs at most C⋅workingSetSum+D n(1+⌊log⁡2n⌋)C \cdot \mathtt{workingSetSum} + D\,n(1 + \lfloor\log_2 n\rfloor), for every initial tree and every valid sequence.

theoremPointer.Lazy.Online.Results.workingSetpropext · Classical.choice · Quot.soundModels/Pointer/Lazy/Online/Abstract.lean:33
/-- Working-set guarantees proved on shapes transfer to the chosen causalpointer implementation, with uniform constants. -/theorem workingSet (a : RotationBST.Algorithm) (bound : HasWorkingSetBound a) :    ∃ C D : Nat, ∀ t xs, (∀ x ∈ xs, x < t.size) →      (run a [] (initial t) xs).2 ≤ C * workingSetSum t.size [] xs +        D * t.size * (1 + t.size.log2) :=  workingSet_transfer a bound

The left-hand side is the cost of the online pointer implementation of a, including requests and every pointer instruction. Postponed rotations are not executed at the end, and they need not be: the pointer execution serves every request without them. The proof combines simulation (pointer cost≤32R+17m\text{pointer cost} \le 32R + 17m) with the hypothesis on RR and with m≤workingSetSumm \le \mathtt{workingSetSum}, giving constants (32C+17,32D)(32C + 17, 32D). The constants hold for every algorithm, independently of nn. For splaying specifically, Section 24.1 gives a more direct pointer implementation with better constants.

14.11 What is not formalized

Two well-known results are absent. The dynamic finger theorem for splay trees (Cole et al. 2000; Cole 2000) is not in the library: its proof is long. The benchmark itself is defined above, and other structures are proved to meet it. The unified bound (Iacono 2001; Bădoiu et al. 2007) replaces the fixed finger by the distance to recently accessed keys; it is open whether splay trees satisfy it. Both are mentioned here only to place the formalized bounds. One further result appears as a dashed arrow in the map of the bounds and is listed in the library’s roadmap: the theorem of Chmel et al. (2026), a 2026 preprint, that splay trees are O(log⁡log⁡n⋅log⁡2log⁡log⁡n)O(\log\log n \cdot \log^2\log\log n)-competitive. The lower-bound side of Part II is Chapter 17.