Proofs: Linear quadtree
These are the human proofs of Linear quadtree, at full length.
The linear quadtree keeps the list of maximal black blocks in Z-order full
This proof shows: correctness, termination.
We use the names of the pseudocode. For a block let , the set of codes of its pixels. The invariant (L) asks: (1) and ; (2) the intervals are disjoint and the list is sorted by ; (3) no four blocks , , with , are all in the list. The image of the list is the set of pixels whose codes lie in some .
Lemma 1 (cells are intervals). For , the codes are exactly the codes of the pixels of one aligned square of side , and is the disjoint union of , . Two such intervals are either disjoint or nested. Proof. The code of has the bits of and at the positions and . Fixing all bits of position and above fixes the bits of and , so it fixes an aligned square of side , and leaves the lower bits free, which is the interval . The bits at positions and give the four children in order. Two intervals of the form with are dyadic in base 4, so they are disjoint or nested.
Lemma 2 (uniqueness). For a set of black pixels, there is exactly one list satisfying (L) whose image is : the black leaves of the region quadtree of . Proof. Call a cell maximal black if it is all black and its parent is not (or it is the root). The maximal black cells are disjoint by Lemma 1, they cover , and they are the black leaves of the region quadtree. Let a list satisfy (L) with image . Each block is a black cell. Suppose some block is not maximal, so its parent is all black. No block contains , since it would overlap ; so by Lemma 1 every block that meets lies inside , and since is black these blocks tile . Let be one of them of the least level ; its parent lies inside . A block that meets a child of is inside , so it has level at least ; if its level were more than it would contain , by Lemma 1, and overlap . So each of the four children of is tiled by blocks of level exactly , that is, each child is a block, which part 3 forbids. Hence each block is maximal black. Each maximal black cell is tiled by blocks, which are maximal black cells and so cannot lie strictly inside it; so it is a block.
LOCATE. By part 2, the intervals are sorted and disjoint. If , then and every later block starts after , so is the last index with , which line L7 finds, and line L8 accepts it. If line L8 accepts the last index with , then . So LOCATE returns the block that holds , or none if is white.
BUILD. We claim that after each pass of lines L1 to L6, the stack, read from bottom to top, satisfies parts 1 and 2, has as image the pixels read so far, and satisfies part 3. Each block on the stack ends at or before the last code read, since a block is pushed as one pixel and a merge replaces four consecutive intervals by their union (Lemma 1). Line L2 pushes , which is larger than all earlier codes, so parts 1 and 2 hold. Line L4 merges the top four only when they are the four children of one cell, so the image does not change, and part 1 holds for the parent, and part 2 holds since the union of four consecutive intervals is an interval in the same place. For part 3, suppose four siblings are on the stack. They are consecutive in the sorted stack, since the blocks between them would lie in the parent's interval, and the last of them, by order of codes, was pushed or made last. At that moment the four were the top four, and lines L3 and L4 merged them. A merge makes one new block on top, and the loop goes on testing the new top four, so no complete group stays on the stack. Each code is pushed once and each merge removes three blocks, so the loop stops. By Lemma 2, the result is the list of black leaves.
INSERT. If line L10 finds black, nothing changes. Otherwise, is disjoint from all blocks, and line L11 adds at its sorted place, so parts 1 and 2 hold and the image is . Part 3 can now fail only for a group that contains a block that holds , since every other group was in the list before. Line L12 checks the group of the current block , which is the only block that holds . If the group is complete, line L13 replaces the four blocks by their parent, which does not change the image and keeps parts 1 and 2 by Lemma 1, and the new block is the only one that holds . If the group is not complete, or , part 3 holds for all groups. Each pass raises , so the loop stops after at most passes.
DELETE. If is white, nothing changes. Otherwise let be the block that holds . For , let be the cell of level that holds , so and . By Lemma 1, is the union of the three other children of , and so
a disjoint union of the blocks that lines L16 and L17 add. They lie inside the removed block, so they are disjoint from the other blocks and from each other, parts 1 and 2 hold, and the image is . For part 3, a group made only of old blocks was there before. A group that contains a new block of level is the four children of , and one of them, , holds the white pixel , so it is not a block. Any other group that changed would contain a part of the removed block, so it is one of these. Hence part 3 holds. The loop runs times.
Blocks in a cell. For an aligned cell with codes , Lemma 1 says each block is inside , contains , or is disjoint from it. If a block contains , it holds , and LOCATE finds it. Otherwise the blocks inside are exactly those with , a run of the sorted list.