Data structures / K-d trees

K-d trees

How to find the nearest point on a map without measuring to every point: cut the plane in half, then each half the other way, and skip whole regions.

Intermediate7 steps~7 min
Point — a stored location, A to G
Query q — where we are asking from
Split line — cuts a region at its median point
Best circle — radius = best distance so far
// THE SHORT VERSION

In 30 seconds

A k-d tree organises points in space so that “what is nearest to here?” can be answered quickly. It splits the points in half with a line through the median point, then splits each half along the other axis, and so on.

A search walks down to the small region holding the query, takes the point there as a first guess, then climbs back. Any region whose boundary is farther than the best distance found is skipped whole.

It works well in two or three dimensions and poorly in dozens.

K-d trees, step by step

STEPS01 / 07

Leaves would split on x again; with no children those lines are left out. The two trees in the last step are an illustration of the trend, not a measured run.

STEP 01 / 07 Seven points, one question
THE MATH
q = (8, 6) nearest of 7 points? brute force: n distances
  1. Step 1: Seven points, one question. Seven points lie on a plane and we want the one closest to q, as a map app does for “nearest café”. The obvious way measures the distance to every point. That is fine for seven and far too slow for millions, asked thousands of times a second.
  2. Step 2: Split at the median x. Sort the points by x and take the middle one, D. It becomes the root of the tree, and a vertical line through it cuts the plane in two: three points on its left, three on its right. Using the median keeps the two halves the same size.
  3. Step 3: Alternate the axis. Each half is split again, this time by y. On the left the middle y belongs to C, on the right to G. Their children are single points, so building stops. Switching axis at every level is the “k-d” idea: k dimensions, taken in turn.
  4. Step 4: Search: walk down. To find the nearest point to q, first walk down as if inserting q: right of D, then below G’s line. That ends at leaf F, 4 units away. F is only a first guess, but it gives a radius: anything better must lie inside that distance.
  5. Step 5: Back up: a closer point. Now climb back up, checking each node passed. G is only 1.41 away, so it becomes the best. Draw a circle of that radius around q: it pokes across G’s line, so a closer point could hide above it. We must look there, and find E at 3.16. No improvement.
  6. Step 6: Prune the other half. Back at the root, D itself is 3.61 away. Its line x = 5 is 3 units from q, more than the 1.41 radius, so the circle stays entirely on the right. Nothing on the left can be closer, and A, B and C are skipped without a single measurement. That skipping is called pruning.
  7. Step 7: Weak in many dimensions. Pruning needs the circle to stay on one side of most lines. With many coordinates, each split uses only one of them, and nearly everything sits at a similar distance, so the ball crosses almost every split and the search opens most of the tree. With a few dozen dimensions it is often no faster than checking every point.
VALUES SO FAR
pointsn = 7input
q(8, 6)query
root····split
depth 1····split
guess····search
best····result
pruned····skipped
limit····rule
// AFTER

Learn next

Nothing builds on this one yet. See every data structures concept.

Spotted a mistake on this page?