Proofs: Splaying
These are the human proofs of Splaying, at full length.
The Access Lemma, an amortized bound of 3(r(t) - r(x)) + 1 for splaying full
This proof shows: amortized-time.
Give each node a weight , which does not change during the splay. For a node , let be the total weight of the subtree rooted at , and . The potential of the tree is . The cost of a splay is the number of rotations, or 1 if there are none. For a step, write and for the values before and and for the values after.
Fact. For , .
Proof. by the inequality of the arithmetic and geometric means, and taking gives .
In each step, only , its parent and (for a double step) its grandparent change their subtrees, so only their ranks change. After the step roots the subtree that (zig) or (double step) rooted, so or . Also before the step, since each subtree contains the one below it, and for each below after it.
Zig. The cost is 1, and , since and . So the amortized cost is at most .
Zig-zig. The cost is 2. With ,
using and . Before the step, the subtree of holds and its two subtrees ; after it, holds , the old sibling subtree of and the old other subtree of . These two node sets are disjoint and lie inside the new subtree of , so . By the fact, , that is . So
Zig-zag. The cost is 2. With ,
using . After the step and are the two children of , so and, by the fact, . So
since .
The whole splay. Let the steps be , with before the first and after the last, where is the root before the splay ( ends with the subtree of the whole tree, whose weight is ). Only the last step can be a zig. Adding the bounds of the steps, the sum telescopes:
If is already the root, the cost is 1 and , which also meets the bound.
Consequences. With for all nodes, and , so the amortized cost is at most . For a sequence of splays, the actual cost is the amortized cost plus ; with unit weights each rank lies between 0 and , so , and the total is at most . In general with , so the net drop of potential is at most .
Splaying keeps the binary search tree and ends with x at the root full
This proof shows: correctness, termination.
Let be a binary search tree with parent pointers in which, for each node , and when the children exist, and the root has no parent. Call this the link invariant.
Lemma 1 (rotation). If has a parent , ROTATE keeps the link invariant and the in-order sequence, makes a child of , and gives the old parent of (or no parent). The depth of drops by 1, and the subtree that now roots holds exactly the nodes of the old subtree of .
Proof. Take (line L8); the other case is the mirror image. Let be the subtrees of and the right subtree of , so the in-order sequence of the subtree of is . After line L8, has children and , and has children and , so the sequence of the new subtree of is , the same. The parent of the root of is set to in line L8, the parent of to and the parent of to in line L10, and the child pointer of that pointed to points to after line L11. All other links are unchanged. So the link invariant holds, and the in-order sequence of the whole tree is unchanged, since the subtree at that position has the same sequence. The keys of are in increasing order, so the BST property holds at and ; at every other node the set of keys in each subtree is unchanged.
Lemma 2 (one round). Assume $x.p \ne $ NIL at line L1. After the round, the link invariant and the in-order sequence still hold, and the depth of has dropped by 1 (line L3) or by 2 (lines L4 and L5).
Proof. Line L3 is one rotation of . In line L4 the first rotation, ROTATE, moves above ; by Lemma 1 the subtree of keeps as a child on the same side, so is still a child of and the second rotation is legal; it moves above , and takes the old place of , so its depth drops by 2. In line L5 the first rotation moves above , and takes the place of as a child of ; the second moves above . Each rotation keeps the invariant and the sequence by Lemma 1.
Theorem. SPLAY ends after rounds and rotations, where is the depth of , and then is a binary search tree with the same in-order sequence, the link invariant, and root .
Proof. By Lemma 2 the depth of drops by 2 in each round while it is at least 2, and the last round is a zig exactly when the depth is 1. So the number of rounds is and the number of rotations is . The loop stops when $x.p = $ NIL, that is when is the root, and line L6 records it. The invariants hold after each round by Lemma 2.