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

Chapter 2Rotations

A rotation at a node swaps it with one of its children while keeping inorder fixed. With keys x<yx < y, the right rotation at yy turns ((A x B) y C)((A\,x\,B)\,y\,C) into (A x (B y C))(A\,x\,(B\,y\,C)), and the left rotation at xx 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 kk 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:

inductiveRotateno axiomsModels/Rotation/Abstract.lean:44
/-- 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:

  • right is the right rotation at the root. The left child of the root becomes the new root. Writing a,b,ca, b, c for the three subtrees, node (node a b) c becomes node a (node b c), which is the picture above with the keys removed.
  • left is its mirror image: the right child of the root becomes the new root.
  • inLeft and inRight lift a rotation of a subtree to the whole tree. If Rotate a b, then rotating inside the left subtree turns node a c into node b c, and likewise on the right.

A rotation at depth dd is therefore a derivation with dd 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.

Reading the Lean
  • The names follow the direction of the rotation, not the child being promoted: right promotes a left child. Chapter 7 names cursor moves after the promoted child instead (rotateLeftChild promotes a left child), so right and rotateLeftChild describe the same restructuring.
  • No subtree is copied, dropped or reordered: in both base cases the subtrees a,b,ca, b, c 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.
  • Rotate is 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 {0,…,n−1}\{0, \dots, n-1\}.

Theorem (rotations preserve size). If tt is obtained from ss by one rotation, then ss and tt 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 (∣a∣+1+∣b∣)+1+∣c∣=∣a∣+1+(∣b∣+1+∣c∣)(|a| + 1 + |b|) + 1 + |c| = |a| + 1 + (|b| + 1 + |c|), 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:

inductiveRotationsno axiomsModels/Rotation/Abstract.lean:51
/-- 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 00) and step adds one rotation at the front (cost k+1k+1). 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 jj rotations turn ss into tt and kk rotations turn tt into uu, then j+kj + k rotations turn ss into uu. If kk rotations turn aa into bb, then kk 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 kk rotations turn ss into tt, then ss and tt have the same number of nodes.

This is Rotations.size_eq, an induction using Rotate.size_eq.

Where these live

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.