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

Chapter 9The geometric view

Run a BST algorithm on a request sequence and record, for each request, the set of nodes it touches. Plot each touched key xx at time tt as the point (x,t)(x, t), with keys on the horizontal axis and time going upward. Demaine, Harmon, Iacono, Kane and Pătraşcu (2009) observed that the point sets arising this way have a purely geometric description. Whenever two points do not share a row or a column, the rectangle they span contains a third point. Conversely, every point set with this property, which they call arborally satisfied, is the touch set of some BST execution. The cost of an execution is its number of points.

This turns BST optimization into a covering problem in the plane. The offline optimum for a request sequence is, up to a constant factor, the size of the smallest arborally satisfied superset of the request points {(xt,t)}\{(x_t, t)\}. Lower bounds become statements about rectangles, and Wilber’s bound of Chapter 17 is proved here against every arborally satisfied set. Algorithms become rules that complete one row at a time, and the most natural such rule, GreedyASS (Section 25.1), is conjectured to be dynamically optimal.

This chapter states the model: points and arborally satisfied sets, offline solutions, and online algorithms that add one row per request. Its theorems are small facts about these definitions. The result that makes the geometric view worth having, that point counts and BST costs agree up to constant factors, is the key result of Chapter 12, and every definition it needs is in this chapter or in Part I. The definitions are in Models/Geometric/Definitions/Abstract.lean, Online/Abstract.lean, Offline/Abstract.lean and Static/Abstract.lean, and the theorems in Models/Geometric/Abstract.lean. The correspondence with the tree models is Chapter 12.

9.1 Points and rectangles

defGeometric.Pointno axiomsModels/Geometric/Definitions/Abstract.lean:15
/-- A key and a time, both numbered from zero. -/abbrev Point := Nat × Nat
defGeometric.Rectangleno axiomsModels/Geometric/Definitions/Abstract.lean:18
/-- The closed axis-aligned rectangle with opposite corners `a` and `b`. -/def Rectangle (a b p : Point) : Prop :=  min a.1 b.1 ≤ p.1 ∧ p.1 ≤ max a.1 b.1 ∧  min a.2 b.2 ≤ p.2 ∧ p.2 ≤ max a.2 b.2

A point is a pair (key, time) of natural numbers. Keys are inorder ranks, as in every other model, and times number the requests from 00. Rectangle a b p says that p lies in the closed axis-parallel rectangle with opposite corners a and b; the corners need not be the lower left and upper right ones.

defGeometric.ASSpropext · Classical.choice · Quot.soundModels/Geometric/Definitions/Abstract.lean:23
/-- Arboral satisfaction: every nondegenerate rectangle spanned by two pointscontains a third point, possibly on its boundary. -/def ASS (P : Finset Point) : Prop :=  ∀ a ∈ P, ∀ b ∈ P, a.1 ≠ b.1 → a.2 ≠ b.2 →    ∃ p ∈ P, p ≠ a ∧ p ≠ b ∧ Rectangle a b p

A finite point set P is arborally satisfied when every pair of points a, b of P with different keys and different times spans a rectangle containing a third point of P. The third point may lie on the boundary, including at one of the two other corners.

Reading the Lean
  • Pairs on a common row or column impose nothing: a.1 ≠ b.1 → a.2 ≠ b.2 → …. Their “rectangle” is a segment.
  • p ≠ a ∧ p ≠ b excludes the two corners themselves, and nothing else. A point at the corner (a1,b2)(a_1, b_2) satisfies the rectangle.
  • Pairs are ordered, so each rectangle is checked twice. This is harmless.

Since P is a finite set, every quantifier in ASS ranges over a finite set, and the library makes this explicit with decidability instances. They let small cases be checked by decide, and they make the brute-force GreedyASS of Section 25.1 executable.

instanceGeometric.instDecidableRectangleno axiomsModels/Geometric/Definitions/Abstract.lean:48
instance (a b p : Point) : Decidable (Rectangle a b p) :=  inferInstanceAs (Decidable (_ ∧ _ ∧ _ ∧ _))
instanceGeometric.instDecidableASSpropext · Classical.choice · Quot.soundModels/Geometric/Definitions/Abstract.lean:51
instance (P : Finset Point) : Decidable (ASS P) :=  inferInstanceAs (Decidable (∀ a ∈ P, ∀ b ∈ P, _ → _ → ∃ p ∈ P, _ ∧ _ ∧ _))
instanceGeometric.instDecidableCompletionpropext · Classical.choice · Quot.soundModels/Geometric/Definitions/Abstract.lean:54
instance (n time request : Nat) (P : Finset Point) (keys : Finset Nat) :    Decidable (Completion n time request P keys) :=  inferInstanceAs (Decidable (_ ∧ _ ∧ _))

9.1.1 An example

Serve the requests 1,3,0,21, 3, 0, 2 on four keys with the fixed tree whose root is 11, with left child 00, right child 33, and 22 the left child of 33. Each search touches its root-to-key path: {1}\{1\} at time 00, then {1,3}\{1, 3\}, then {0,1}\{0, 1\}, then {1,2,3}\{1, 2, 3\}. These are 88 points, which is the fixed-tree cost StaticTree.cost of the sequence: one unit per request plus its depth.

The request points alone, (1,0),(3,1),(0,2),(2,3)(1,0), (3,1), (0,2), (2,3), are not arborally satisfied: the rectangle of (1,0)(1,0) and (3,1)(3,1) is empty. The touched points are. In the figure, the “Requests only” and “GreedyASS” buttons load the two sets (GreedyASS happens to produce exactly the touches of this tree). Click two points to inspect their rectangle, or click cells to add and remove points; the panel finds an empty rectangle whenever there is one.

9.2 Why tree executions are satisfied

Here is the classical argument for a fixed tree, where each request touches its search path. Take two touched points (x,t)(x, t) and (y,u)(y, u) with x<yx < y and t≠ut \ne u, and let ℓ\ell be the lowest common ancestor of xx and yy, so x≤ℓ≤yx \le \ell \le y. If ℓ≠x\ell \ne x, then ℓ\ell is a proper ancestor of xx, so it was touched at time tt, and (ℓ,t)(\ell, t) is a third point in the rectangle. If ℓ=x\ell = x, then xx is an ancestor of yy, so (x,u)(x, u) was touched and lies in the rectangle. For trees that change over time the same argument works, because the lowest common ancestor of two keys can only change at a time when it is touched.

The library states the fixed-tree case for any family of touched sets that are rooted and connected in each row:

defGeometric.RootedRowspropext · Classical.choice · Quot.soundModels/Geometric/Static/Abstract.lean:9
/-- In each row, the touched keys form a rooted connected region of one fixedBST. The root has absolute rank `lo + left.size`; subtrees stay unlabelled. -/def RootedRows : Tree → Nat → Finset Point → Prop  | .nil, _, P => P = ∅  | .node l r, lo, P =>      (∀ p ∈ P, (lo + l.size, p.2) ∈ P) ∧      RootedRows l lo (P.filter fun p => p.1 < lo + l.size) ∧      RootedRows r (lo + l.size + 1) (P.filter fun p => lo + l.size < p.1)

RootedRows t lo P reads the tree top-down. At a node whose key is rr = lo + l.size, every point of P (all of which have keys in this subtree) forces (r,its time)(r, \text{its time}) into P: if anything below or at this node is touched at some time, the node itself is touched at that time. The points with keys below rr must satisfy the same condition in the left subtree, and those above rr in the right subtree. The empty tree admits no points.

Reading the Lean

Keys outside the tree’s range are ruled out, not ignored. A point with key below lo is passed to the left subtree at every level, and eventually reaches a nil, where P = ∅ fails. The same holds for keys at or beyond lo + t.size.

Theorem (fixed trees give satisfied sets). For every tree t, offset lo and finite point set P, if RootedRows t lo P holds then P is arborally satisfied.

theoremGeometric.Results.fixed_tree_viewpropext · Classical.choice · Quot.soundModels/Geometric/Abstract.lean:39
/-- The geometric view of rooted touches in a fixed BST is ASS. -/theorem fixed_tree_view (t : Tree) (lo : Nat) (P : Finset Point)    (rooted : RootedRows t lo P) : ASS P := rootedRows_ass t lo P rooted

The proof is the induction above: a pair on the same side of the root is handled by the subtree, and a pair separated by the root, or containing it, finds a root point on one of its two rows. The dynamic version of the argument is part of the equivalence theorems of Chapter 12.

9.3 Offline solutions

Requests are the points (xt,t)(x_t, t):

defGeometric.requestsAtpropext · Quot.soundModels/Geometric/Online/Abstract.lean:28
/-- The access-sequence point set, starting at the supplied time. -/def requestsAt : Nat → List Nat → Finset Point  | _, [] => ∅  | time, x :: xs => insert (x, time) (requestsAt (time + 1) xs)

requestsAt time xs places the jj-th request on row time + j. With time = 0, request jj is the point (xj,j)(x_j, j). Repeated keys are allowed; they lie in different rows.

structureGeometric.Solutionpropext · Classical.choice · Quot.soundModels/Geometric/Definitions/Abstract.lean:58
/-- An offline geometric solution includes all requests and is arborallysatisfied. Its cost is `points.card`; the initial BST convention is separate. -/structure Solution (requests : Finset Point) where  points : Finset Point  contains : requests ⊆ points  satisfied : ASS points

A solution for a set of requests is an arborally satisfied superset of them. Its cost is its number of points.

structureGeometric.Offlinepropext · Classical.choice · Quot.soundModels/Geometric/Offline/Abstract.lean:16
/-- A bounded offline geometric execution of the given access sequence.The entire set may depend on future requests. -/structure Offline (n : Nat) (xs : List Nat) extends Solution (requestsAt 0 xs) where  bounded : History n xs.length points

An offline solution for nn keys and the sequence xs is a solution for requestsAt 0 xs whose points all have keys below nn and times below mm = xs.length. The set is chosen with the whole sequence in view, hence “offline”. The bound rules out extra columns, which would be keys that do not exist, and extra rows, which would be work done after the last request.

Reading the Lean
  • Offline extends Solution, so E.points, E.contains and E.satisfied are its fields, together with E.bounded.
  • No initial tree is mentioned. A geometric solution corresponds to a BST execution from an initial tree of its choosing. Starting from a prescribed tree costs an extra O(n)O(n), and the theorems of Chapter 12 state this term explicitly.

Two facts about offline solutions. Every solution contains the mm request points, so its cost is at least mm, just as the total cost of a BST execution is at least the number of requests.

Theorem (requests are paid for). Every offline solution for a sequence of mm requests has at least mm points.

theoremGeometric.Results.request_lower_boundpropext · Classical.choice · Quot.soundModels/Geometric/Abstract.lean:35
/-- Every solution contains the request points, so it has at least `m` points. -/theorem request_lower_bound {n : Nat} {xs : List Nat} (E : Offline n xs) :    xs.length ≤ E.points.card := offline_lower_bound E

Theorem (an optimum exists). For every sequence xs of keys below nn, there is an offline solution EE whose number of points is at most that of every offline solution for nn keys and xs.

theoremGeometric.Results.optimal_existspropext · Classical.choice · Quot.soundModels/Geometric/Abstract.lean:30
/-- A minimum offline geometric solution exists for every valid sequence. -/theorem optimal_exists {n : Nat} (xs : List Nat) (valid : ∀ x ∈ xs, x < n) :    ∃ E : Offline n xs, ∀ F : Offline n xs, E.points.card ≤ F.points.card :=  offline_minimum xs valid

Existence of some solution is the only content, since costs are natural numbers. The full grid {0,…,n−1}×{0,…,m−1}\{0, \dots, n-1\} \times \{0, \dots, m-1\} is a solution: every nondegenerate rectangle with corners on the grid contains its two other corners. The library instead runs the GreedyASS sweep of Section 25.1 and takes the least cost with Nat.find. The minimum cost is the geometric counterpart of the offline BST optimum.

9.4 Row by row

A BST algorithm serves requests in order, so its points arrive one row at a time. Rows are built from sets of keys:

defGeometric.rowpropext · Classical.choice · Quot.soundModels/Geometric/Definitions/Abstract.lean:29
/-- Put a finite set of keys on one time row. -/def row (time : Nat) (keys : Finset Nat) : Finset Point :=  keys.image fun x => (x, time)
defGeometric.Historypropext · Classical.choice · Quot.soundModels/Geometric/Definitions/Abstract.lean:33
/-- A history over `n` keys, strictly before the next time `time`. -/def History (n time : Nat) (P : Finset Point) : Prop :=  ∀ p ∈ P, p.1 < n ∧ p.2 < time

row time keys puts the keys on one row. History n time P says that P uses only the nn keys and only rows strictly before time: it describes what has happened before the current request.

defGeometric.Completionpropext · Classical.choice · Quot.soundModels/Geometric/Definitions/Abstract.lean:37
/-- A legal online extension: include the request, use only valid keys, andsatisfy all rectangles after adding points on this row. -/def Completion (n time request : Nat) (P : Finset Point) (keys : Finset Nat) : Prop :=  request ∈ keys ∧ keys ⊆ Finset.range n ∧ ASS (P ∪ row time keys)

A legal row for the request at time time contains the request, uses only existing keys, and keeps the whole set arborally satisfied once it is added. The condition is checked on the union P ∪ row time keys, so a new row must satisfy the rectangles it forms with every old point, not only among its own points. Old rows cannot change.

Among legal rows, the smallest one is a natural choice:

defGeometric.GreedyRowpropext · Classical.choice · Quot.soundModels/Geometric/Definitions/Abstract.lean:42
/-- A minimum-cardinality legal extension of the current row. This specifiesthe greedy geometric algorithm independently of its implementation. -/def GreedyRow (n time request : Nat) (P : Finset Point) (keys : Finset Nat) : Prop :=  Completion n time request P keys ∧    ∀ other, Completion n time request P other → keys.card ≤ other.card

GreedyRow specifies the greedy rule by a property: a legal row of minimum cardinality. It does not say that such a row is unique, or how to find it. Section 25.1 proves that it is unique and is computed by a simple sweep.

9.4.1 Online algorithms

structureGeometric.OnlineAlgorithmpropext · Classical.choice · Quot.soundModels/Geometric/Online/Abstract.lean:8
/-- An online geometric algorithm sees only the old point set and the currentrequest. It must complete the current row without changing past rows. -/structure OnlineAlgorithm (n : Nat) where  step : Nat → Nat → Finset Point → Finset Nat  correct : ∀ time request P, History n time P → ASS P → request < n →    Completion n time request P (step time request P)

An online geometric algorithm is a function from the current time, the current request and the points so far to the keys of the new row, with a certificate. Whenever the history is bounded and arborally satisfied and the request is valid, the row is legal. The step receives no future requests, and this is the only sense of “online”.

Reading the Lean
  • The certificate must hold for every satisfied history, not only for those the algorithm itself produces. This is stronger than necessary, and GreedyASS meets it.
  • The step sees the past points but not the past request sequence. It cannot tell which point of an old row was the request. For GreedyASS this does not matter.
  • Computation is uncharged, as in Section 5.1. Only points count.
defGeometric.fillpropext · Classical.choice · Quot.soundModels/Geometric/Online/Abstract.lean:15
/-- Run from a supplied history, numbering new rows consecutively. -/def fill (step : Nat → Nat → Finset Point → Finset Nat) :    Nat → Finset Point → List Nat → Finset Point  | _, P, [] => P  | time, P, x :: xs => fill step (time + 1) (P ∪ row time (step time x P)) xs
defGeometric.rowCostpropext · Classical.choice · Quot.soundModels/Geometric/Online/Abstract.lean:21
/-- Charge exactly one unit for every point added, including each request. -/def rowCost (step : Nat → Nat → Finset Point → Finset Nat) :    Nat → Finset Point → List Nat → Nat  | _, _, [] => 0  | time, P, x :: xs => (step time x P).card +      rowCost step (time + 1) (P ∪ row time (step time x P)) xs

fill step time P xs runs a step function from history P, numbering new rows consecutively from time. rowCost charges one unit for each point added, the request included, so it is the geometric analogue of the total cost T=m+RT = m + R.

An online run is an offline solution, and its cost is exactly its number of points.

Theorem (online runs are solutions). For every online geometric algorithm A on nn keys and every sequence xs of keys below nn, the points that A produces on xs, starting from the empty history, form an offline solution with exactly rowCost points.

theoremGeometric.Results.online_solutionpropext · Classical.choice · Quot.soundModels/Geometric/Abstract.lean:14
/-- Online row cost is exactly the cardinality of the resulting ASS solution. -/theorem online_solution {n : Nat} (A : OnlineAlgorithm n) (xs : List Nat)    (valid : ∀ x ∈ xs, x < n) :    ∃ E : Offline n xs, E.points = fill A.step 0 ∅ xs ∧      E.points.card = rowCost A.step 0 ∅ xs := offline_of_online A xs valid

The cardinality claim needs a short argument: each row is added at a fresh time, so no two rows share a point.

Conversely, every offline solution can be read row by row as a sequence of legal moves:

defGeometric.pastPointspropext · Quot.soundModels/Geometric/Offline/Abstract.lean:8
/-- The rows strictly preceding `time`. -/def pastPoints (P : Finset Point) (time : Nat) : Finset Point :=  P.filter fun p => p.2 < time
defGeometric.keysAtpropext · Classical.choice · Quot.soundModels/Geometric/Offline/Abstract.lean:12
/-- The keys touched at a given time. -/def keysAt (P : Finset Point) (time : Nat) : Finset Nat :=  (P.filter fun p => p.2 = time).image Prod.fst

pastPoints P time is the part of P strictly before time, and keysAt P time the keys on row time.

Theorem (rows of a solution are legal). If a finite point set P is arborally satisfied, has keys below nn and contains the request point at time time, then the row of P at time is a legal completion of the rows of P before it.

theoremGeometric.Results.offline_rowpropext · Classical.choice · Quot.soundModels/Geometric/Abstract.lean:20
/-- Conversely, an offline ASS solution can be revealed in legal rows.The choice of those rows may depend on the future. -/theorem offline_row {n time request : Nat} {P : Finset Point}    (bounded : ∀ p ∈ P, p.1 < n) (satisfied : ASS P) (req : (request, time) ∈ P) :    Completion n time request (pastPoints P time) (keysAt P time) :=  offline_completion bounded satisfied req

The reason is that arboral satisfaction is inherited by every prefix of the rows. A rectangle spanned by two points at times at most tt has all its points at times at most tt, so the third point it contains is in the prefix too. The rows of an offline solution are legal, but each may have been chosen with knowledge of later requests, and an online algorithm cannot reproduce them in general.

Reading an offline solution row by row also accounts for all of its points. Here scheduleCost P time adds the row sizes of the first time rows.

Theorem (rows account for every point). For every offline solution EE of a sequence of mm requests, the sizes of its first mm rows add up to its number of points.

defGeometric.scheduleCostpropext · Classical.choice · Quot.soundModels/Geometric/Offline/Abstract.lean:21
/-- The number of points in the first `time` rows, computed row by row. -/def scheduleCost (P : Finset Point) : Nat → Nat  | 0 => 0  | time + 1 => scheduleCost P time + (keysAt P time).card
theoremGeometric.Results.offline_costpropext · Classical.choice · Quot.soundModels/Geometric/Abstract.lean:27
theorem offline_cost {n : Nat} {xs : List Nat} (E : Offline n xs) :    scheduleCost E.points xs.length = E.points.card := bounded_schedule_cost E.bounded

For an offline solution all rows are below mm, so the first mm rows hold every point.

What comes next

Online and offline geometric algorithms now mirror online algorithms and offline executions in the BST models. Chapter 12 makes the correspondence precise: offline, point counts and BST costs agree up to constant factors (exactly, for the region model), and online, every geometric algorithm can be simulated by an online BST algorithm. The simulation is the subject of Section 25.2, and GreedyASS, the geometric algorithm it is usually applied to, is Section 25.1.