{
  "id": "kd-tree",
  "title": "KD-tree",
  "lang": "en",
  "source_lang": "en",
  "summary": "A KD-tree is a binary spatial data structure that partitions multidimensional points using axis-aligned splits to accelerate range searches and nearest-neighbor queries.",
  "category": "computer-science",
  "subcategory": "algorithms-theory",
  "type": "concept",
  "aliases": [
    "k-d tree",
    "kd tree",
    "k-dimensional tree"
  ],
  "generated_at": "2026-11-04",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/kd-tree/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/kd-tree/",
      "json": "https://aiwiki.page/zh/kd-tree.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "A KD-tree (short for k-dimensional tree) is a data structure for organizing points in a multidimensional coordinate space. It recursively partitions space using hyperplanes perpendicular to coordinate axes, placing points on opposite sides of a split into different subtrees. This organization allows searches to exclude regions that cannot contain relevant results. KD-trees are widely used in computational geometry, machine learning, and computer graphics, particularly for low-dimensional range searching and nearest-neighbor queries."
    },
    {
      "heading": "Structure and spatial partitioning",
      "text": "A KD-tree is a binary tree. Each internal node identifies a splitting coordinate and a splitting value. For points \\(x=(x_1,\\ldots,x_d)\\), a split on coordinate \\(j\\) at value \\(s\\) separates points according to a rule such as\n\n\\[x_j < s \\qquad\\text{or}\\qquad x_j \\ge s.\\]\n\nThe corresponding geometric boundary is the hyperplane \\(x_j=s\\). Successive splits divide the space into axis-aligned regions. In two dimensions these regions are rectangles, possibly unbounded; in three dimensions they are rectangular boxes.\n\nTwo common representations are:\n\n- Point-at-node trees: each node stores a point as well as the information needed to split the remaining points.\n- Bucket-leaf trees: internal nodes store splitting information, while leaves store small groups of points.\n\nIn a classical construction, the splitting coordinate cycles through the dimensions. A two-dimensional tree alternates between the horizontal and vertical coordinates. Other constructions choose the coordinate with the largest spread or use a rule intended to improve the shapes of the resulting regions.\n\nDespite its name, a KD-tree is not a tree with \\(k\\) children per node: it normally has at most two children. The letter \\(k\\) denotes the number of dimensions. It generalizes aspects of a binary search tree, but different levels can compare different coordinates, so it does not impose a single global ordering of points."
    },
    {
      "heading": "Construction",
      "text": "A common static construction uses divide and conquer:\n\n- Choose a splitting coordinate.\n- Find a median point or median coordinate value.\n- Divide the points into two approximately equal subsets.\n- Construct each subtree recursively.\n- Stop when a subset is sufficiently small to become a leaf.\n\nWith a suitable tie-breaking rule, median splitting produces a tree of height \\(O(\\log n)\\), where \\(n\\) is the number of points. Equal coordinate values require care: assigning every point equal to the split value to the same side can prevent an otherwise median-based construction from remaining balanced.\n\nUsing linear-time median selection at each level gives an \\(O(n\\log n)\\) construction for fixed dimension. Sorting each subset from scratch at every recursive call instead gives a straightforward \\(O(n\\log^2 n)\\) construction. Presorting and maintaining coordinate orderings can also support efficient construction, with additional bookkeeping.\n\nA balanced tree is not necessarily a geometrically effective tree. Median splits balance the numbers of points, but may produce long, thin regions when data are unevenly distributed. Midpoint-based and sliding-midpoint rules instead emphasize spatial subdivision, sometimes at the expense of population balance.\n\nA two-dimensional example\n\nConsider the points\n\n\\[(2,3),\\ (5,4),\\ (9,6),\\ (4,7),\\ (8,1),\\ (7,2).\\]\n\nA construction might first split on the first coordinate near its median. Within each resulting subset, it then splits on the second coordinate. Later levels return to the first coordinate.\n\nA query near \\((8,2)\\) initially descends toward the region containing points with larger first coordinates and smaller second coordinates. After finding a nearby candidate, it can potentially exclude distant regions without inspecting their individual points."
    },
    {
      "heading": "Search operations",
      "text": "Exact lookup\n\nTo locate a particular point, a search compares its coordinates with the splitting rules and descends through the appropriate children. In a balanced tree with an unambiguous routing rule, this takes \\(O(\\log n)\\) node visits.\n\nDuplicate points and split-coordinate ties must follow the same conventions during construction and lookup. Floating-point coordinates also require a distinction between exact coordinate equality and application-defined proximity.\n\nOrthogonal range searching\n\nAn orthogonal range query reports points inside an axis-aligned box,\n\n\\[Q=[a_1,b_1]\\times\\cdots\\times[a_d,b_d].\\]\n\nFor each subtree, its spatial region can be classified as:\n\n- Disjoint from the query: skip the subtree.\n- Completely contained in the query: report its points without further geometric filtering.\n- Partially overlapping: inspect the node or recurse into its children.\n\nFor a standard balanced KD-tree with cyclic coordinate splitting, fixed dimension \\(d\\ge2\\), and suitable handling of boundary degeneracies, the classical reporting bound is\n\n\\[O\\!\\left(n^{1-1/d}+m\\right),\\]\n\nwhere \\(m\\) is the number of reported points. This guarantee belongs to that construction and query model; it is not a universal bound for every structure called a KD-tree.\n\nNearest-neighbor search\n\nA nearest-neighbor query seeks a stored point minimizing its distance to a query point \\(q\\). For Euclidean distance,\n\n\\[\\operatorname{dist}(q,x) = \\sqrt{\\sum_{i=1}^{d}(q_i-x_i)^2}.\\]\n\nAn exact search generally visits the child containing the query first, finds an initial candidate, and then checks whether other subtrees could contain a better candidate.\n\nIf the current best distance is \\(r\\), the distance from the query to a splitting plane provides a simple lower bound for the opposite subtree. That subtree can be skipped when the lower bound exceeds \\(r\\). A tighter test uses the minimum distance from the query to the subtree’s bounding box.\n\nFor a box \\(B=\\prod_i[\\ell_i,u_i]\\), define\n\n\\[\\delta_i=\\max(\\ell_i-q_i,\\ 0,\\ q_i-u_i).\\]\n\nThe squared Euclidean distance from the query to the box is\n\n\\[\\operatorname{dist}(q,B)^2=\\sum_{i=1}^{d}\\delta_i^2.\\]\n\nBecause every point in that box is at least this far away, the bound supports safe pruning. This is an instance of branch and bound: a region is excluded when its best possible result cannot improve the current answer. If all equally near points must be returned, equality at the pruning boundary must be handled accordingly.\n\nFor multiple nearest neighbors, the search commonly maintains a bounded priority queue of candidates. Once enough candidates have been found, the farthest retained candidate determines the current pruning distance. Radius queries use the same bounds to find all points within a specified distance.\n\nApproximate search\n\nApproximate queries can reduce work by relaxing the result requirement. Some methods guarantee a returned nearest-neighbor distance \\(\\hat r\\) satisfying\n\n\\[\\hat r\\le(1+\\varepsilon)r^\\ast,\\]\n\nwhere \\(r^\\ast\\) is the true nearest-neighbor distance and \\(\\varepsilon\\ge0\\). Others limit the number of leaves or nodes inspected and provide an empirical speed–accuracy trade-off rather than that guarantee.\n\nThe approximation rule is part of the search algorithm, not an automatic property of the tree."
    },
    {
      "heading": "Complexity and performance",
      "text": "KD-tree performance depends on dimension, data distribution, splitting policy, query type, and leaf size.\n\nOperation or property | Typical bound or qualification\nBalanced-tree height | \\(O(\\log n)\\)\nStatic construction using efficient median selection | \\(O(n\\log n)\\) for fixed dimension\nTree storage | \\(O(n)\\) for fixed dimension\nExact lookup in a balanced tree | \\(O(\\log n)\\) node visits\nExact nearest-neighbor search | Often efficient in low dimensions; worst-case \\(O(n)\\) point examinations\nOrthogonal range reporting | \\(O(n^{1-1/d}+m)\\) under the classical assumptions\n\nThese bounds distinguish tree traversal from coordinate arithmetic. When dimension varies, storing the points requires \\(O(nd)\\) coordinates, and an individual distance computation generally costs \\(O(d)\\). Storing a full bounding box at every node also introduces dimension-dependent storage and computation.\n\nA balanced tree does not guarantee logarithmic nearest-neighbor search. A query can intersect the relevant distance bounds of many regions and force extensive backtracking. For fixed dimension, worst-case exact nearest-neighbor search can therefore approach a full scan.\n\nBucket leaves reduce the overhead of recursive traversal and allow contiguous scans of small point arrays. Their practical efficiency is influenced by CPU cache behavior and vectorized distance calculations. Small leaves provide finer pruning; large leaves reduce tree-navigation overhead."
    },
    {
      "heading": "Updates",
      "text": "Insertion follows the splitting rules until it reaches an appropriate location. In a simple unbalanced implementation, unfavorable insertion orders can produce a tree of height \\(O(n)\\).\n\nDeletion is more involved than in an ordinary binary search tree because replacement points must preserve coordinate-dependent partition constraints. Implementations may use coordinate-specific replacement searches, mark points as deleted, or rebuild affected subtrees.\n\nOrdinary search-tree rotations are not generally a drop-in balancing technique: they can invalidate the spatial partitions. Static or mostly static datasets are consequently a natural fit for KD-trees. Dynamic implementations often rely on periodic rebuilding, buffered updates, or specialized balancing methods."
    },
    {
      "heading": "Applications",
      "text": "KD-trees are useful when repeated spatial queries justify the cost of constructing an index.\n\n- Machine learning: they can accelerate neighbor retrieval for the k-nearest neighbors algorithm and other distance-based methods. The tree is an indexing mechanism, not itself a predictive model.\n- Computer vision and robotics: they support point-cloud matching, local neighborhood extraction, and geometric correspondence searches.\n- Scientific computing: they organize multidimensional samples and support proximity queries in numerical datasets.\n- Databases: they provide one form of multidimensional database indexing, especially for point data.\n- Computer graphics: related spatial KD-tree structures accelerate ray tracing by partitioning scene space. These may index geometric primitives rather than points, and their construction objectives differ from those of nearest-neighbor indexes."
    },
    {
      "heading": "Limitations and alternatives",
      "text": "High dimensionality\n\nKD-trees are affected by the curse of dimensionality. As dimension increases, bounding regions often become less effective at excluding points, and a search may visit much of the tree. There is no universal dimension at which a KD-tree becomes ineffective: performance also depends on intrinsic dimension, data size, distribution, and required accuracy.\n\nA direct scan may outperform tree traversal when pruning is weak, especially when distance calculations are efficiently vectorized. High-dimensional embedding retrieval frequently uses other approximate nearest-neighbor indexes.\n\nCoordinate dependence and distance functions\n\nAxis-aligned partitions depend on the coordinate system. Rotating a dataset preserves Euclidean pairwise distances but can substantially change KD-tree performance. Likewise, feature scaling changes both partition geometry and the meaning of distances unless the metric is adjusted correspondingly.\n\nKD-trees work particularly naturally with coordinate-based distances such as Euclidean, Manhattan, and other Minkowski distances. More general similarity measures require valid region-distance lower bounds to support correct pruning.\n\nRelated structures\n\nA ball tree groups points into regions bounded by balls rather than axis-aligned boxes. An R-tree organizes objects using bounding rectangles and is widely used for spatial objects with extent, rather than only individual points.\n\nQuadtrees and octrees typically subdivide two- or three-dimensional space into fixed sets of spatial children. KD-trees instead use binary splits and can adapt split locations to the data. None of these alternatives is uniformly superior; their effectiveness depends on the indexed objects and query workload."
    },
    {
      "heading": "History",
      "text": "Jon Louis Bentley introduced the multidimensional binary search tree in 1975 as a structure for associative searching. The KD-tree became a foundational spatial-search structure, with later work developing nearest-neighbor search methods, alternative splitting rules, bucket representations, and approximate querying techniques.\n\nThe term now covers a family of related implementations rather than one fixed layout or construction algorithm. Performance claims therefore need to specify the representation, split policy, distance metric, and whether queries are exact or approximate."
    }
  ],
  "links": [
    "data-structure",
    "computational-geometry",
    "machine-learning",
    "computer-graphics",
    "binary-tree",
    "binary-search-tree",
    "divide-and-conquer",
    "branch-and-bound",
    "priority-queue",
    "recursion",
    "cpu-cache",
    "k-nearest-neighbors-algorithm",
    "database-index",
    "ray-tracing",
    "curse-of-dimensionality",
    "feature-scaling"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}