Proofs: Region quadtree
These are the human proofs of Region quadtree, at full length.
The region quadtree operations keep a unique maximal tree of the image full
This proof shows: correctness, termination.
A block is uniform if all its pixels have one color. For a block and an image , let be the tree defined by: if is uniform with color , a leaf of color ; else an internal node whose children are for the quadrants of . A block of side 1 is uniform, so is well defined by induction on the side.
Lemma 1 (uniqueness). A tree for a block satisfies (Q) for exactly when it equals . Proof. By induction on the side of . For , by part 1 the only tree is a leaf with the color of the pixel. Let and let satisfy (Q). If is a leaf, part 1 says that is uniform, and . If is internal, each child satisfies (Q) for its quadrant, so by induction the children are . If were uniform with color , each quadrant would be uniform, each child a leaf of color , and part 2 would fail. So is not uniform, and . Conversely, satisfies part 1 by its definition, and part 2 because, if all four children of an internal node are leaves of color , all four quadrants are uniform with color , so is uniform, and would be a leaf.
Lemma 2 (merge). If equal , the merge step of lines L4 and L5 returns . Proof. is uniform with color exactly when each is uniform with color , that is, exactly when each is a leaf of color . In that case the step returns a leaf of color ; else it returns the internal node with the , and both cases are .
BUILD. Claim: after the round with side (and at the start, for , by line L1), the node kept for each block of side is of that block. The induction step is Lemma 2 applied in line L3 to each block of side . The loop stops when , after rounds, and the node of the whole image is of the image. By Lemma 1 it satisfies (Q).
GET. The block of the child chosen in line L7 holds : the pixel is in the right half exactly when , and in the bottom half exactly when . So each node on the path holds the pixel, and the side halves each step, so the loop stops at a leaf after at most steps. By part 1 of (Q), its color is the color of .
SET. If the pixel already has color , the image does not change and the tree is kept. Else, replacing a leaf of color and side by an internal node with four leaves of color (line L11) keeps part 1, since each pixel keeps its color. Lines L10 and L11 go down to the leaf of side 1 at , and line L12 sets it to , so after line L12 part 1 holds for the new image . Part 2 can now fail only at nodes on the recorded path, since only they changed. Let the path be the blocks , from the quadrant of the root down to the pixel's block of side 1. Each child that is not on the path is of its block for : it is a subtree that was not changed, or a leaf made by a split, which is uniform with its color in since the only changed pixel is on the path; and in both cases it satisfies (Q), so it is of its block by Lemma 1. The node for is a leaf of side 1, which is . Line L13 goes up: if the node for is , then the node for has four children equal to of its quadrants, and the merge of Lemma 2 makes it . By induction up to the root, the tree is of the image and satisfies (Q). The path has blocks, so SET stops.
UNION. Claim: for nodes and of two images, UNION returns , where is the pixelwise OR. By induction on the side of . If either is a black leaf, every pixel of is black in , and a black leaf is (line L14). If is a white leaf, equals on , and is the answer (line L15); the same for . Else both are internal, the side is at least 2, and by induction the four recursive results are of the quadrants; Lemma 2 gives (line L16). INTERSECTION is the same with black and white swapped, since the AND with a white block is white and the AND with a black block is the other block (line L17). Each call goes down one level, and there are at most levels, so both stop.