Proofs: DFUDS
These are the human proofs of DFUDS, at full length.
Correctness of navigation on DFUDS full
This proof shows: correctness.
Let with and the nodes in preorder. For a node let be the start of its code and the concatenation of the codes of the subtree of , which is a contiguous segment of because preorder lists each subtree contiguously, starting at .
Lemma 1. has one more ) than (, every proper prefix of has at least as many ( as ), and so is balanced. By induction on the height. If is a leaf, . Otherwise for the children . After the excess relative to the start is , and each lowers it by exactly one without going below its starting value minus one, by induction. So the excess relative to the start goes at the ends of the , and is before the last symbol. With the leading (, is balanced.
Lemma 2 (matching). Let have children , , and let be the position of the ) of , so that and is one past the end of . For , the ( at matches the ) at .
Let be the number of ( minus the number of ) in ; the match of is the least with . The positions are (, so there, and . If , the match is . If , then by Lemma 1 each lowers by exactly one and keeps it at or above its starting value inside, so at the end of and before that end. For all these values are at least 1, and at the end of , which is , is 0 for the first time.
Likewise the leading ( at position 0 matches the last position of , by Lemma 1, and .
DEGREE (L1). There are codes before , so the ) ending is the next one, at , and has symbols (.
CHILD (L2-L4). By Lemma 2, .
PARENT (L5-L7). For with parent and , Lemma 2 says the ) at is matched by the ( at , which lies in . The number of ) before that position is the number of codes before , , so starts right after the -th ), or at 1 if that number is 0, by the convention .
CHILD-RANK (L8-L9). With , the ) ending is , so .
SUBTREE-SIZE (L10-L11). is the first position after where the excess falls one below its value at . By Lemma 1, inside the excess stays at or above its value at and falls one below it exactly at the last symbol of . So is the last position of , and since has ( and ) for a subtree of nodes, .
Space. .