Spatial Partitioning via Alternating Hyperplanes
A $k$-d Tree ($k$-dimensional tree) is a binary space-partitioning tree where each internal node at depth $d$ splits space with an axis-aligned hyperplane perpendicular to coordinate axis $a = d \bmod k$. Choosing the median point along axis $a$ guarantees a balanced tree of height $\lceil \log_2(n + 1) \rceil$ built in $O(n \log n)$ time.
Branch-and-Bound Hypersphere Pruning
Given a query point $q$, how do we find its exact nearest neighbor without scanning all $n$ points?
- Maintain a running candidate ball of radius $R = \text{bestDist}$ centered at $q$.
- At any splitting node $p$ on axis $a$, compute the perpendicular distance from $q$ to the splitting hyperplane: $$\Delta_a = |q[a] - p[a]|$$
- Always search the near half-space first (the side of the hyperplane containing $q$). This rapidly shrinks $R$.
- Prune the far half-space whenever $\Delta_a \ge R$ (equivalently $\Delta_a^2 \ge R^2$, avoiding square roots). Geometrically, if the query hypersphere of radius $R$ does not intersect the splitting hyperplane, no point on the far side can possibly be closer than our current best!
Curriculum Connections
- Spatial Indexing: k-d Trees, Quadtrees & R-Trees
- Backtracking, Pruning & Branch-and-Bound
- Quickselect & Median-of-Medians (BFPRT)
Guided Implementation Walkthrough (4 Progressive Steps)
Step 1: Alternating-Axis Median Partitioning
- Core Mechanical Concept: Alternating-Axis Median Partitioning
- Target Module:
index.js(Step1) - Engineering Specification: At depth $d$, select splitting axis $\text{axis} = d \bmod k$, sort points along that coordinate, pick the median as the splitting hyperplane, and recursively build left and right subtrees.
- Verification Invariant: verifies Step 1: Alternating-Axis Median Partitioning
Step 2: Orthogonal Bounding-Box Range Search
- Core Mechanical Concept: Orthogonal Bounding-Box Range Search
- Target Module:
index.js(Step2) - Engineering Specification: Implement
rangeSearch(minBounds, maxBounds)by pruning the left half-space whenminBounds[axis] > node.point[axis]and pruning the right half-space whenmaxBounds[axis] < node.point[axis]. - Verification Invariant: verifies Step 2: Orthogonal Bounding-Box Range Search
Step 3: Branch-and-Bound Nearest Neighbor Pruning
- Core Mechanical Concept: Branch-and-Bound Nearest Neighbor Pruning
- Target Module:
index.js(Step3) - Engineering Specification: Always recurse into the
nearchild containingtargetfirst to shrinkbestSqquickly. Only recurse into thefarchild if the perpendicular distance to the splitting hyperplane satisfies $(target[axis] - node.point[axis])^2 < bestSq$. - Verification Invariant: verifies Step 3: Branch-and-Bound Nearest Neighbor Pruning
Step 4: Bounded k-Nearest Neighbors (k-NN)
- Core Mechanical Concept: Bounded k-Nearest Neighbors (k-NN)
- Target Module:
index.js(Step4) - Engineering Specification: Generalize pruning to $k$ nearest neighbors by comparing the hyperplane distance against the $k$-th best squared distance collected so far.
- Verification Invariant: verifies Step 4: Bounded k-Nearest Neighbors (k-NN)
Asymptotic & Memory Layout Guarantees
- Category: Computational Geometry & Spatial Indexing
- Difficulty: Advanced (~45 minutes)
- Key Architectural Highlights:
• Alternating-axis median partitioning building a balanced k-d tree
• Branch-and-Bound Nearest Neighbor pruning the far half-space when axisDist^2 >= bestDist^2
• Bounded Max-Heap k-Nearest Neighbors (k-NN) and orthogonal hyper-rectangle range queries - Tags & Primitives:
k-d Tree,Spatial Indexing,Branch-and-Bound k-NN,Orthogonal Range Search,Hypersphere Pruning
Production Systems Context & Verification
This from-scratch implementation is engineered with zero external dependencies and deterministic verification harnesses. Every internal invariant—from pointer restoration and bitmask addressing to amortized potential bounds and tree height balance—is asserted directly in test.js. Use the interactive browser workbench above to complete the guided steps in Guided Lab mode or inspect and benchmark the complete reference architecture in Full Solution mode.