Proofs: Rooted tree representation (left-child right-sibling)
These are the human proofs of Rooted tree representation (left-child right-sibling), at full length.
The left-child, right-sibling operations keep the invariant, and PREORDER visits the subtree in preorder full
This proof shows: correctness, termination.
Claim. If (T1) to (T4) hold for all nodes before an operation, then: NEW-NODE, ADD-CHILD (with a root and not in the subtree of ) and DETACH keep them, and change the trees as the tree type says; CHILDREN visits the children of from left to right; and PREORDER visits each node of the subtree of exactly once, in preorder, stops, and makes at most pointer moves, where is the size of the subtree.
Setup. The preorder of the subtree of , with children , is . The invariant is (T1) to (T4) of the page: the child list of starts at , follows right-sibling pointers through the children in order and ends with nil, each child points to with , and a root has nil in and right-sibling.
Lemma 1 (ADD-CHILD). Let have children . After lines L2 to L4, , (nil when ), and . So the child list of is , which gives (T1) to (T3) for . The only fields written are two fields of and one of . Before the call, was a root, so it was in no child list, and by (T4) its two written fields were nil; the child lists of all other nodes are unchanged. Since is not in the subtree of , the new edge from to closes no cycle, so the result is again a forest, in which the tree of now contains the subtree of as the leftmost subtree of .
Lemma 2 (DETACH). Let . If is nil, is a root and nothing changes. Else let the child list of be with . If , line D2 sets (nil when ). If , the loop of line D4 starts at and follows the list; it stops at , the first node whose right sibling is , after steps, because the list is finite by (T2) and contains . Line D5 sets . In both cases the child list of is now . Line D6 gives (T4) for . The fields of the nodes in the subtree of other than are not touched, so is now the root of its old subtree. The cost is .
Lemma 3 (CHILDREN). By (T1) and (T2), the variable takes the values , nil, in this order: at the -th test of line L6, (and $c = $ nil for ), by induction on with line L7. So line L7 visits the children in order and the loop stops after steps. NEW-NODE makes a root with no child, which satisfies (T1) and (T4).
Lemma 4 (the successor in preorder). Let be a node in the subtree of , and let be the node that follows in , or none.
- If has a child, .
- If is a leaf, let be the first node on the path that is or has a right sibling. If , then is the last node of . Else .
Proof. Part 1 follows from , which is a contiguous part of (by induction on the depth of below , from the recursive definition), and starts with . For part 2, a leaf is the last node of . For any node in the subtree, let be the child of . If , the part in is followed by , which starts with . If , has no right sibling, and the last node of is also the last node of . Apply this along the path from : as long as the current node has no right sibling, the last node of , which is , is also the last node of . At : if , is the last node of ; else is the last node of and is followed by .
Lemma 5 (PREORDER). Let with . At the -th test of line L9, for , and $x = $ nil at the test number .
Proof. By induction on . Line L8 gives . Assume at the -th test, . Line L10 visits . If has a child, line L11 sets to by Lemma 4, part 1. Else the loop of line L12 moves up the path from and stops at the first node that is or has a right sibling, which is of Lemma 4, part 2. It stops because each step moves to the parent, the depth below drops by exactly 1, and is on the path. Line L13 sets if , and $x = $ nil if , which by Lemma 4 happens exactly when .
So PREORDER visits , each once, in preorder, and stops.
Lemma 6 (cost of PREORDER). Each node with is reached exactly once by line L11 or line L13, by Lemma 5. A climb in line L12 leaves a node whose subtree is finished: by Lemma 4, all nodes visited after it lie outside , since is contiguous and its last node was visited before the climb. A second climb from would need again, which is a node of , and line L12 only moves to ancestors of the last visited node, while lines L11 and L13 move it to the next node of the preorder. So each node other than is climbed from at most once, and the walk makes at most moves. With one visit per node, the time is , and the extra space is the one variable .
Conclusion. Lemmas 1 to 3 show that each update keeps (T1) to (T4) and that CHILDREN lists the children in order, and Lemmas 5 and 6 show that PREORDER is correct and runs in time with extra space. By induction on the number of operations, any sequence of operations from single-node trees keeps the invariant.
Back to Rooted tree representation (left-child right-sibling)