Sub topic 14
Point, Region and PR Quadtrees, and Octrees
This chapter assumes you have read the earlier chapters on quadtree insertion and range queries. You should be comfortable with recursion and with the capacity-based Quadtree class from those chapters.
"Quadtree" names a family of structures, not one. They all divide a 2D area into four quadrants, but they differ in what they store and where they cut.
Three kinds of quadtree
A point quadtree stores one data point in each node. That point is also the center of the split. Its four children cover the regions above-left, above-right, below-left and below-right of the point. It works like a binary search tree in two dimensions, so its shape depends on insertion order. Sorted input can produce a long chain.
A region quadtree stores no points. It divides an area into equal quadrants until each block is uniform, for example all one color in a bitmap. Image compression and map rasters use it. A leaf says "this whole block has value V."
A PR quadtree (point-region quadtree) cuts each cell at its geometric midpoint, whatever the data. Points live only in leaves. Because the cuts are fixed, the same set of points gives the same tree in any insertion order. The Quadtree class you built earlier is a PR quadtree with a bucket size, since each leaf holds up to capacity points.
| Variant | Where it cuts | What leaves hold | Shape depends on insertion order? |
|---|---|---|---|
| Point quadtree | At a stored data point | Nodes hold one point each | Yes |
| Region quadtree | At cell midpoints | A uniform value for the block | No |
| PR quadtree | At cell midpoints | Up to `capacity` points | No |
Octrees
An octree is the 3D version of the PR quadtree. Each cell is a cube that splits into eight octants. Games, collision detection and point-cloud processing use octrees.
The only real change is how you pick a child. In 3D you compare the point with the cell center on each axis and combine the three results into a number from 0 to 7.
def octant(center, p):
# One bit per axis: 1 if the point is at or above the center on that axis.
return sum(int(p[a] >= center[a]) << a for a in range(3))
def child_center(center, half, i):
# Move a quarter of the parent's width toward the chosen octant.
h = half / 2
return tuple(c + (h if (i >> a) & 1 else -h) for a, c in enumerate(center))
center, half = (50, 50, 50), 50
i = octant(center, (70, 20, 80))
print(i) # 5, binary 101: x high, y low, z high
print(child_center(center, half, i)) # (75.0, 25.0, 75.0)The same bit trick works for any number of axes. With k axes a cell has 2^k children, so a quadtree has 4 and an octree has 8. At 10 dimensions a cell would have 1,024 children, and most would be empty. This is one reason k-d trees, which always split in two, handle higher dimensions better.
Gotchas
- Duplicate points. If more than
capacitypoints share the same coordinates, no amount of subdividing separates them. Cap the depth, as themax_depthparameter does in the earlier code, and let the deepest leaf grow past its capacity. - Clustering. A PR quadtree cuts at fixed midpoints, so tightly clustered points create deep chains of mostly empty cells. A point quadtree adapts to the data but can become unbalanced.
- Half-open cells. Use
lo <= x < hifor cell bounds so a point on a shared edge belongs to exactly one child.
Exercise
You index 50,000 GPS points that are concentrated in a few dense city centers, and you want a tree whose shape does not depend on the order in which points arrive. Which quadtree variant fits, and what problem should you guard against?
Show answer
A PR quadtree fits, because it cuts at fixed cell midpoints and so gives the same tree for any insertion order. Dense clusters will produce deep subtrees, and duplicate or near-duplicate points can cause endless subdivision, so set a maximum depth.