# 搜索树（计算机科学）

搜索树是一种树状数据组织，其中每个节点保存一个键，而节点之间键的次序引导对目标值、其位置或一个值范围的系统搜索。二叉搜索树由托马斯·N·希巴德（Thomas N. Hibbard）于 1962 年正式提出，原因很实际：需要一种既能高效搜索又能高效修改的列表；在普通顺序表中插入或删除平均需要移动 \( n/2 \) 个元素，而树形列表的期望搜索和更新时间与 log n 成正比。<sup>[1](https://doi.org/10.1145/321105.321108)</sup> 搜索树保持键有序，因此同一结构还支持最小、最大、下界（floor）、上界（ceiling）、秩和范围查询。<sup>[2](https://algs4.cs.princeton.edu/32bst/index.php)</sup>

| 性质 | 值 |
|---|---|
| 次序不变式 | 每个节点的键大于其左子树中的所有键，小于其右子树中的所有键 <sup>[2](https://algs4.cs.princeton.edu/32bst/index.php)</sup> |
| 非平衡 BST 的代价 | 在最坏情况下，搜索、插入、删除、最小/最大、下界（floor）、上界（ceiling）、秩、选择和范围计数所需时间都与树高成正比 <sup>[2](https://algs4.cs.princeton.edu/32bst/index.php)</sup> |
| 随机键平均 | 在由 N 个随机键构建的 BST 中，一次成功搜索大约需要 2 ln N 次比较，约为 1.39 lg N 次 <sup>[2](https://algs4.cs.princeton.edu/32bst/index.php)</sup> |
| B 树代价 | 检索、插入和删除的时间与 \( \log_{k} l \) 成正比，存储利用率至少为 50% <sup>[3](https://liacs.leidenuniv.nl/~stefanovtp/courses/StudentenSeminarium/Papers/DB/OMLO.pdf)</sup> |
| AVL 高度界 | 至多 1.4405 lg(n+2) − 0.3277 <sup>[4](https://web.stanford.edu/~blp/papers/libavl.pdf)</sup> |
| 红黑高度界 | 至多 2 lg(n+1) <sup>[4](https://web.stanford.edu/~blp/papers/libavl.pdf)</sup> |
| 伸展树代价 | 每次标准操作摊还 <sup>[5](https://doi.org/10.1145/3828.3835)</sup> |

## 工作原理

搜索树性质按位置排列键：任一节点中的键大于其左子树中的所有键，小于其右子树中的所有键。<sup>[2](https://algs4.cs.princeton.edu/32bst/index.php)</sup> 搜索将目标与根比较，并沿一个分支下行，因此每次操作只触及一条根到节点路径，代价与树高成正比。<sup>[2](https://algs4.cs.princeton.edu/32bst/index.php)</sup>

## 实现方法

**搜索与插入。** 搜索将查询键与当前节点的键比较，并向左或向右下行，直到找到该键或到达空链接。插入一个缺失的键也沿同一路径进行；搜索结束处的空链接由一个新节点替代。<sup>[2](https://algs4.cs.princeton.edu/32bst/index.php)</sup>

**删除。** 至多有一个子节点的节点被直接摘除。有两个子节点的节点由其后继替代，即其右子树中键最小的节点，该节点必然没有左子节点；这种方法由希巴德于 1962 年首次提出。<sup>[2](https://algs4.cs.princeton.edu/32bst/index.php)</sup><sup> • </sup><sup>[6](https://web.stanford.edu/class/archive/cs/cs161/cs161.1176/Lectures/CS161Lecture07.pdf)</sup> 一种常见实现把后继的键复制到被删除节点的位置，然后从右子树中递归删除该后继。<sup>[7](https://dsa.handbook.academy/curriculum/trees-heaps/binary-search-trees/)</sup>

**旋转。** 旋转在 \( O(1) \) 时间内局部重构树，同时保持 BST 性质，是再平衡方案所使用的手段。<sup>[6](https://web.stanford.edu/class/archive/cs/cs161/cs161.1176/Lectures/CS161Lecture07.pdf)</sup>

## 起源

希巴德在 1962 年发表于《ACM 杂志》的《某些树的若干组合性质及其在搜索与排序中的应用》一文中引入了二叉搜索树。<sup>[1](https://doi.org/10.1145/321105.321108)</sup> 第一种使搜索、插入和删除在时间上都最优的平衡树是 AVL 树，它要求任一节点两棵子树的高度差至多为 1。<sup>[8](https://people.apache.org/~shv/docs/amcs04.pdf)</sup> 鲁道夫·拜尔（Rudolf Bayer）在 1971 年的一份普渡大学（Purdue）报告中描述了对称二叉 B 树，即红黑树的前身。<sup>[9](https://docs.lib.purdue.edu/cgi/viewcontent.cgi?article=1457&context=cstech)</sup> 伸展树由丹尼尔·D·斯莱特（Daniel D. Sleator）和[罗伯特·E·塔扬](https://www.edgepedia.org/zh/robert-e-tarjan)（Robert E. Tarjan）于 1985 年在《ACM 杂志》上提出并分析。<sup>[5](https://doi.org/10.1145/3828.3835)</sup> 克努特（Knuth）的动态规划算法在 \( O(n^{2}) \) 时间内计算最优二叉搜索树，于 1971 年发表在《信息学学报》上。<sup>[10](https://doi.org/10.1007/bf00264289)</sup>

## 变体

**完全平衡的多路节点。** 在 2-3 树中，所有空链接到根的距离相同，在含 N 个键的树中进行搜索或插入至多访问 lg N 个节点。<sup>[11](https://algs4.cs.princeton.edu/33balanced/)</sup> 红黑二叉搜索树将 2-3 树编码进一棵二叉树：每个 3-节点变成两个 2-节点，由一条左倾红链接相连；高度至多为 2 lg N，平均路径长度约为 1.00 lg N。<sup>[11](https://algs4.cs.princeton.edu/33balanced/)</sup>

**随机化。** 树堆（treap）让键保持 BST 次序，并让独立抽取的随机优先级保持堆序；查找、插入、删除、分裂和合并的期望运行时间为 \( O(\log n) \)。<sup>[12](https://ti.inf.ethz.ch/ew/courses/APC22/Chapter_2.pdf)</sup> B 树将 2-3-4 树推广到分支因子有时达数千的情况，适合访问速度慢的磁盘。<sup>[13](https://www.cs.cmu.edu/afs/cs/academic/class/15210-s14/www/lectures/bst.pdf)</sup>

**自调整。** 伸展树不施加结构约束；相反，伸展启发式沿访问路径将被访问节点旋转到根，所有标准操作都在摊还对数时间内运行。<sup>[5](https://doi.org/10.1145/3828.3835)</sup> 伸展树被猜想具有动态最优性，已确立的保证包括静态最优性、工作集界和动态手指界。<sup>[14](https://erikdemaine.org/papers/BST_SODA2009/paper.pdf)</sup> Tango 树由埃里克·D·德梅恩（Erik D. Demaine）及其同事于 2007 年在《SIAM 计算杂志》上提出，达到 O(lg lg n) 竞争比，这是 1985 年猜想上的首个重大进展。<sup>[15](https://epubs.siam.org/doi/10.1137/S0097539705447347)</sup>

**并发。** 法图鲁（Fatourou）和鲁珀特（Ruppert）于 2024 年发表了一种通用技术，用可由一个节点及其子节点计算出的字段来增广任意无锁树，使无锁顺序统计树、区间树和线段树成为可能；其增广 BST 在高度为 \( h \) 的树上以 \( O(h) \) 步回答顺序统计查询。<sup>[16](https://doi.org/10.4230/lipics.disc.2024.23)</sup> 对于并发 B 树，菲利普·L·莱曼（Philip L. Lehman）和 S·宾·姚（S. Bing Yao）于 1981 年在《ACM 数据库系统汇刊》上发表了用于并发操作的高效加锁方法。<sup>[17](https://doi.org/10.1145/319628.319663)</sup>

## 应用

**磁盘上的 B 树。** 类别为 \( (k, h) \) 的 B 树，其所有根到叶路径的高度都等于 h，每个非根内部节点有 \( k+1 \) 到 \( 2k+1 \) 个子节点，其中 \( k \) 是取决于设备的页参数。<sup>[3](https://liacs.leidenuniv.nl/~stefanovtp/courses/StudentenSeminarium/Papers/DB/OMLO.pdf)</sup> 对于大小为 l 的索引，检索、插入和删除的时间与 \( \log_{k} l \) 成正比，存储利用率至少为 50%。<sup>[3](https://liacs.leidenuniv.nl/~stefanovtp/courses/StudentenSeminarium/Papers/DB/OMLO.pdf)</sup> 插入会分裂已满的页，并把分裂向根传播；根分裂会创建新的根，是高度增加的唯一方式。<sup>[3](https://liacs.leidenuniv.nl/~stefanovtp/courses/StudentenSeminarium/Papers/DB/OMLO.pdf)</sup><sup> • </sup><sup>[18](https://www.cs.ubc.ca/~liorma/cpsc320/files/B-trees.pdf)</sup> 由于每次读取节点都需要一次缓慢的随机磁盘访问，数据库使用高分支因子来最小化 I/O 开销，因此一次根到叶的遍历只需少量二级存储器访问。<sup>[18](https://www.cs.ubc.ca/~liorma/cpsc320/files/B-trees.pdf)</sup><sup> • </sup><sup>[19](https://cs.nyu.edu/~shasha/papers/datastructuresbook.pdf)</sup>

B+ 树一词指这样一种变体：所有键都驻留在叶节点中，内部节点保存仅用于引导搜索的副本，且有序链接的叶节点支持范围查询。<sup>[20](https://users.cs.utah.edu/~pandey/courses/cs6530/fall22/papers/trees/p121-comer.pdf)</sup><sup> • </sup><sup>[19](https://cs.nyu.edu/~shasha/papers/datastructuresbook.pdf)</sup>

## 局限与替代方案

**退化。** 按有序顺序插入键会产生一棵极不平衡的树，总构建时间为 \( O(n^{2}) \)，搜索代价增长到 \( n \) <sup>[21](https://chalmersgu-data-structure-courses.github.io/dsabook/html/section-10.1.html)</sup>；在最坏情况下，BST 的高度为 \( O(n) \)，例如一条很长的向右路径。<sup>[6](https://web.stanford.edu/class/archive/cs/cs161/cs161.1176/Lectures/CS161Lecture07.pdf)</sup> 几乎没有人使用普通 BST，因为不存在最坏情况保证；标准库改为提供红黑树：Java 的 TreeMap、.NET 的 SortedDictionary 和 C++ 的 std::map，后者的常见实现采用红黑树，尽管 C++ 标准并未强制要求这种结构。<sup>[7](https://dsa.handbook.academy/curriculum/trees-heaps/binary-search-trees/)</sup> 希巴德删除会发生漂移：在大量混合插入和删除循环中，总是取右子树后继会使树向左偏斜，平均路径长度从约 1.39 lg N 漂向 \( \sqrt{N} \)。<sup>[7](https://dsa.handbook.academy/curriculum/trees-heaps/binary-search-trees/)</sup>

**哈希表。** 冲突很少的哈希索引在一次点查询中读取一个数据块，但它支持范围查询的效率不如有序树索引。<sup>[19](https://cs.nyu.edu/~shasha/papers/datastructuresbook.pdf)</sup>

**跳跃表与字典树。** 跳跃表使用抛硬币式随机插入，使搜索、插入和删除的期望时间为 \( O(\log n) \) <sup>[22](https://users.cs.utah.edu/~pandey/courses/cs6530/fall22/slides/Lecture03.pdf)</sup>；字典树一次检查一个键前缀，不需要再平衡，对于长度为 \( k \) 的键以 \( O(k) \) 时间运行，例如为内存数据库系统构建的自适应基数树（Adaptive Radix Tree）这样的 256 路[基数树](https://www.edgepedia.org/zh/radix-tree)。<sup>[22](https://users.cs.utah.edu/~pandey/courses/cs6530/fall22/slides/Lecture03.pdf)</sup>

**学习型索引。** B 树索引可以看作一个把键映射到有序数组中位置的模型，现有索引结构可以用其他类型的模型替代，包括深度学习模型，这类模型被称为学习型索引。<sup>[23](https://dl.acm.org/doi/10.1145/3183713.3196909)</sup>

## References

1. [Thomas N. Hibbard (1962). Some Combinatorial Properties of Certain Trees With Applications to Searching and Sorting. Journal of the ACM.](https://doi.org/10.1145/321105.321108)
2. [Binary Search Trees (Sedgewick & Wayne, Algorithms 4th ed.)](https://algs4.cs.princeton.edu/32bst/index.php)
3. [Organization and Maintenance of Large Ordered Indices (Bayer & McCreight)](https://liacs.leidenuniv.nl/~stefanovtp/courses/StudentenSeminarium/Papers/DB/OMLO.pdf)
4. [Performance of BST data structures in system software (Stanford libavl study)](https://web.stanford.edu/~blp/papers/libavl.pdf)
5. [Daniel Dominic Sleator, Robert Endre Tarjan (1985). Self-adjusting binary search trees. Journal of the ACM.](https://doi.org/10.1145/3828.3835)
6. [Stanford CS161 Lecture 7: BSTs and Red-Black Trees](https://web.stanford.edu/class/archive/cs/cs161/cs161.1176/Lectures/CS161Lecture07.pdf)
7. [Binary search trees, The DSA Handbook](https://dsa.handbook.academy/curriculum/trees-heaps/binary-search-trees/)
8. [S(b)-trees: external dynamic dictionaries with variable length keys](https://people.apache.org/~shv/docs/amcs04.pdf)
9. [Symmetric Binary B-Trees (Bayer, 1972)](https://docs.lib.purdue.edu/cgi/viewcontent.cgi?article=1457&context=cstech)
10. [D. E. Knuth (1971). Optimum binary search trees. Acta Informatica.](https://doi.org/10.1007/bf00264289)
11. [Balanced Search Trees (Sedgewick & Wayne, Algorithms 4th ed.)](https://algs4.cs.princeton.edu/33balanced/)
12. [Random(ized) Search Trees (ETH Zürich course chapter)](https://ti.inf.ethz.ch/ew/courses/APC22/Chapter_2.pdf)
13. [CMU 15-210 Lecture: Binary Search Trees](https://www.cs.cmu.edu/afs/cs/academic/class/15210-s14/www/lectures/bst.pdf)
14. [The Geometry of Binary Search Trees (Demaine et al., SODA 2009)](https://erikdemaine.org/papers/BST_SODA2009/paper.pdf)
15. [Dynamic Optimality, Almost (Demaine, Harmon, Iacono, Pătraşcu, Tango trees, SICOMP)](https://epubs.siam.org/doi/10.1137/S0097539705447347)
16. [Fatourou, Panagiota, Ruppert, Eric (2024). Lock-Free Augmented Trees. DISC 2024, Leibniz International Proceedings in Informatics (LIPIcs).](https://doi.org/10.4230/lipics.disc.2024.23)
17. [Philip L. Lehman, s. Bing Yao (1981). Efficient locking for concurrent operations on B-trees. ACM Transactions on Database Systems.](https://doi.org/10.1145/319628.319663)
18. [B-Trees (UBC CPSC 320 notes, CLRS-based)](https://www.cs.ubc.ca/~liorma/cpsc320/files/B-trees.pdf)
19. [Data Structures for Data-Intensive Applications (Shasha et al.)](https://cs.nyu.edu/~shasha/papers/datastructuresbook.pdf)
20. [The Ubiquitous B-Tree (Comer, Computing Surveys, 1979)](https://users.cs.utah.edu/~pandey/courses/cs6530/fall22/papers/trees/p121-comer.pdf)
21. [DSABook – Binary search trees](https://chalmersgu-data-structure-courses.github.io/dsabook/html/section-10.1.html)
22. [Utah CS6530 Lecture 3: In-memory indexing (Trees, Tries, Skip Lists)](https://users.cs.utah.edu/~pandey/courses/cs6530/fall22/slides/Lecture03.pdf)
23. [The Case for Learned Index Structures (Kraska et al., SIGMOD 2018)](https://dl.acm.org/doi/10.1145/3183713.3196909)

---
*Initially written Sep 29, 2026 · Edited: 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/search-tree-computer-science

English original: https://www.edgepedia.org/search-tree-computer-science
