Chapter 23Move-to-root
The simplest self-adjusting rule comes first. Move-to-root (Allen and Munro 1978) serves a request by finding and then rotating it over its parent, again and again, until it is the root. Recently requested keys stay near the top, which is the intuition behind every self-adjusting tree. Under independent random requests, Allen and Munro showed that its expected cost is within a constant factor of the optimal static tree. In the worst case it is poor: a sequential scan costs rotations per request, forever. Splaying (Section 24.1) repairs exactly this defect by rotating in pairs.
What the library proves about move-to-root is that it is correct: it really performs an access in the rotation model, and it reports its rotation count truthfully. It proves no amortized bound, in either direction. The poor behaviour on scans is shown at the end of the chapter as a measurement.
Let be any binary search tree on keys and any of its keys. Move-to-root returns a tree and a count such that rotations turn into , the key is the root of , and .
In Lean, the algorithm is the function moveToRoot, which returns the pair . The conclusion is Access from Chapter 3: a sequence of exactly rotations from to , with at the root of .
The Lean statement
/-- Move-to-root exposes the requested rank with a legal rotation trace, usingfewer than `size` rotations. -/theorem correct (t : Tree) (x : Nat) (valid : x < t.size) : Access t (moveToRoot t x).1 x (moveToRoot t x).2 ∧ (moveToRoot t x).2 < t.size := … moveToRoot_correct t x validIn the library, move-to-root plays a second role. It is the constructive witness for the existence results of the rotation model (Chapter 3): every existing key can be brought to the root, with fewer rotations than there are nodes.
23.1 The algorithm
/-- Recursively expose x in its child subtree, then rotate it one level up.Out-of-range inputs have arbitrary behavior; correctness is required only in range. -/def moveToRoot : Tree → Nat → Tree × Nat | .nil, _ => (.nil, 0) | .node l r, x => if x < l.size then let (u, k) := moveToRoot l x match u with | .nil => (.node l r, 0) | .node a b => (.node a (.node b r), k + 1) else if x = l.size then (.node l r, 0) else let (u, k) := moveToRoot r (x - l.size - 1) match u with | .nil => (.node l r, 0) | .node a b => (.node (.node l a) b, k + 1)moveToRoot t x returns the new tree and the number of rotations performed. It follows the three-way search on ranks of Chapter 1:
- If
x < l.size, the key is in the left subtree. The recursive call returns a treeuwithxat its root,u = node a b. One right rotation at the root ofnode u rthen liftsxabove its parent:node (node a b) rbecomesnode a (node b r). The cost is the recursive cost plus one. - If
x = l.size, the key is already the root, and nothing happens. - Otherwise the key is in the right subtree, at rank
x - l.size - 1there, and the mirror-image left rotation lifts it.
The rotations are performed on the way back from the recursion, so the deepest one comes first. Each is a rotation at the root of the current subtree, which in the whole tree is a Rotate.right or .left wrapped in inLeft/inRight constructors. The number of rotations is the depth of .
- The two branches
| .nil => (.node l r, 0)cannot occur for a valid rank: whenx < l.size, the left subtree is nonempty and its recursive result has a root. They make the function total, as does the arbitrary result for out-of-range ranks, which the docstring states explicitly. x - l.size - 1is truncated subtraction onNat. It is only evaluated whenx > l.size, so it never truncates.- The function returns its own cost. Nothing in the definition prevents a wrong count; the theorem below ties it to an actual rotation sequence.
23.2 Correctness
Theorem (correctness of move-to-root). For every tree t and every rank x < t.size, the pair (moveToRoot t x) is a legal root access: there is a sequence of exactly (moveToRoot t x).2 rotations from t to the returned tree, and the returned tree has x at its root. Moreover the count is below t.size.
/-- Move-to-root exposes the requested rank with a legal rotation trace, usingfewer than `size` rotations. -/theorem correct (t : Tree) (x : Nat) (valid : x < t.size) : Access t (moveToRoot t x).1 x (moveToRoot t x).2 ∧ (moveToRoot t x).2 < t.size := moveToRoot_correct t x validThe “statement reads only” line shows that this theorem depends on nothing but the rotation model and the definition above.
This is the witness behind access_exists_iff. The count is in fact exactly the depth of , which is at most t.size - 1; the library states only the weaker bound, which is what the existence result needs.
23.3 As an online algorithm
Packaged as an Algorithm, move-to-root ignores the request history:
/-- A baseline algorithm, not splaying: single rotations move the request upward. -/def moveToRootAlgorithm : Algorithm where step _ t x := moveToRoot t x correct _ t x hx := (moveToRoot_correct t x hx).1The certificate correct is the first conjunct of the theorem above. All the definitions of Chapter 5 and Part II therefore apply to it: moveToRootAlgorithm.cost, its total cost, and the properties HasWorkingSetBound and the others of Chapter 14. The library proves none of those properties for move-to-root, and they fail: on the repeated scans below, the cost per request grows linearly in , while every benchmark of Part II charges a scan request .
23.4 Why it is not enough
Start from a left path on keys, the tree whose root is and whose every node has only a left child, and request . Key is at depth ; moving it to the root costs rotations and leaves keys in a path hanging from its right child. Each later request is again at the bottom of that path. The scan ends in a left path again, so the next scan costs the same. The figure measures rotations per scan, per request.
The reason is that move-to-root brings to the top but pushes every other node of the access path down by one level. The path does not get shorter, it just changes which node is at its top. Splaying differs from move-to-root in one case only. When and its parent are both left children, or both right children, it rotates the parent over the grandparent first and then over its parent (the zig-zig step). This roughly halves the depth of every node on the access path. The same scan then costs in total, the sequential access theorem.
With sequential scans on a left path of keys, move-to-root pays rotations per scan and splaying about . Switch to uniform random requests: the two curves are close, in line with Allen and Munro’s average-case analysis. The difference between the algorithms is in the worst case, and only splaying has the amortized guarantees of Section 24.2.
The library proves no lower bound for move-to-root. The quadratic scan above is a measurement of the JavaScript port of moveToRoot, which the test suite checks against Lean on all shapes with at most five nodes and on random larger trees. It is not a theorem.