Skip to the workspace
Pebellslearn by building
Balanced Trees  ›  Lesson

k-d Trees & Nearest Neighbour

Spatial Trees 28 minSplitting the plane by alternating x/y turns nearest-neighbour search into pruning whole half-planes instead of checking every point

dsa / k-d Trees & Nearest Neighbour

Explore each scene at your own pace.

01 · Snapping a map tap to the nearest pin

Snapping a map tap to the nearest pin

k-d Trees & Nearest Neighbour

Definition
A k-d tree is a binary tree that organises points in space by splitting them at the median along alternating coordinates, such as x and then y. It finds the nearest point to a query by skipping any region that cannot hold anything closer.
In plain words
Picture finding the nearest cafe on a map by repeatedly cutting it in half, first with a vertical line, then a horizontal one, and so on. If a whole half lies farther away than a cafe you have already found, you never look inside it.

In short: every balanced tree so far split on one key. A k-d tree splits a set of points on alternating coordinates — x at the root, y at its children, x again below that — always at the median, so each split keeps the two halves even. The payoff is nearest-neighbour search: instead of checking every point, you can prune a whole half-plane the moment its splitting line alone is already farther away than the best point found so far. No distance check is needed inside a pruned half — it's ruled out by one comparison, not by visiting it.

A square divided by a vertical cut line, each half divided again by a horizontal cut line, with a handful of dots inside the small regions and one dot circled as the answer to a nearest-point query, with an arrow showing one shaded region ruled out without being checked

Snapping a map tap to the nearest pin

A maps app has a few thousand saved pins and needs to answer "which pin did the user just tap near?" many times a second while they drag the map. A k-d tree built once over the pins answers each query by pruning, not scanning:

python

The nearest pin to (9, 2) is (8, 1) — found after visiting only 3 of the 6 pins. The other 3, all on the far side of the root's split line, were ruled out in one comparison each.

Scene 1 of 7
Project context and further resources
You're building a piece ofQuarry — the index layer
This piece — nearest_neighbor(): Finds the closest point on a map by pruning whole regions of the plane.
Scenario Quarry's index layer needs to answer "what's the closest stop to here?" against a real map, without scanning every point for every query.
nearest_neighbor.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
nearest_neighbor(points, query) → dictThe nearest point to query, its distance, and how many k-d tree nodes the pruned search visited versus a brute-force scan.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.