Proofs: Point quadtree
These are the human proofs of Point quadtree, at full length.
The point quadtree keeps the quadrant invariant and range search reports exactly the points in the box full
This proof shows: correctness, termination.
We use the names of the pseudocode. Write . The invariant (Q) says that for each node and each point in the subtree , , and that no point is stored twice.
Lemma 1 (regions). Define and, for a node with point , let , where , , and . If (Q) holds, every point of the subtree of a node lies in . Proof. By induction on depth: a point in the subtree lies in the subtree of , so in , and has , which means .
Search. If (Q) holds and is stored at a node , then at each proper ancestor of , the point is in the subtree with , so the path of INSERT from the root reaches . If is not stored, the path ends at an empty child. Each step goes one level down, so the walk stops after at most steps.
INSERT. If the tree is empty, line L1 makes a one-node tree, which satisfies (Q). Otherwise, by the search argument, line L2 stops if is stored, so no point is stored twice. Each pass of lines L3 to L5 moves one level down, so the loop stops. When line L4 adds as , the nodes whose subtrees change are and its ancestors, all on the path. At each such node , the loop chose the child , and is in the subtree of exactly that child; so (Q) holds at . Other nodes do not change.
RANGE-SEARCH. Let . Each reported point is stored and lies in by line L7, and each node is visited at most once, since the calls follow the tree; with no point stored twice, no point is reported twice. Let be stored at a node , and let be a proper ancestor of , with point , such that is in the subtree . By (Q), . If then , so holds; if then , so holds. In the same way, gives and gives . So the two tests that line L9 makes for child both hold, and the search enters . By induction along the path from the root, the search visits , and line L7 reports . The search visits each node at most once, so it stops.
DELETE. If is not stored, line L10 finds no node, and nothing changes. Otherwise let be its node. After line L12, the tree holds exactly the stored points other than and those of , and it satisfies (Q), since removing a subtree removes points from some subtrees and adds none. Each point of is not in the tree at this moment, so line L13 adds it by INSERT, which keeps (Q) and adds exactly that point. After the loop, the tree holds exactly the old set minus and satisfies (Q). The loop runs times, so it stops.
Balanced build. Let the points of a group, with distinct coordinates, be sorted by , and let be the point at index of a group of . The points before it have and go west; the points after it have and go east. So the two west quadrants together get points and the two east quadrants points, and each child group has at most points. Each child gets exactly the points with , so the tree satisfies (Q), and a node at depth (root at depth 0) has at most points in its subtree, which gives a height of at most .