Sub topic 9
Rectangular Range Queries on a Quadtree
This chapter assumes you have read the previous chapters on quadtree structure and insertion. You should be comfortable with recursion and with axis-aligned rectangles.
The idea
A range query asks: which stored points lie inside a rectangle? A linear scan tests every point. A quadtree tests whole cells first. If a cell's square does not overlap the query rectangle, no point inside it can match, so the query skips that cell and everything below it.
The query procedure
For each cell the query follows three steps:
- Compare the cell's square with the query rectangle. If they do not overlap, return immediately.
- Test each point stored in this cell against the rectangle and keep the matches.
- Recurse into each child cell, if the cell has been subdivided.
Full code
This is the same quadtree as in the insertion chapter, with the query method in use.
class Quadtree:
def __init__(self, cx, cy, half, capacity=2, max_depth=8):
# Covers the half-open square [cx-half, cx+half) x [cy-half, cy+half)
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 _subdivide(self):
h = self.half / 2
self.children = [
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)
]
old, self.points = self.points, []
for p in old:
any(c.insert(p) for c in self.children)
def insert(self, p):
if not self.contains(p):
return False
if self.children is None:
if len(self.points) < self.capacity or self.max_depth == 0:
self.points.append(p)
return True
self._subdivide()
return any(c.insert(p) for c in self.children)
def query(self, xmin, ymin, xmax, ymax, found=None):
if found is None:
found = []
if (xmax < self.cx - self.half or xmin >= self.cx + self.half or
ymax < self.cy - self.half or ymin >= self.cy + self.half):
return found
for p in self.points:
if xmin <= p[0] <= xmax and ymin <= p[1] <= ymax:
found.append(p)
if self.children:
for c in self.children:
c.query(xmin, ymin, xmax, ymax, found)
return found
qt = Quadtree(50, 50, 50)
for p in [(10, 10), (12, 11), (80, 80), (55, 60), (11, 12), (13, 9)]:
qt.insert(p)
print(sorted(qt.query(0, 0, 20, 20))) # [(10, 10), (11, 12), (12, 11), (13, 9)]Reading the overlap test
The if at the top of query checks four ways the rectangles can be separate. The query is entirely left of the cell, right of it, below it, or above it. If none of these holds, the rectangles overlap.
The two sides use different comparisons because the shapes differ. A cell is half-open, so its right edge cx + half belongs to the neighbor. A query rectangle is closed, so its edges count. A query with xmin equal to a cell's right edge therefore skips that cell, and a query with xmax equal to its left edge still enters it.
Tracing the example
The query rectangle is (0, 0) to (20, 20).
- The root covers
[0, 100)in both axes, so it overlaps the query. It holds no points, because it subdivided. - The child centered at
(75, 75)covers[50, 100). Herexmax = 20 < 50, so the whole cell is skipped. This is where the pruning pays off, since(80, 80)and(55, 60)are never examined. - The child centered at
(25, 25)overlaps the query. The query descends through deeper cells until it reaches the leaves holding the four points, and each point passes the closed-interval test.
Cost
The work is the number of cells whose squares overlap the query, plus the points tested inside them. A small rectangle over evenly spread points touches few cells. A rectangle that covers most of the space visits most of the tree, so it costs about as much as a linear scan.
Exercise
A quadtree covers [0, 100) x [0, 100). You call query(50, 0, 50, 99). The query rectangle has zero width, with xmin equal to xmax. Does the cell [0, 50) x [0, 100) get entered, and does the cell [50, 100) x [0, 100) get entered?
Show answer
The cell [0, 50) is skipped. Its test is xmin >= cx + half, which is 50 >= 50, so it is true. The cell [50, 100) is entered, because xmax (50) is not less than its left edge (50). The half-open cell edges mean the line x = 50 belongs only to the right cell.