Proofs: MX-CIF quadtree
These are the human proofs of MX-CIF quadtree, at full length.
The MX-CIF quadtree stores each rectangle at its smallest enclosing cell and reports exactly the rectangles that meet a window full
This proof shows: correctness, termination.
We use the names of the pseudocode. Cells are half-open squares, the root cell is , and rectangles are half-open with integer corners. For a cell of side and a rectangle , write for the quadrant of that holds , or none if no quadrant does; for it is none. The invariant (M) asks: (1) each stored rectangle is stored once, at a node whose cell holds it, with none; (2) the children of a node are its quadrants; (3) each node other than the root stores a rectangle or has a child.
Lemma 1 (the quadrant test). For , the test of the representation returns the quadrant that holds if there is one, and none otherwise. Proof. On the axis, if and only if , and if and only if , since . The same holds on the axis, and a quadrant is the product of one half on each axis.
Lemma 2 (the path of a rectangle). Let be the root cell and while this is not none, ending at . Then is the smallest cell of the quadtree that holds , and every cell of the quadtree that holds is one of . Proof. Cells of the quadtree are nested or disjoint. A cell that holds meets , so ; if and , then lies in one quadrant of , which holds , so it is by Lemma 1 and . Since the sides halve, for some . Also no quadrant of holds , so no cell smaller than does.
INSERT. Lines L1 to L4 follow the path of Lemma 2, making the missing nodes as quadrants of their parents, so part 2 holds. They stop at , after at most steps. Line L5 stores at , which holds and has no quadrant that holds it, so part 1 holds; the code first checks that is not already there, and by Lemma 2 this is the only node where it could be. Each node made on the path now has below it, so part 3 holds.
DELETE. By Lemma 2 and part 1, if is stored, it is at the node of , and all nodes on the path to it exist; so line L11 returns false only if is not stored, and line L12 finds if it is. After removing , parts 1 and 2 still hold, and part 3 can fail only at nodes of the path, since only their sets of rectangles below them changed. Lines L13 and L14 go up the path and remove each node with no rectangle and no child. When the loop stops at a node that stores a rectangle or has a child, every node above has below it, so part 3 holds there. The loop goes up one level each pass, so it stops after at most passes.
Uniqueness. By part 1 and Lemma 2, each rectangle is at the node of its own , which depends only on the rectangle. By part 3, a node exists if and only if its cell is on the path of a stored rectangle. So the tree depends only on the set of rectangles.
WINDOW. Each node is pushed once, by its parent, so the loop stops after visiting each node at most once, and by part 1 no rectangle is reported twice. Each reported rectangle is stored and meets , by line L9. Let be stored at and meet . For each node on the path from the root to , the cell of holds the cell of , which holds , so it meets , and line L7 does not skip . Line L10 pushes all children, so is visited, and line L9 reports .
STAB. If holds and is stored at , then the cell of holds , so it is one of the cells of the quadtree that hold , which form the single path from the root to the cell of side 1 at . The stab walk visits the existing nodes of that path, and each rectangle it reports holds by its test.