Proofs: Scapegoat tree
These are the human proofs of Scapegoat tree, at full length.
The scapegoat tree keeps its keys and its depth bound full
This proof shows: correctness.
Fix with , and write for . The depth of the root is 0, and is the number of nodes in the subtree of . The tree keeps two counters, and . We show that after each operation the following hold.
- (S1) The tree is a binary search tree whose keys are the keys inserted and not deleted, and is their number.
- (S2) .
- (S3) Each node has depth at most .
All three hold for the empty tree with (there are no nodes, so (S3) holds).
Rebuilding. REBUILD lists the nodes of the subtree of in key order (line L9) and links them again by BUILD (line L10). BUILD puts at the root, the nodes left of in its left subtree and those right of in its right subtree, so by induction on the result is a search tree on the same nodes in the same order. Its two subtrees have sizes that differ by at most one, so a tree on nodes has height , the least possible height of a binary tree on nodes. Rebuilding does not change the key set or the order, and only the depths of nodes inside the rebuilt subtree change.
Lemma 1. Let be a node of depth in a tree with nodes. Then some proper ancestor of has a child on the path to with .
Proof. Let be the path from up to the root. If for all , then , which is not possible, since the root has nodes.
So the loop of line L4 always stops at a node , and is the first such ancestor met when walking up from .
Insertion. If is present, line L1 returns and nothing changes. Otherwise a leaf with key is attached where the search ended, which keeps (S1), and line L2 adds one to and , which keeps (S1) and (S2), because follows from and . Let now denote the new value. The old nodes keep their depths, which are at most the old . Let be the depth of . If , (S3) holds and line L3 does nothing.
Otherwise . The parent of is an old node, so , and is the only node of depth more than . By Lemma 1, line L4 finds ; let be its depth, the depth of below , , and the child of on the path to , with .
Claim: . Before was attached, the subtree of had nodes, all at depth at most in the tree, that is at most below . A binary tree of height at most has at most nodes, so . If , that old subtree was the perfect tree of height , in which both children of have nodes. Then after the insertion , a contradiction. So .
By the claim, the rebuilt subtree has height . Its nodes, among them, now have depth at most . The nodes outside the subtree of keep their depths, which are at most . So (S3) holds, and by the paragraph on rebuilding, (S1) holds.
Deletion. If is absent, line L6 returns and nothing changes. Otherwise line L6 is the deletion of a binary search tree: a node with at most one child is replaced by that child, and a node with two children takes the key of its successor, whose node, which has no left child, is replaced by its right child. This keeps (S1). Each node keeps its depth or moves up one level, so no depth grows, and since is unchanged, (S3) holds. Line L7 makes the new count. Now still holds. If , (S2) holds and line L8 does nothing. Otherwise line L8 rebuilds the whole tree and sets , so (S2) holds with equality. The new tree has height , since , and so (S3) holds. If , the tree is empty.
Theorem. After any sequence of operations from the empty tree, the tree holds exactly the keys inserted and not deleted, CONTAINS answers correctly, and the height is at most .
Proof. The cases above show that (S1) to (S3) hold after each operation. CONTAINS is the search of a binary search tree, which is correct by (S1). By (S2), , so .