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

Chapter 8Touched regions

The rotation model of Chapter 3 counts rotations, and the pointer machine of Chapter 7 counts cursor moves and rotations. A third accounting is standard in the work on dynamic optimality since Demaine, Harmon, Iacono, Kane and Pătraşcu (2009). It forgets how a search restructures and counts only which nodes it touches.

Serving a request touches a set of nodes that contains the requested key and forms a connected subtree containing the root. The algorithm may then rearrange the touched nodes into any shape, as long as every untouched subtree hangs back in its inorder slot. The cost is the number of touched nodes. A pointer search that visits kk nodes is such a step, since the cursor only travels along edges from the root and only rotates nodes it has visited. Conversely, any rearrangement of kk touched nodes can be performed by a pointer machine with O(k)O(k) instructions. The models therefore agree up to constant factors, and Chapter 12 proves it.

The point of this model is the next chapter. Each step is described by a single set of keys, so a whole execution becomes a set of points (key, time) in the plane, and its cost is the number of points. This chapter only sets up the model; it has no theorem of its own. Its definitions are used by the exact equivalence between region executions and point sets in Chapter 12, by Wilber’s bound (Chapter 17) and by Tango trees (Section 26.2). They are in Models/Region/Abstract.lean and Models/Region/View/Abstract.lean.

8.1 Regions

A touched region is a tree whose leaves are either empty or entire untouched subtrees. The library already has this type: the skeletons of the lazy pointer simulation (Lazy.Skeleton, from Chapter 7). A skeleton is built from node, an active node of the region, and hole t, an untouched subtree t hanging below the region. Skeleton.tree fills the holes in, Skeleton.size counts only the active nodes, and Skeleton.fringe lists the hole subtrees in inorder.

defRegionno axiomsModels/Region/Abstract.lean:9
/-- A rooted region of a BST: the active nodes near the root, with untouchedsubtrees hanging from its holes. -/abbrev Region := Pointer.Lazy.Skeleton

Region is another name for the same type, so all the skeleton operations apply to regions unchanged.

Reading the Lean

Being rooted and connected is not a hypothesis: it is built into the type. A region of s is a skeleton before with before.tree = s, and the node constructors of a skeleton always form a top part of the tree it describes. The only region with no active node is hole s, the whole tree left untouched.

Regions do not store keys, so, as everywhere in the library, keys are inorder ranks computed with an offset lo (Section 1.5). Two functions read a region in absolute ranks:

defRegion.keyspropext · Classical.choice · Quot.soundModels/Region/Abstract.lean:15
/-- Absolute inorder ranks of the nodes which may be rearranged. -/def keys : Region → Nat → Finset Nat  | .hole _, _ => ∅  | .node l r, lo => insert (lo + l.tree.size)      (keys l lo ∪ keys r (lo + l.tree.size + 1))
defRegion.holesno axiomsModels/Region/Abstract.lean:21
/-- Untouched fringe subtrees, with their absolute inorder offsets. -/def holes : Region → Nat → List (Nat × Tree)  | .hole t, lo => [(lo, t)]  | .node l r, lo => holes l lo ++ holes r (lo + l.tree.size + 1)

keys s lo is the set of ranks of the active nodes when the region’s leftmost key has rank lo. The root of node l r has rank lo + l.tree.size: the size of the whole left subtree, holes included, since untouched keys still come before it in inorder. holes s lo lists the hole subtrees with the rank at which each one starts. A region with kk active nodes has k+1k + 1 holes, some of which may be empty trees.

defRegion.Samepropext · Classical.choice · Quot.soundModels/Region/Abstract.lean:26
/-- Reordering preserves all active keys and the exact untouched fringe. -/def Same (s t : Region) (lo : Nat) : Prop :=  s.tree.size = t.tree.size ∧ keys s lo = keys t lo ∧ holes s lo = holes t lo

Same s t lo says that two regions differ only in the arrangement of their active nodes. They have the same total size, the same active keys and the same holes at the same offsets. It is the invariant that a step preserves, stated in absolute ranks for the proofs of Wilber’s bound (Chapter 17) and of the treap construction (Chapter 32).

8.1.1 An example

Take the perfect tree on the ranks 0,…,60, \dots, 6: root 33, children 11 and 55, and leaves 0,2,4,60, 2, 4, 6. Request 44, and touch its search path {3,5,4}\{3, 5, 4\}. Write LL for the subtree of 11 (ranks 00–22) and RR for the leaf 66. The region is

.node (.hole L) (.node (.node (.hole .nil) (.hole .nil)) (.hole R))

Its keys at offset 00 are {3,4,5}\{3, 4, 5\}, and its holes are [(0,L),(4,nil),(5,nil),(6,R)][(0, L), (4, \mathtt{nil}), (5, \mathtt{nil}), (6, R)]. One legal rearrangement puts 44 at the root:

.node (.node (.hole L) (.hole .nil)) (.node (.hole .nil) (.hole R))

It has the same fringe [L,nil,nil,R][L, \mathtt{nil}, \mathtt{nil}, R], the same keys and the same holes, and the step costs 33. In the rotation model the same restructuring costs 22 rotations, and the request itself is free.

8.2 Steps and executions

structureRegionSteppropext · Classical.choice · Quot.soundModels/Region/View/Abstract.lean:8
/-- One search in the rooted-subtree version of the BST model. The activeregion contains the requested key, and the inorder fringe is unchanged. -/structure RegionStep (s t : Tree) (request : Nat) where  before : Region  after : Region  source : before.tree = s  target : after.tree = t  fringe : before.fringe = after.fringe  visits : request ∈ Region.keys before 0

A RegionStep s t request is a record of one search: a region before of the old tree s, a region after of the new tree t, a proof that both have the same fringe, and a proof that the request is one of the touched keys. The fringe condition is the whole content of “rearrange the touched nodes and leave everything else alone”. It fixes the hole subtrees and their inorder order. Counting fringe entries, it also forces before and after to have the same number of active nodes, which then occupy the same ranks: before and after are Same at offset 00.

Reading the Lean
  • RegionStep is a structure, not a Prop: it is data that carries both regions, so that the cost can be read from it. The executions below quantify over it through their constructor.
  • visits makes every step touch at least one node, the requested one. A request for the current root therefore costs 11, like the total cost T=m+RT = m + R of Section 5.2 and the unit request charge of the pointer machine.
  • The new shape of the region is arbitrary. In particular, the request need not end at the root, and nothing limits how far the region is reshaped.
inductiveRegionExecutionpropext · Classical.choice · Quot.soundModels/Region/View/Abstract.lean:18
/-- A sequence of rooted-region operations, charging the number of touchednodes on each row. -/inductive RegionExecution : Tree → List Nat → Tree → Nat → Prop  | nil {t : Tree} : RegionExecution t [] t 0  | cons {s t u : Tree} {x cost : Nat} {xs : List Nat} (step : RegionStep s t x) :      RegionExecution t xs u cost →      RegionExecution s (x :: xs) u (step.before.size + cost)

RegionExecution s xs u cost serves the requests xs one step at a time from s, ends at u, and charges each step the number of nodes in its before region. It has the same shape as Execution and Pointer.Execution, with a region step in place of a root access or a pointer search.

Where this model is used

The region model is the hinge between trees and geometry. Chapter 12 proves that a set of points in the key–time plane is an offline solution of the geometric problem of Chapter 9, with exactly cc points, if and only if some region execution, from some initial tree, has cost exactly cc. Each direction rests on a heap invariant (Chapter 32). From a region execution, the touched points are the geometric solution, and the proof that they are arborally satisfied keeps the current tree a max-heap of the time each key was last touched (Geometric.lastTouch, Geometric.HistoryHeap). Rearranging a region preserves this for free, since the touched nodes are on top and all share the latest time. From a geometric solution, the tree at each time is a treap keyed by the time each key is next touched, and the region touched at that time is the top of the treap (Region.project).