Skip to content

docs: Write section 9.1 of the book: nearest neighbor search with a kd-tree - #284

Open
hskl18 wants to merge 1 commit into
ShisatoYano:mainfrom
hskl18:docs/204-kd-tree-section
Open

hskl18 wants to merge 1 commit into
ShisatoYano:mainfrom
hskl18:docs/204-kd-tree-section

Conversation

@hskl18

@hskl18 hskl18 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #282. Part of #204.

Adds the theory layer of section 9.1 as doc/perception/kd_tree.md, following the writing guide and the sections of chapter 4:

  • Building the tree: the median split on the axis depth % k, and why the tree has $\lfloor \log_2 n \rfloor + 1$ levels.
  • Searching it: the nearest neighbor search and the radius search, both with pruning. The page shows why no point on the far side of a splitting line can be closer than the line, which gives the test for skipping a subtree.
  • Limits: a note that the current KdTree visits every node until Skip subtrees in the kd-tree search #281 is fixed, the worst case, many dimensions, and rebuilding the tree for every scan.

Two figure scripts in doc/perception/figures/ draw a tree of seven points (kd_tree_build.py) and one search in it with the skipped cells shaded (kd_tree_pruning.py). The second script runs the search it draws. The page is linked on the chapter page, whose note now says the chapter is being written, and the new symbols are in a Perception table of Notation and Units.

How I checked it works

  • Locally on Linux with Python 3.13: sphinx-build -W --keep-going -b html doc doc/_build/html builds without warnings, pytest test/test_book_chapters.py passes, and the whole suite passes (138 passed). I didn't run macOS or Windows.
  • Read the built page in Chrome: equations (1) to (8) render with their tags, the simulation image and both figures load, the page is under chapter 9 in the sidebar, and the lines of the Algorithm block fit its width. I didn't open the three folded answers.
  • The numbers in the text are computed. kd_tree_pruning.py asserts its result against comparing every point, and the same search with the query point of question 1 gives the answer written there.
  • The level counts come from the tree that KdTree builds from random points in the unit square: 7, 10 and 14 levels for 100, 1000 and 10000 points. On those trees, a pruned search I wrote for the check visited 11.1, 15.8 and 19.5 nodes on average (1000 queries each), and every result equals the brute-force one.
  • Both DOIs resolve to the cited papers. I cite [2] only for the logarithmic expected search time, which is its title; I didn't re-read the paper for this.

Images

Book text only, no change under src/.

Book

  • This PR doesn't change code that the book describes, or I updated those pages

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Book section 9.1: kd-tree search

1 participant