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

Chapter 12How the models agree

Part I has now described binary search tree executions in five ways: rotation traces on shapes (Chapter 3), the same traces on labelled trees (Chapter 4), a single-pointer machine (Chapter 7), rooted regions rearranged at each request (Chapter 8), and arborally satisfied point sets (Chapter 9). The two models since then, many fingers and linked searches, serve particular results and are related to the others in their own chapters. This chapter proves that the geometric view measures the same thing as the other four. Demaine, Harmon, Iacono, Kane and Pătraşcu (2009) showed that the cheapest arborally satisfied superset of the request points has, up to constant factors, the cost of the best BST execution. Every BST question about a fixed sequence thereby becomes a question about points in the plane: no trees, no rotations, only rectangles.

Key result · Demaine et al. 2009
Offline BST cost and arborally satisfied sets agree up to constant factors

Fix nn keys and a sequence X=x0,…,xm−1X = x_0, \dots, x_{m-1} of requests. A geometric solution for XX is a finite set of points (key,time)(\text{key}, \text{time}), with keys below nn and times below mm, that contains every request point (xj,j)(x_j, j) and is arborally satisfied: any two of its points that share neither a key nor a time span a closed rectangle containing a third point of the set. Its cost is its number of points.

  1. Trees to points. If some tree on nn keys serves XX by root accesses with RR rotations in total, then XX has a geometric solution with at most 2R+m2R + m points.
  2. Points to trees. If XX has a geometric solution with ∣E∣|E| points, then from every initial tree on nn keys some sequence of root accesses serves XX with RR rotations, where m+R≤2n+99 ∣E∣m + R \le 2n + 99\,|E|.

So, with OPTASS\mathrm{OPT}_{\mathrm{ASS}} the size of a smallest geometric solution and OPT\mathrm{OPT} the least total cost m+Rm + R of an execution from a given initial tree,

12 OPTASS  ≤  OPT  ≤  99 OPTASS+2n.\tfrac12\,\mathrm{OPT}_{\mathrm{ASS}} \;\le\; \mathrm{OPT} \;\le\; 99\,\mathrm{OPT}_{\mathrm{ASS}} + 2n.

In Lean, a root-access execution is an Execution (with its rotation count) and a geometric solution is a Geometric.Offline n xs, whose points field is the point set.

The Lean statements
theoremGeometric.Equivalence.Results.rotations_to_asspropext · Classical.choice · Quot.soundModels/Equivalence/Abstract.lean:21
/-- A rotation execution has a geometric solution of comparable total cost. -/theorem rotations_to_ass {s t : Tree} {xs : List Nat} {r : Nat}    (execution : RotationBST.Execution s xs t r) :    ∃ E : Offline s.size xs, E.points.card ≤ 2 * r + xs.length := …  rotations_to_offline execution
theoremGeometric.Equivalence.Results.ass_to_rotationspropext · Classical.choice · Quot.soundModels/Equivalence/Abstract.lean:27
/-- Every ASS solution gives a rotation algorithm from any prescribed initialshape. The additive term pays for initialization; no final shape is prescribed. -/theorem ass_to_rotations (s : Tree) (xs : List Nat) (E : Offline s.size xs) :    ∃ t r, RotationBST.Execution s xs t r ∧      xs.length + r ≤ 2 * s.size + 99 * E.points.card := … offline_from_initial s xs E
13 definitions in 4 chapters · reading list

The chapter proves this in both directions, first exactly for the region model of Chapter 8, then with constants for the rotation model and the pointer machine. It ends with an online version, which turns an online geometric algorithm into an online BST algorithm. The introduction to Part I tabulates every correspondence of the part.

All six theorems are in Models/Equivalence/Abstract.lean, under Geometric.Equivalence.Results. Their statements use the geometric definitions of Chapter 9, the region model of Chapter 8, and the BST models of Chapter 3, Chapter 5 and Chapter 7.

12.1 What has to be compared

A geometric solution has no initial tree. Geometric.Offline n xs is an arborally satisfied set containing the request points (xj,j)(x_j, j), with every point on a key below nn and a time below mm = xs.length, and its cost is its number of points. A BST execution starts from a given tree of size nn. So each direction of the correspondence must decide what happens to the initial tree:

  • From a BST execution to points, the initial tree is simply forgotten.
  • From points to a BST execution, the conversion may choose a convenient initial tree, or it may be given one and pay to reach a convenient tree first. Rotations connect any two shapes of size nn in at most 2n2n steps (Geometry.reset), so the price of a prescribed initial tree is an additive 2n2n.

All conversions in this chapter except the last are offline: they read a whole execution or a whole point set. The last section turns an online geometric algorithm into an online BST algorithm.

12.2 Regions are points

The rooted-region model of Chapter 8 is the BST model of the 2009 paper. At each request the algorithm touches a connected region containing the root and the requested key, rearranges it, and pays one unit per touched node. Its executions and point sets are the same objects, with the same cost.

Theorem (regions are points). For every number of keys nn, sequence xs and number cost, there is an offline geometric solution with exactly cost points if and only if some initial tree of size nn has a RegionExecution serving xs with charge exactly cost.

theoremGeometric.Equivalence.Results.ass_iff_region_executionpropext · Classical.choice · Quot.soundModels/Equivalence/Abstract.lean:15
/-- Exact point-count equivalence with rooted-subtree BST executions.The initial tree is free; the conversion is offline. -/theorem ass_iff_region_execution (n : Nat) (xs : List Nat) (cost : Nat) :    (∃ E : Offline n xs, E.points.card = cost) ↔      ∃ s t, s.size = n ∧ RegionExecution s xs t cost := offline_iff_regions n xs cost

The equivalence is exact: one touched node per point, in both directions, with no constant.

  • From regions to points, record row jj as the keys touched by the jj-th step. Each row contains the request, since a RegionStep must visit it, and the rows together are arborally satisfied. The proof keeps the current tree a max-heap of last-touch times, and finds the third point of every rectangle as a common ancestor in that heap (Section 32.4).
  • From points to regions, build a treap whose priorities are next-touch times. Before each row, the keys of that row are exactly the top region of the treap, and rearranging that region alone restores the treap for the next time (Section 32.2). This is the offline step: priorities come from the future of the point set.
Reading the Lean
  • The initial tree s is existential on the right-hand side. The treap construction needs its own initial tree, the treap for the first touches. The final tree t is unconstrained.
  • Region charges count step.before.size, the nodes of the touched region and not the untouched subtrees hanging from it. Point counts and region charges therefore agree exactly.
  • cost is the same variable on both sides, so the equivalence also says that the minimum costs coincide. A minimum offline solution exists (Results.optimal_exists).

12.3 From trees to points

An execution in the rotation model of Chapter 3, or on the pointer machine of Chapter 7, yields a geometric solution that is at most a constant factor larger.

Theorem (rotations to points). If a root-access execution from a tree s serves the mm requests xs with RR rotations, then xs has an offline geometric solution on s.size keys with at most 2R+m2R + m points.

theoremGeometric.Equivalence.Results.rotations_to_asspropext · Classical.choice · Quot.soundModels/Equivalence/Abstract.lean:21
/-- A rotation execution has a geometric solution of comparable total cost. -/theorem rotations_to_ass {s t : Tree} {xs : List Nat} {r : Nat}    (execution : RotationBST.Execution s xs t r) :    ∃ E : Offline s.size xs, E.points.card ≤ 2 * r + xs.length :=  rotations_to_offline execution

The touched nodes of an access are not determined by its rotations: the rotation model allows rotations anywhere, and those far from the request touch nodes the access never needed. The proof reuses the lazy normal form of Chapter 7. The rotations of an access that are connected to the root in the rotation graph form an access on their own, and they rearrange a region of at most 2r+12r + 1 nodes, where rr is their number. That region is a region step. The other rotations are postponed to later accesses. Summing over the execution, the region charge plus twice the rotations still pending at the end is at most 2R+m2R + m, and Results.ass_iff_region_execution turns the region execution into points.

Theorem (pointers to points). If a pointer execution from s serves xs at total cost KK (requests plus instructions), then xs has an offline geometric solution with at most 6K6K points.

theoremGeometric.Equivalence.Results.pointers_to_asspropext · Classical.choice · Quot.soundModels/Equivalence/Abstract.lean:33
/-- The conventional pointer model also maps to geometry at constant cost. -/theorem pointers_to_ass {s t : Tree} {xs : List Nat} {cost : Nat}    (execution : Pointer.Execution s xs t cost) :    ∃ E : Offline s.size xs, E.points.card ≤ 6 * cost := pointer_to_offline execution

This composes two earlier bounds. Results.to_rotations turns the pointer execution into a rotation execution with m+R≤3Km + R \le 3K, and the previous theorem gives at most 2R+m≤2(m+R)≤6K2R + m \le 2(m + R) \le 6K points.

Lower bounds transfer

These two theorems carry geometric lower bounds back to the tree models. Wilber’s interleave bound (Chapter 17) is proved once for point sets, IB≤2 ∣E∣\mathrm{IB} \le 2\,|E|, and stated for rotation executions as IB≤2(2R+m)\mathrm{IB} \le 2(2R + m) and for pointer executions as IB≤2⋅6K\mathrm{IB} \le 2 \cdot 6K, through exactly these conversions.

12.4 From points to trees

Conversely, every geometric solution can be executed by a tree at constant cost per point. Here the initial tree matters, and the two theorems treat it differently.

Theorem (points to pointers). For every offline geometric solution EE of xs on nn keys, there is an initial tree of size nn from which a pointer execution serves xs at total cost at most 33 ∣E∣33\,|E|.

theoremGeometric.Equivalence.Results.ass_to_pointerspropext · Classical.choice · Quot.soundModels/Equivalence/Abstract.lean:38
/-- Without fixing the initial tree, the geometric solution has a pointerrealization with no additive initialization term. This construction is offline. -/theorem ass_to_pointers {n : Nat} {xs : List Nat} (E : Offline n xs) :    ∃ s t cost, s.size = n ∧ Pointer.Execution s xs t cost ∧ cost ≤ 33 * E.points.card :=  offline_to_pointer E

The construction chooses the initial tree. It runs the treap construction of Chapter 32 on the pointer machine. Each row is a region of kk nodes that must be rearranged, with the request visited along the way. The reshaping lemma Results.reshape transforms a region into any other with the same fringe in at most 16k16k instructions. Reshaping once to bring the request to the root, where the cursor is, and once more into the target arrangement costs 1+16k+16k≤33k1 + 16k + 16k \le 33k, including the request unit. Summed over the rows, this is 3333 per point.

Theorem (points to rotations). For every tree s with nn nodes and every offline geometric solution EE of the mm requests xs on nn keys, some root-access execution from s serves xs with RR rotations, where m+R≤2n+99 ∣E∣m + R \le 2n + 99\,|E|.

theoremGeometric.Equivalence.Results.ass_to_rotationspropext · Classical.choice · Quot.soundModels/Equivalence/Abstract.lean:27
/-- Every ASS solution gives a rotation algorithm from any prescribed initialshape. The additive term pays for initialization; no final shape is prescribed. -/theorem ass_to_rotations (s : Tree) (xs : List Nat) (E : Offline s.size xs) :    ∃ t r, RotationBST.Execution s xs t r ∧      xs.length + r ≤ 2 * s.size + 99 * E.points.card := offline_from_initial s xs E

The initial shape is prescribed here, not chosen. Two steps produce the constants. First, the pointer execution of ass_to_pointers becomes a rotation execution by Results.to_rotations, which multiplies by 33: 99=3⋅3399 = 3 \cdot 33. That execution starts from the treap chosen by the construction, so the theorem first rotates s into it, using at most 2n2n rotations (Geometry.reset), merged into the first access. The suffix of postponed rotations that to_rotations leaves at the end is dropped, since no final shape is prescribed.

Theorem (points to pointers, prescribed start). For every tree s with nn nodes and every offline geometric solution EE of xs on nn keys, some pointer execution from s serves xs at total cost at most 64n+3168 ∣E∣64n + 3168\,|E|.

theoremGeometric.Equivalence.Results.ass_to_pointers_frompropext · Classical.choice · Quot.soundModels/Equivalence/Abstract.lean:44
/-- The pointer machine from a prescribed initial tree: a linear initializationterm, as for rotations. No final shape is prescribed. -/theorem ass_to_pointers_from (s : Tree) (xs : List Nat) (E : Offline s.size xs) :    ∃ t cost, Pointer.Execution s xs t cost ∧ cost ≤ 64 * s.size + 3168 * E.points.card :=  pointer_from_initial s xs E

It composes the two conversions already shown: the rotation execution of ass_to_rotations from s, then the lazy pointer simulation of Results.to_pointer_lazy (Chapter 7). The lazy simulation costs at most 32R+17m≤32(m+R)≤32(2n+99 ∣E∣)32R + 17m \le 32(m + R) \le 32(2n + 99\,|E|). Its pending rotations at the end are dropped, since no final shape is prescribed.

Together, these theorems pin down the offline optimum. Writing OPTASS\mathrm{OPT}_{\mathrm{ASS}} for the size of a smallest geometric solution, every rotation execution from s satisfies OPTASS≤2R+m\mathrm{OPT}_{\mathrm{ASS}} \le 2R + m, and the best one satisfies m+R≤2n+99 OPTASSm + R \le 2n + 99\,\mathrm{OPT}_{\mathrm{ASS}}. Up to constant factors and the 2n2n for the initial tree, the offline cost of a sequence is a property of a point set.

12.5 Online: from geometric algorithms to BST algorithms

The conversions above see the whole future. The treap construction in particular orders keys by their next touch, which no online algorithm knows. An online geometric algorithm (Geometric.OnlineAlgorithm, Chapter 9) instead adds the points of each row knowing only the past rows and the current request, and Geometric.rowCost counts the points it adds. Demaine et al. (2009) showed that such an algorithm can be simulated online by a BST with a constant-factor overhead. The library proves this in the following form.

Theorem (online simulation). For every online geometric algorithm A on nn keys there is a single BST Algorithm of Chapter 5 such that, from every initial tree of size nn and on every sequence of keys below nn, its rotation count is at most 60186018 times the number of points A adds, plus 6018 n6018\,n.

theoremGeometric.Equivalence.Results.online_bstpropext · Classical.choice · Quot.soundModels/Equivalence/Abstract.lean:50
/-- Online: every online geometric algorithm has one causal BST policy, validfrom all initial shapes, with constant rotation cost per added point and alinear initialization allowance. The policy is fixed before the request sequence. -/theorem online_bst (n : Nat) (A : OnlineAlgorithm n) :    ∃ algorithm : Algorithm, ∀ tree : Tree, tree.size = n →      ∀ xs : List Nat, (∀ x ∈ xs, x < n) →        algorithm.cost tree xs ≤ 6018 * rowCost A.step 0 ∅ xs + 6018 * n :=  Online.Supernode.uniform_online_policy n A
  • Online in the strong sense. The algorithm is chosen before the tree and the sequence. Like every Algorithm, its step receives only the past requests, the current tree and the current request. It may simulate A on the past, since A sees no future either.
  • Uniform in the initial tree. One algorithm works for every initial shape of size nn, and the additive 6018 n6018\,n pays for adapting to it.
  • The constants are not sharp. The proof is an existence argument through last-touch supernodes stored in split trees, and its constants are the product of several lemmas. They are explicit only so that the statement is closed.

This theorem is the headline of Section 25.2, which proves it and states variants for one fixed initial tree, with total cost, and on the pointer machine. Its main use is GreedyASS (Section 25.1): the greedy geometric rule becomes an online BST algorithm whose cost is within a constant factor of its point count.

Direction

Only this direction is stated online. The other direction, from an online BST algorithm to an online geometric algorithm, is immediate in the region model (record the touched nodes), but the library states the tree-to-points conversions offline only, as in the previous sections.

12.6 A map of the models

The introduction to Part I collects the theorems of this chapter, together with the correspondences of Chapter 4 and Chapter 7, in one figure and one table.