Chapter 2Rotations
A rotation at a node swaps it with one of its children while keeping inorder fixed. With keys , the right rotation at turns into , and the left rotation at undoes it. Rotations are the only restructuring primitive in the model. Searching, splaying, insertion and deletion are all built from them, or charged in terms of them.
This chapter defines a single rotation, Rotate, and a sequence of exactly rotations, Rotations. Every cost in the book is a count of such steps, so these two relations appear on the reading list of almost every result. The theorems here are sanity checks rather than results: rotations preserve the number of nodes, sequences compose with additive cost, and a sequence inside a subtree is a sequence in the whole tree.
2.1 One rotation
Since keys are ranks and rotations preserve inorder, a rotation never changes any key. It only changes the shape. The library defines rotation as a relation between shapes:
/-- One rotation, at any depth. -/inductive Rotate : Tree → Tree → Prop where | right {a b c : Tree} : Rotate (.node (.node a b) c) (.node a (.node b c)) | left {a b c : Tree} : Rotate (.node a (.node b c)) (.node (.node a b) c) | inLeft {a b c : Tree} : Rotate a b → Rotate (.node a c) (.node b c) | inRight {a b c : Tree} : Rotate a b → Rotate (.node c a) (.node c b)Rotate s t holds when t is obtained from s by exactly one rotation somewhere in the tree. The four constructors split this into a rotation at the root and a rotation inside a subtree:
rightis the right rotation at the root. The left child of the root becomes the new root. Writing for the three subtrees,node (node a b) cbecomesnode a (node b c), which is the picture above with the keys removed.leftis its mirror image: the right child of the root becomes the new root.inLeftandinRightlift a rotation of a subtree to the whole tree. IfRotate a b, then rotating inside the left subtree turnsnode a cintonode b c, and likewise on the right.
A rotation at depth is therefore a derivation with uses of inLeft/inRight wrapped around one right or left. The relation has no position argument; where the rotation happens is recorded in the shape of its proof. The figure computes this proof term for every rotation you perform.
- The names follow the direction of the rotation, not the child being promoted:
rightpromotes a left child. Chapter 7 names cursor moves after the promoted child instead (rotateLeftChildpromotes a left child), sorightandrotateLeftChilddescribe the same restructuring. - No subtree is copied, dropped or reordered: in both base cases the subtrees appear in the same left-to-right order on both sides. This is exactly the statement that inorder is preserved, made precise in
Representation.rotation_preserves_keys. Rotateis a relation, not a function, so a shape may have many successors, one per non-root node. Every non-root node can be rotated over its parent, and these are the only moves.
A rotation preserves size, and therefore the set of keys .
Theorem (rotations preserve size). If is obtained from by one rotation, then and have the same number of nodes.
The library proves it as Rotate.size_eq, by induction on the rotation. Each base case is the equation , discharged by omega. The relation is also symmetric, since right and left are mutually inverse; this is proved in the library as Rotate.symm.
2.2 Counted sequences of rotations
Costs in the rotation model count rotations, so a sequence of rotations is recorded together with its exact length:
/-- Exactly `k` rotations. -/inductive Rotations : Tree → Tree → Nat → Prop where | refl {t : Tree} : Rotations t t 0 | step {s t u : Tree} {k : Nat} : Rotate s t → Rotations t u k → Rotations s u (k + 1)Rotations s t k says that t can be reached from s by exactly k rotations. refl is the empty sequence (cost ) and step adds one rotation at the front (cost ). This is the reflexive-transitive closure of Rotate, indexed by length. Because the count is exact, an upper bound on cost is always stated explicitly, as in ∃ k, k ≤ 2 * s.size ∧ Rotations s t k. The relation itself never silently allows extra rotations.
Three closure properties are used throughout.
Theorem (composition and lifting). If rotations turn into and rotations turn into , then rotations turn into . If rotations turn into , then rotations turn node a c into node b c, and node c a into node c b.
These are Rotations.append, Rotations.inLeft and Rotations.inRight.
Theorem (sequences preserve size). If rotations turn into , then and have the same number of nodes.
This is Rotations.size_eq, an induction using Rotate.size_eq.
Rotate and Rotations are in Models/Rotation/Abstract.lean, the most basic reading module. The five lemmas above are proved in Models/Rotation/Basic.lean and Models/Rotation/Rotations.lean. They are not part of any theorem statement, so they are outside the reading interface and the book states them without quoting their Lean. They are the first sanity checks a reader would want, and the proofs use them constantly.