Proofs: Rope
These are the human proofs of Rope, at full length.
The rope operations keep the invariant and compute concatenation, split and indexing full
This proof shows: correctness.
We use the representation, the invariant (R1) and (R2), and the pseudocode of the page. Write and , and .
Lemma 1 (rotations). Let be a node whose children are balanced trees (each satisfies (R1) and (R2)) with heights differing by at most 2. Then BALANCE() returns a tree satisfying (R1) and (R2) with the same text, whose height is or .
Proof. NODE computes and from the children, so (R1) holds for every node that BALANCE builds. A rotation replaces by or the reverse, which keeps the left-to-right order of the subtrees , so the text is the same. The rest is the standard case analysis of AVL rebalancing after an insertion: if the taller child, say the left one with height against for the right, has its left child of height , one right rotation gives children of heights and or at the root, with all lower nodes balanced; if its left child has height and its right child height , the double rotation gives a root with children of heights and . If the children of differ by at most 1, BALANCE returns .
Lemma 2 (CONCAT). If and satisfy (R1) and (R2), CONCAT(, ) returns a tree that satisfies (R1) and (R2), with and , unless one of them is NIL, in which case is the other one.
Proof. By induction on . Line L5 is clear. In line L6, the new node is balanced and has height . (Merging two leaves into one leaf when their total length is at most is also correct: the text is the concatenation, and the height 0 is the maximum.) In line L7, , so is an inner node, and ; the difference is smaller than , so by the induction hypothesis $c' = $ CONCAT(, ) is balanced, has text , and has height or . Since and , the heights of and differ by at most 2, and Lemma 1 applies. The text of NODE(, ) is . Its height is and at least , and BALANCE lowers it by at most 1 only when the node was unbalanced, which can happen only when , so the result has height at least . Line L8 is symmetric.
Lemma 3 (SPLIT). If satisfies (R1) and (R2) and , SPLIT(, ) returns , both satisfying (R1) and (R2), with and .
Proof. By induction on the height of . For a leaf, line L9 cuts its string; the pieces are nonempty or dropped as NIL, and shorter than , so (R1) holds. For an inner node with , the induction hypothesis splits into and at , and by Lemma 2, CONCAT(, ) is balanced with text . The case is symmetric, with the offset into , which is in range since .
CHAR-AT. The loop keeps the claim that the answer is character of and . If , character of is character of ; otherwise it is character of , which is less than . By (R1) the stored lengths are correct. At a leaf, line L4 returns the character. The loop ends because the depth decreases.
INSERT and DELETE. By Lemma 3, line L12 gives and with split at ; BUILD() is a perfectly balanced tree, where the two halves of each node differ by at most one leaf and so their heights differ by at most 1, with leaves of length at most ; and by Lemma 2 the two concatenations give a balanced tree with text . For DELETE, the two splits give with the first characters and with the characters from position on, and line L15 joins them. No operation modifies an existing node, so all earlier ropes keep their texts.