Sub topic 7
Quadtrees: recursive division into four quadrants
This chapter assumes you know recursion, binary search trees, and basic Python classes. You should also be comfortable with 2D coordinates and axis-aligned rectangles. It covers the structure of a quadtree. The next chapters cover insertion and queries.
The idea
A k-d tree splits the points: each node holds one point and cuts along one axis. A quadtree splits the space. Each node covers a square region and, when it holds too many points, divides that square into four equal quadrants.
This chapter uses a point quadtree with a capacity. Each cell holds up to capacity points. A cell that overflows becomes an internal node with four child cells. Points are stored only in leaves.
What a node stores
Each node needs five things:
- A center point
(cx, cy)and a half-widthhalf. Together they describe the square. - A list of points. It is non-empty only in a leaf.
- A
childrenfield. It isNonein a leaf and a list of four cells in an internal node. - A
capacity, the number of points a leaf may hold before it splits. - A
max_depth, which stops splitting even if the cell is still full.
Storing a center and half-width instead of two corners makes subdividing simple. A child's half-width is half / 2, and its center moves by that amount in each direction.
Boundaries are half-open
Each cell covers the half-open square [cx - half, cx + half) x [cy - half, cy + half). The low edge is included and the high edge is excluded. This matters for points that lie exactly on a dividing line. With closed intervals, a point at x = 50 would belong to two quadrants. With half-open intervals, exactly one child accepts it.
class Quadtree:
def __init__(self, cx, cy, half, capacity=2, max_depth=8):
self.cx, self.cy, self.half = cx, cy, half
self.capacity = capacity
self.max_depth = max_depth
self.points = []
self.children = None
def contains(self, p):
return (self.cx - self.half <= p[0] < self.cx + self.half and
self.cy - self.half <= p[1] < self.cy + self.half)
def make_children(self):
h = self.half / 2
return [
Quadtree(self.cx + dx * h, self.cy + dy * h, h,
self.capacity, self.max_depth - 1)
for dx in (-1, 1) for dy in (-1, 1)
]
root = Quadtree(50, 50, 50)
kids = root.make_children()
for c in kids:
print((c.cx, c.cy), c.half)
edge = (50, 50)
print([i for i, c in enumerate(kids) if c.contains(edge)])The output is:
(25.0, 25.0) 12.5
(25.0, 75.0) 12.5
(75.0, 25.0) 12.5
(75.0, 75.0) 12.5
[3]Wait: the half-width of each child is 50 / 2 = 25.0, so the first four lines actually print 25.0, not 12.5. The correct output is:
(25.0, 25.0) 25.0
(25.0, 75.0) 25.0
(75.0, 25.0) 25.0
(75.0, 75.0) 25.0
[3]The point (50, 50) falls in child 3, the upper-right cell, because that cell's low edge is 50 and includes it.
Why the structure helps
The four children tile the parent exactly, with no gaps and no overlaps. So a point belongs to exactly one leaf, and finding it takes one step per level. A query rectangle can be compared with a cell's square in constant time. If the two do not overlap, the whole subtree under that cell is skipped. This is the pruning from the earlier chapters.
Splitting is driven by density, not by the data's median. Crowded areas get deep, small cells. Empty areas stay as large, shallow cells.
Common errors
- Using closed intervals on both edges, so boundary points are accepted by several children or none.
- Forgetting
max_depth. Many points at the same coordinates can never be separated, so splitting recurses forever. - Storing points in internal nodes and leaves at once, which makes queries visit points twice.
Exercise
A root cell has center (0, 0) and half-width 8. What are the center and half-width of its upper-right child, and which child accepts the point (0, 0)?
Show answer
The child's half-width is 4 and its center is (4, 4). The point (0, 0) goes to the upper-right child, because that child covers the half-open range [0, 8) on both axes and so includes 0.