# k-d 树

**k-d 树**（k 维树的简称）是一种二叉搜索树，它将 k 维空间中的点组织起来，以便通过关联搜索检索信息，例如查找某个区域内的所有点或查询点的最近邻。它由当时正在研究多维数据结构的计算机科学家乔恩·路易斯·本特利（Jon Louis Bentley）提出，发表于其 1975 年的论文《用于关联搜索的多维二叉搜索树》（"Multidimensional Binary Search Trees Used for Associative Searching"），载于《ACM 通讯》（*Communications of the ACM*）18(9): 509–517。<sup>[1](https://sigmod.org/publications/dblp/db/journals/cacm/Bentley75.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 一种多维二叉搜索树，其中 k 为搜索空间的维数，用于存储通过关联搜索检索的信息 |
| 起源 | 乔恩·路易斯·本特利，《ACM 通讯》18(9): 509–517，1975 年 9 月<sup>[1](https://sigmod.org/publications/dblp/db/journals/cacm/Bentley75.html)</sup> |
| 部分匹配查询 | 在 k 个键中指定 t 个键时，已证明的最大运行时间为 \( O(n^{(k-t)/k}) \) |
| 最近邻查询 | 经验观测到的平均运行时间为 \( O(\log n) \) |
| 实际查询开销 | 构建完成后，典型使用中只需 \( O(\log N) \) 次距离计算即可确定查询点的最近邻<sup>[2](https://github.com/scikit-learn/scikit-learn/blob/main/doc/modules/neighbors.rst)</sup> |
| 最佳适用情形 | 经验上在低维情形下表现最佳；在更高维情形下，一项比较中覆盖树表现最佳<sup>[3](https://citeseerx.ist.psu.edu/document?doi=9961fda75320f975a70ff8adb19f931d2dfebec9&repid=rep1&type=pdf)</sup> |
| 标准实现 | SciPy 的 `KDTree`（默认 leafsize 为 10）和 `cKDTree`（默认 leafsize 为 16），以及 scikit-learn 的 `KDTree`<sup>[4](https://vaje.um.si/docs/scipy-html/reference/generated/scipy.spatial.KDTree.html)</sup><sup> • </sup><sup>[5](https://cache-docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.cKDTree.html)</sup> |

## 结构与构建

k-d 树将有限维空间中的有限点集存储在二叉树中。<sup>[6](https://epubs.siam.org/doi/abs/10.1137/1.9780898718348.appb)</sup> 每个内部节点用一个与某一坐标轴对齐的分割超平面来划分点集，使得沿该轴坐标值较小的点进入一个子树，其余点进入另一个子树。因此，沿树下行时每一步都会缩小空间区域，而无需计算数据点之间的距离。

正是这种轴对齐的划分使构建变得快速：由于划分仅沿数据轴进行，构建树时无需计算任何 D 维距离。<sup>[2](https://github.com/scikit-learn/scikit-learn/blob/main/doc/modules/neighbors.rst)</sup> 各实现都提供一个叶大小参数，用于控制查询在叶节点内从树遍历切换为暴力搜索的样本数；SciPy 的 `KDTree` 默认 leafsize 为 10，`cKDTree` 默认 leafsize 为 16。<sup>[7](https://scikit-learn.org/stable/modules/neighbors.html)</sup><sup> • </sup><sup>[4](https://vaje.um.si/docs/scipy-html/reference/generated/scipy.spatial.KDTree.html)</sup><sup> • </sup><sup>[5](https://cache-docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.cKDTree.html)</sup>

## 查询

本特利的原始论文给出了针对多种查询类型的搜索算法。对于部分匹配查询——即 k 个键中只指定 t 个——他证明了最大运行时间为 \( O(n^{(k-t)/k}) \)；对于最近邻查询，他报告了经验观测到的平均运行时间 \( O(\log n) \)；他还提出了一种处理任意一般交集查询的算法。

**最近邻搜索。** 搜索首先沿树下行至查询点将落入的终端节点，然后检查附近那些区域可能包含更近点的子树。<sup>[8](https://citeseerx.ist.psu.edu/document?doi=6fdafcaf454f735fc79b72a565efc68decab26fe&repid=rep1&type=pdf)</sup> 由于树会剪除那些区域不可能包含更近点的分支，在由 n 个点构建的树上进行单次邻居查询，典型使用中只需 \( O(\log N) \) 次距离计算。<sup>[2](https://github.com/scikit-learn/scikit-learn/blob/main/doc/modules/neighbors.rst)</sup>

**范围查询与球查询。** 后来的库接口推广了区域搜索的思想。SciPy 的 `query_ball_point(x, r)` 查找距点 x 距离 r 以内的所有点，使用闵可夫斯基 p-范数（默认 p = 2.0，即[欧几里得范数](https://www.edgepedia.org/zh/euclidean-norm)），并支持一种近似模式：若分支的最近点距离大于 r / (1 + eps)，则不探索该分支。<sup>[9](https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.cKDTree.query_ball_point.html)</sup> SciPy 还提供 `query_pairs` 以查找给定距离内的所有点对，以及 `boxsize` 选项，对树施加 m 维环面拓扑，适用于周期性边界条件。<sup>[10](https://github.com/scipy/scipy/blob/main/scipy/spatial/_kdtree.py)</sup><sup> • </sup><sup>[11](https://docs.scipy.org/doc/scipy-1.3.1/scipy-ref-1.3.1.pdf)</sup>

## 历史

本特利 1975 年的论文在 ACM 1975 年 George E. Forsythe 学生论文竞赛中获得第二名，它建立在芬克尔（Finkel）与本特利更早的工作之上，他们的四叉树将记录存储在节点出度为 \( 2^k \) 的树中，是首个用树结构解决该问题的通用方法。<sup>[12](https://www.cs.rpi.edu/~cutler/classes/advancedgraphics/S23/papers/bentley_kdtree_1975.pdf)</sup><sup> • </sup><sup>[13](https://cs.wmich.edu/gupta/teaching/cs6310/lectureNotes_cs6310/kdtree-bentley.pdf)</sup> 弗里德曼（Friedman）、本特利与芬克尔在 1977 年一篇密切相关的报告中，在最近邻搜索的背景下引入了这一数据结构。<sup>[14](https://arxiv.org/abs/1903.04936)</sup>

本特利继续发展这一思想。他在多维分治方面的工作运用该范式，为范围搜索、最近点对和全最近邻等问题给出了当时已知的最佳解法。<sup>[15](https://doi.org/10.1145%2F358841.358850)</sup> 一篇 1990 年关于半动态点集 k-d 树的论文表明，在一般 k-d 树中需要 \( O(\log N) \) 期望时间的若干操作，在半动态树中可在常数期望时间内完成，并且简单的采样可将构建树的时间从 \( O(KN \log N) \) 降至 \( O(KN + N \log N) \)。<sup>[16](https://doi.org/10.1145/98524.98564)</sup>

## 变体

研究者提出了针对特定查询组合进行调整的修改方案。方形 k-d 树（squarish k-d tree）改变了分割规则，并针对部分匹配查询、正交范围搜索和最近邻查询的期望性能进行了分析。<sup>[17](https://citeseerx.ist.psu.edu/document?doi=e3c0c2cbb0c7497474bab4dd1ba2156ca071af18&repid=rep1&type=pdf)</sup> 较近期的排序投影半径 k-d 树（Sorted-Projection Radius KD-tree，SPRK-tree）将基于半径缩减的子树剪枝与基于排序投影的叶节点相结合，据报道在球形范围查询的几乎所有基准测试中都取得了最快的查询时间。<sup>[18](https://arxiv.org/abs/2607.07367)</sup> 正确性也得到了形式化处理：一项 2023 年的案例研究使用 Coq 证明助手验证了 k-d 树的构建及配套的 K 最近邻搜索算法。<sup>[19](https://arxiv.org/abs/2311.10965)</sup>

## 与相关结构的比较

最近邻工作的主要替代方案是球树（ball tree），它递归地将数据划分为由质心 C 和半径 r 定义的节点，因此节点中的每个点都位于二者所定义的超球体内；与轴对齐分割不同，这种方式能适应数据的形状。<sup>[2](https://github.com/scikit-learn/scikit-learn/blob/main/doc/modules/neighbors.rst)</sup> 一项对精确最近邻方法的经验比较发现，KD 树在低维情形下最佳，而在更高维情形下覆盖树最佳。<sup>[3](https://citeseerx.ist.psu.edu/document?doi=9961fda75320f975a70ff8adb19f931d2dfebec9&repid=rep1&type=pdf)</sup> 随着请求的邻居数 k 增大，KD 树和球树的查询时间增长都会变慢，二者都使用叶大小参数来控制查询何时切换为暴力搜索。<sup>[7](https://scikit-learn.org/stable/modules/neighbors.html)</sup><sup> • </sup><sup>[2](https://github.com/scikit-learn/scikit-learn/blob/main/doc/modules/neighbors.rst)</sup>

## 软件

k-d 树是科学计算库的标准组件。SciPy 提供 `KDTree` 以及更快的 `cKDTree`，其方法包括用于最近邻的 `query`、用于半径搜索的 `query_ball_point`、用于两棵树之间点对的 `query_ball_tree` 以及 `count_neighbors`。<sup>[5](https://cache-docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.cKDTree.html)</sup><sup> • </sup><sup>[10](https://github.com/scipy/scipy/blob/main/scipy/spatial/_kdtree.py)</sup> scikit-learn 在其 neighbors 模块内部使用 KDTree 和 BallTree，并公开它们的查询策略和距离度量。<sup>[7](https://scikit-learn.org/stable/modules/neighbors.html)</sup>

## References

1. [Jon Louis Bentley: Multidimensional Binary Search Trees Used for Associative Searching. Commun. ACM 18(9): 509–517 (1975), DBLP](https://sigmod.org/publications/dblp/db/journals/cacm/Bentley75.html)
2. [scikit-learn documentation: Nearest Neighbors (neighbors.rst), GitHub](https://github.com/scikit-learn/scikit-learn/blob/main/doc/modules/neighbors.rst)
3. [An Empirical Comparison of Exact Nearest Neighbor Algorithms, CiteSeerX](https://citeseerx.ist.psu.edu/document?doi=9961fda75320f975a70ff8adb19f931d2dfebec9&repid=rep1&type=pdf)
4. [scipy.spatial.KDTree, SciPy Manual](https://vaje.um.si/docs/scipy-html/reference/generated/scipy.spatial.KDTree.html)
5. [scipy.spatial.cKDTree, SciPy v1.18.0 Manual](https://cache-docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.cKDTree.html)
6. [Data Clustering: Theory, Algorithms, and Applications, Appendix B: The kd-Tree Data Structure, SIAM](https://epubs.siam.org/doi/abs/10.1137/1.9780898718348.appb)
7. [1.6. Nearest Neighbors, scikit-learn 1.9.1 documentation](https://scikit-learn.org/stable/modules/neighbors.html)
8. [Nearest Neighbour Searching in k-d trees, CiteSeerX](https://citeseerx.ist.psu.edu/document?doi=6fdafcaf454f735fc79b72a565efc68decab26fe&repid=rep1&type=pdf)
9. [scipy.spatial.cKDTree.query_ball_point, SciPy v1.18.0 Manual](https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.cKDTree.query_ball_point.html)
10. [scipy/scipy/spatial/_kdtree.py, GitHub](https://github.com/scipy/scipy/blob/main/scipy/spatial/_kdtree.py)
11. [SciPy Reference Guide, Release 1.3.1](https://docs.scipy.org/doc/scipy-1.3.1/scipy-ref-1.3.1.pdf)
12. [Bentley (1975), 1. Introduction, RPI course PDF](https://www.cs.rpi.edu/~cutler/classes/advancedgraphics/S23/papers/bentley_kdtree_1975.pdf)
13. [Bentley (1975), 1975 ACM Student Award copy, WMich course PDF](https://cs.wmich.edu/gupta/teaching/cs6310/lectureNotes_cs6310/kdtree-bentley.pdf)
14. [The k-d tree data structure and a proof for neighborhood computation in expected logarithmic time, arXiv:1903.04936](https://arxiv.org/abs/1903.04936)
15. [Jon Louis Bentley: Multidimensional divide-and-conquer, Communications of the ACM](https://doi.org/10.1145%2F358841.358850)
16. [K-d trees for semidynamic point sets, Proceedings of the sixth annual Symposium on Computational Geometry](https://doi.org/10.1145/98524.98564)
17. [Squarish k-d trees, CiteSeerX](https://citeseerx.ist.psu.edu/document?doi=e3c0c2cbb0c7497474bab4dd1ba2156ca071af18&repid=rep1&type=pdf)
18. [Benchmarking and Engineering Data Structures for Spherical Range Queries, arXiv:2607.07367](https://arxiv.org/abs/2607.07367)
19. [(Nearest) Neighbors You Can Rely On: Formally Verified k-d Tree Construction and Search in Coq, arXiv:2311.10965](https://arxiv.org/abs/2311.10965)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026; Oct 25, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Data structures › Trees

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/k-d-tree

English original: https://www.edgepedia.org/k-d-tree
