Split the map into four squares again and again, only where there are many points.
A quad tree is a tree that splits a map into four equal squares, and then splits any square that is too crowded into four smaller squares, again and again.
Imagine a square map with lots of dots on it. A fixed grid (like graph paper) wastes effort: in a city, one grid square holds thousands of dots, while in a desert thousands of squares are empty. A quad tree fixes this by making the squares adapt to the data.
The rule is simple. Each square (a node) can hold a small number of points, called its capacity, for example 4. When a fifth point arrives, the square splits into four equal quarters (top-left, top-right, bottom-left, bottom-right), and its points move into the right quarters. If a quarter is still too crowded, it splits again.
To search, you start at the top and ask each square: “do you touch my search box?” If not, the whole square (and everything inside it) is skipped. If yes, go into its four children. At the bottom you check the few points inside.
| Kind | What it splits | Used for |
|---|---|---|
| Point-region (PR) quad tree | Always at the middle of the square | Points, such as places and drivers. This is the one in the visualizer. |
| Point quad tree | At the position of a point | Points; the shape of the tree depends on insert order |
| Region quad tree | A grid of values, split until each part is the same | Images and raster maps |
| Octree | A cube into 8 cubes | 3D space |
Green box = your search. Cyan squares = looked inside. Green dots = found. Crowded areas get small squares, empty areas stay as one big square.
A real point-region quad tree: a square splits into 4 equal squares when it holds too many points. To match the finest square of this tree everywhere, a fixed grid would need 1,024 cells. The point sets are fixed (made with a seeded generator), so every visit shows the same data.
| Term | Meaning in easy words |
|---|---|
| Node | One square in the tree |
| Root | The top square that covers everything |
| Leaf | A square that has not split and holds the points |
| Child / quadrant | One of the four equal parts of a square (NW, NE, SW, SE) |
| Capacity | How many points a leaf may hold before it splits |
| Depth | How many splits you passed from the root |
| Max depth | A limit that stops splitting |
| Range query | Find every point inside a box or circle |
| Nearest neighbour | Find the point closest to a given spot |
| Octree | The 3D version, with 8 children per node |
| Pruning | Skipping a square because it cannot contain a result |
| Choice | We gain | We lose |
|---|---|---|
| Small capacity | Fewer points to check per leaf | A deeper tree with more nodes |
| Large capacity | A small, shallow tree | More points to check in each leaf |
| High max depth | Very fine detail in dense spots | More memory, risk of long chains |
| Low max depth | Bounded size and speed | Crowded leaves that are slow to scan |
| Quad tree | Adapts to the data, supports updates | Hard to keep on disk |
| Geohash / fixed grid | Easy to store and shard | Does not adapt: busy cells stay busy |