Suppose you have a million points scattered across a map and someone drops a pin. Which point is closest? The obvious answer is to measure the distance to all million and keep the smallest. It works, but it is brute force — every query touches every point.
The k-d tree, invented by Jon Louis Bentley in 1975, does something cleverer. It organizes the points by repeatedly slicing space along one axis at a time: split by the x-coordinate, then by the y-coordinate, then x again, alternating as you go down the tree. Each cut puts roughly half the points on each side.
Once space is carved up this way, a search can prune: if an entire region lies farther away than the best point found so far, the whole branch is skipped without ever looking inside. This is not a guess or a heuristic — the geometry guarantees nothing better hides in there.
Comments
Loading comments...