Proofs: Splay tree
These are the human proofs of Splay tree, at full length.
Splay tree operations take O(log n) amortized time full
This proof shows: amortized-time.
Setting. Give each node weight 1. For a node let be the number of nodes in its subtree and . The potential of a tree is , and of a collection of trees the sum over the trees. Each rank is between 0 and , where is the largest number of keys at any time. We count the cost of an operation as the number of nodes visited by its searches plus the number of rotations, plus ; a search that visits nodes is followed by a splay of a node at depth , which makes rotations, so the search costs at most one more than the splay, and it is enough to count rotations plus .
Access Lemma (Sleator and Tarjan, Lemma 1; proved in the complexity proof of splaying). The amortized number of rotations of a splay of in a tree with root is at most .
Access. One splay: at most amortized, plus .
Join. The splay of in costs at most amortized. Then is hung below . This changes only the rank of , which becomes , so the potential grows by at most . The total is .
Split. One access, then one link is cut at the root. Cutting a subtree from the root only lowers the rank of the root, and no other rank changes, so the potential does not grow. The total is .
Insert. One access, at most . Then the new node becomes the root with the old root below it. The rank of does not grow (it loses a subtree), no other old rank changes, and adds . The total is .
Delete. One access, at most . Removing the root removes its rank, which only lowers . Then a join, at most . The total is .
Sequences. For a sequence of operations that starts from a tree with keys, the actual cost is the sum of the amortized costs plus . Since and , the total actual cost is at most . So each operation costs amortized.
Remark. The constant 7 for delete is not tight; it comes from adding the bounds of the parts. Other weight choices in the Access Lemma give the static optimality, static finger and working set theorems.
Splay tree operations keep the binary search tree and return the right answers full
This proof shows: correctness, termination.
We use the result of splaying: SPLAY ends, keeps the strict BST property, the in-order sequence and the parent links, and makes the root. Write for the set of keys of .
Lemma 1 (access). After ACCESS with , the root is the node of if ; otherwise the root key is or . ACCESS returns the node of or NIL, correctly.
Proof. The loop of line L2 is the BST search; if it stops at the node of and line L4 splays it. If , the search ends at NIL below the last node of the path. If the search left to the left, then and the left subtree of is empty. Suppose some key lies strictly between and . For each node on the path, and are on the same side of , because is in the subtree that the search for entered at . So is not in the closed range from to , and is on the same side of as . Hence is in the subtree that the search entered at each , so it is in the subtree of , and as it is in the empty left subtree of , a contradiction. So . The case to the right gives the floor. Splaying makes it the root. Line L5 tests the root key against .
Insert. If , line L7 returns and the set does not change. Otherwise, by Lemma 1, the root is the floor or the ceiling of . If , then is the ceiling, so every key in the left subtree of is less than and not in , hence less than ; the keys of and its right subtree are greater than . So the tree with root , left subtree the old left subtree of , and right subtree (without its left subtree) has the BST property at , and at (which lost only a subtree). The case is the mirror image. Line L11 sets the parent links. The new key set is .
Join. Let all keys of be less than all keys of . If $L = $ NIL the answer is . Otherwise the maximum of , after the splay, is the root of ; it has no right child, since a right child would have a larger key. Hanging as its right subtree keeps the BST property at , as all keys of exceed , and at every other node the subtrees are unchanged.
Delete. If ACCESS returns NIL, and nothing changes in the set. Otherwise the root holds , its left subtree holds exactly the keys less than and its right subtree the keys greater than . So JOIN is legal and gives a BST on .
Split. After ACCESS, the root is the node of , or its floor or ceiling, by Lemma 1. If , then is or its floor, so each key of the right subtree of is greater than (a key in would contradict the choice of ), and each key of and its left subtree is at most . So line L19 splits into the keys and . If , then is the ceiling, so every key in the left subtree of is less than , and line L20 splits correctly.
Termination. Each search walks down a finite tree, each splay ends, and the walk to the maximum in line L16 walks down a finite tree.