# 自平衡二叉搜索树

自平衡二叉搜索树通过平衡规则或重构来控制其高度或操作代价。许多变体保证 O(log n) 的高度，使得查找、插入和删除在最坏情况下都是对数级的，无论键的顺序如何。如果没有这样的不变量，普通的二叉搜索树可能长得过高，其集合操作虽然运行时间为 O(h)，但可能并不比链表更快<sup>[1](http://koclab.cs.ucsb.edu/teaching/cs130a/docx/07-redblack-chapter.pdf)</sup>。两种使用最广泛的变体——AVL 树和红黑树——都将 n 个节点的树高限制在 O(lg n)<sup>[2](https://stanford.edu/~blp/papers/libavl.pdf)</sup>。

| 关键事实 | 详情 |
|---|---|
| AVL 高度上界 | n 个条目时至多 2 log n + 2<sup>[3](https://www.cs.swarthmore.edu/courses/CS35/F26/book/Chapter10.pdf)</sup>；通常表述为约 1.44 log₂ n<sup>[4](https://notes.amittai.studio/algorithms/data-structures/balanced-trees)</sup> |
| 红黑树高度上界 | n 个值时至多 2 log n<sup>[5](https://opendatastructures.org/newhtml/ods/latex/redblack.html)</sup> |
| 旋转代价 | 红黑树插入至多执行 2 次旋转，删除至多 3 次；AVL 删除可能级联 Θ(log n) 次旋转<sup>[4](https://notes.amittai.studio/algorithms/data-structures/balanced-trees)</sup> |
| 摊还再平衡 | 自底向上再平衡时，红黑树摊还时间为 O(1)，最坏情况下旋转次数为 O(1)<sup>[6](https://dl.acm.org/doi/10.1145/2689412)</sup> |
| 伸展树 | 每次操作摊还 O(log n)，但单次操作可能为 Θ(n)<sup>[7](https://dl.acm.org/doi/10.1145/3828.3835)</sup><sup> • </sup><sup>[4](https://notes.amittai.studio/algorithms/data-structures/balanced-trees)</sup> |
| 应用场景 | 红黑树是 Java 集合框架、若干 C++ STL 实现以及 Linux 内核中的主要搜索结构<sup>[5](https://opendatastructures.org/newhtml/ods/latex/redblack.html)</sup> |
| 缓存现实 | 缓存感知的索引结构比非缓存感知的结构性能高出约 48%<sup>[8](https://www.cs.unm.edu/~saia/classes/361-s04/project/skiplist.pdf)</sup> |

## 什么是自平衡二叉搜索树

查找、前驱、后继、最小、最大、插入和删除的运行时间均为 O(h)，其中 h 是树的高度，因此性能取决于保持 h 较小<sup>[1](http://koclab.cs.ucsb.edu/teaching/cs130a/docx/07-redblack-chapter.pdf)</sup>。

许多自平衡树通过对节点高度、秩、颜色或子树大小施加不变量来防止性能退化，并在更新后修复违反不变量之处。其他方案，如伸展树，在没有显式平衡约束的情况下重构树。根据方案不同，对数级性能可能是最坏情况高度保证、期望界或摊还界。

## 平衡如何工作：旋转与不变量

**旋转。** 大多数自平衡树通过两种局部操作恢复其不变量：单旋转和双旋转。AVL 树、红黑树、伸展树以及其他几种自平衡二叉搜索树都使用这些相同的旋转<sup>[9](https://chalmersgu-dsa-course.github.io/dsabook/html/section-10.2.html)</sup>。重量平衡树中的再平衡沿搜索路径自底向上进行，在每个违反平衡条件的节点处应用一次旋转或双旋转<sup>[10](https://doi.org/10.1007/978-3-031-42209-6_10)</sup>。

**AVL 不变量。** AVL 树由阿德尔松-韦尔斯基（Adel'son-Vel'skii）和兰迪斯（Landis）于 1962 年发明，是第一种具有对数级查找和更新时间的数据字典结构，并引入了使用旋转的再平衡技术<sup>[10](https://doi.org/10.1007/978-3-031-42209-6_10)</sup>。其高度平衡性质要求对于每个内部节点，其子节点的高度差至多为 1<sup>[11](https://barsky.ca/marina/WC/algoF2021/slides/BalancedBSTBookChapter.pdf)</sup>；每个节点存储一个 −1、0 或 +1 的平衡因子，可用两位表示<sup>[10](https://doi.org/10.1007/978-3-031-42209-6_10)</sup>。

**红黑树不变量。** [Linux 内核](https://www.edgepedia.org/zh/linux-kernel)的 rbtree 实现陈述了五条规则：每个节点是红色或黑色，根是黑色，所有 NULL 叶子是黑色，每个红色节点的两个子节点都是黑色，每条从根到叶子的路径包含相同数量的黑色节点<sup>[12](https://github.com/torvalds/linux/blob/master/lib/rbtree.c)</sup>。不变量 4 和 5 给出了 O(log n) 保证：规则 4 禁止路径上出现两个连续的红色节点，因此如果 B 是每条简单路径上的黑色节点数，最长可能的路径为 2B<sup>[12](https://github.com/torvalds/linux/blob/master/lib/rbtree.c)</sup>。

**重建而非旋转。** 并非所有方案都旋转。替罪羊树在检测到不平衡时重建一棵全新的平衡子树<sup>[9](https://chalmersgu-dsa-course.github.io/dsabook/html/section-10.2.html)</sup>，插入后使用部分重建，删除后使用全局重建；查找最坏情况时间为 O(log n)，插入和删除为摊还 O(log n)<sup>[13](https://jeffe.cs.illinois.edu/teaching/algorithms/notes/10-scapegoat-splay.pdf)</sup>。替罪羊树按子树大小进行平衡，从头重建过大的子树，而重量平衡树使用子树大小和旋转<sup>[4](https://notes.amittai.studio/algorithms/data-structures/balanced-trees)</sup>。

## 操作代价与保证

高度上界有可测量的差异。存储 n 个条目的 AVL 树高度至多为 2 log n + 2<sup>[3](https://www.cs.swarthmore.edu/courses/CS35/F26/book/Chapter10.pdf)</sup>，实践者通常表述为约 1.44 log₂ n，而红黑树的上界为 2 log₂(n + 1)；因此 AVL 树中的查找在最坏情况下检查的节点更少<sup>[4](https://notes.amittai.studio/algorithms/data-structures/balanced-trees)</sup>。存储 n 个值的红黑树高度至多为 2 log n，添加和删除的最坏情况时间为 O(log n)，摊还旋转次数为常数<sup>[5](https://opendatastructures.org/newhtml/ods/latex/redblack.html)</sup>。

其他方案以最坏情况确定性换取简单性或速度。跳表和树堆依赖随机化，因此其 O(log n) 运行时间只是期望值；树堆为每个键分配随机优先级，并维护键上的 BST 顺序和优先级上的堆顺序<sup>[5](https://opendatastructures.org/newhtml/ods/latex/redblack.html)</sup><sup> • </sup><sup>[4](https://notes.amittai.studio/algorithms/data-structures/balanced-trees)</sup>。替罪羊树保证高度，但更新仅为摊还 O(log n)<sup>[5](https://opendatastructures.org/newhtml/ods/latex/redblack.html)</sup>。伸展树完全不保存平衡信息：其效率不来自显式的结构约束，而来自一种重构启发式——伸展（splaying），在每次访问树时应用<sup>[7](https://dl.acm.org/doi/10.1145/3828.3835)</sup>。单次操作可能为 Θ(n)，但任意 m 次操作的序列代价为摊还 O(m log n)<sup>[4](https://notes.amittai.studio/algorithms/data-structures/balanced-trees)</sup>。

## 数据说话

**旋转次数。** 向红黑树插入至多需要 2 次旋转，删除节点至多 3 次，其余工作都是重新着色；AVL 插入同样在单次再平衡步骤后完成，但 AVL 删除可能沿整个搜索路径向上传播旋转，最坏情况下达到 Θ(log n)<sup>[4](https://notes.amittai.studio/algorithms/data-structures/balanced-trees)</sup>。这与 Linux 内核文档的陈述一致：红黑树提供更快的实时有界最坏情况插入和删除，分别至多两次和三次旋转，查找稍慢但仍是 O(log n)<sup>[14](https://www.kernel.org/doc/html/latest/core-api/rbtree.html)</sup>。在秩平衡框架中，红黑树插入或删除后的自底向上再平衡摊还时间为 O(1)，最坏情况旋转次数为 O(1)<sup>[6](https://dl.acm.org/doi/10.1145/2689412)</sup>。

**结构变化。** AVL 树的查找、插入和删除各耗时 O(log n)；查找不引起结构变化，插入引起 O(1) 次，删除引起 O(log n) 次<sup>[11](https://barsky.ca/marina/WC/algoF2021/slides/BalancedBSTBookChapter.pdf)</sup>。

**重量平衡常数。** 平衡参数为 α 的重量平衡树的高度上界形式为 c · log n，其中 c = −1/log(1 − α) > 1.709，每个节点存储 log n 位的重量<sup>[10](https://doi.org/10.1007/978-3-031-42209-6_10)</sup>。

**指针开销。** 在 Linux 内核 2.4.10 的 vm_area_struct（一个 68 字节的结构）中，20 字节用于红黑树和单向链表：每个红黑树节点三个指针，一个颜色整数，一个右链接指针，实测空间开销固定为 41.7%<sup>[15](https://www.cs.helsinki.fi/linux/linux-kernel/2001-39/0315.html)</sup>。

**指令数与缓存未命中。** 在一个实测工作负载中，AVL 树需要 1.41 亿条指令，其中 3830 万次加载（27%）和 2180 万次存储（15%），而概率跳表为 8830 万条指令<sup>[8](https://www.cs.unm.edu/~saia/classes/361-s04/project/skiplist.pdf)</sup>。在 18 周期内存延迟下，AVL 树运行了 1.26 亿个周期（CPI 0.89），但在 500 周期延迟下需要 7.67 亿个周期，CPI 为 5.42，增长了 508%，表明当内存缓慢时指针追逐性能下降之剧烈<sup>[8](https://www.cs.unm.edu/~saia/classes/361-s04/project/skiplist.pdf)</sup>。在这些实验中，自顶向下再平衡操作平均减少了 31% 的总指令数<sup>[8](https://www.cs.unm.edu/~saia/classes/361-s04/project/skiplist.pdf)</sup>，另一项独立研究量化了 AVL 平衡操作对空间局部性的负面影响<sup>[16](https://www.scitepress.org/Papers/2006/13159/13159.pdf)</sup>。

## 如何在平衡树之间选择

**AVL 与红黑树。** 读多写少的工作负载倾向于 AVL 树较矮的树形，而插入/删除混合的工作负载倾向于红黑树更廉价的修复，这就是为什么红黑树是许多标准库有序映射的底层结构<sup>[4](https://notes.amittai.studio/algorithms/data-structures/balanced-trees)</sup>。基准证据指向同一方向，但有一个转折：一项受控的《软件：实践与经验》研究发现，对于随机顺序键的插入和删除，自底向上红黑树比 AVL 树更快，而 AVL 树在查找方面更快<sup>[17](https://onlinelibrary.wiley.com/doi/10.1002/spe.3437)</sup>。[北卡罗来纳大学](https://www.edgepedia.org/zh/university-of-north-carolina)早期的一项比较得出了更直截了当的结论：在树中，红黑树优于 AVL 树，因为它容忍更多不平衡且需要更少的再平衡，AVL 树在构建和查找时间上明显处于劣势<sup>[18](https://www.cs.unc.edu/~plaisted/comp550/Neyer%20paper.pdf)</sup>。两项研究在查找方面存在分歧，且两者都可信；差异可能反映了工作负载和实现的不同，但没有检索到的来源能解决这一分歧。

**与哈希表对比。** 对于纯查找，哈希表胜出：在北卡罗来纳大学的字典基准中，除了最平凡的字典外，哈希表在内存消耗和查找时间上都优于 AVL 树和红黑树<sup>[18](https://www.cs.unc.edu/~plaisted/comp550/Neyer%20paper.pdf)</sup>。哈希表无法做到的是维护键的顺序。平衡二叉搜索树支持有序操作，如范围查询、查找不大于给定键的最大键和不小于给定键的最小键、前驱和后继，以及有序迭代；通过大小增强，它可以在 O(log n) 内找到第 k 小的元素。哈希映射不维护键顺序，通常无法高效地回答这些问题<sup>[19](https://ironclad.academy/dsa/structures/balanced-bst)</sup>。对于 n = 100 万，log₂ n ≈ 20，因此有序结构在平均 O(1) 哈希查找之上付出约 20 倍的查找代价，以换取这些操作<sup>[19](https://ironclad.academy/dsa/structures/balanced-bst)</sup>。

**与 B 树对比。** B 树最初设计用于处理存储在外存上的数据，将物理块与多路树中的高度数节点关联；其最小示例是 2-3 树，节点度为 2 或 3<sup>[10](https://doi.org/10.1007/978-3-031-42209-6_10)</sup>。最小度为 t 的 B 树每个节点容纳 t − 1 到 2t − 1 个键，并保持所有叶子在同一深度，高度为 O(log_t n)；每块约一千个键时，十亿键的树只有 2 或 3 层深<sup>[4](https://notes.amittai.studio/algorithms/data-structures/balanced-trees)</sup>。

**与跳表和树堆对比。** 在北卡罗来纳大学的比较中，跳表的内存占用比其他结构更小，对于随机查询的性能与红黑树相当<sup>[18](https://www.cs.unc.edu/~plaisted/comp550/Neyer%20paper.pdf)</sup>。树堆提供的是期望而非最坏情况的对数行为<sup>[5](https://opendatastructures.org/newhtml/ods/latex/redblack.html)</sup>。

## 实际系统中的实现

红黑树作为主要搜索结构出现在 Java 集合框架、C++ 标准模板库的若干实现以及 Linux 内核中<sup>[5](https://opendatastructures.org/newhtml/ods/latex/redblack.html)</sup>。GCC 的 libstdc++ 在红黑树上实现关联容器 set、multiset、map 和 multimap，其插入和删除算法基于科曼（Cormen）、莱瑟森（Leiserson）和里维斯特（Rivest）的《算法导论》（[麻省理工学院出版社](https://www.edgepedia.org/zh/mit-press)，1990 年）<sup>[20](https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/stl_tree.h)</sup>。

Linux 内核的 rbtree 刻意保持精简。它将每个 struct rb_node 嵌入其所组织的数据结构中，比更传统的树实现少一层间接寻址，缓存局部性更好，并要求用户自己编写搜索和插入函数，同时将加锁留给用户<sup>[14](https://www.kernel.org/doc/html/latest/core-api/rbtree.html)</sup>。内核 6.14 添加了 Rust 绑定，暴露一个 RBTree 类型，其自有节点由 C 实现支持<sup>[21](https://rust.docs.kernel.org/6.14/kernel/rbtree/struct.RBTree.html)</sup>。

## 历史

阿德尔松-韦尔斯基和兰迪斯于 1962 年引入了 AVL 树，这是第一种具有对数级查找和更新时间的数据字典，也是基于旋转的再平衡技术的起源<sup>[10](https://doi.org/10.1007/978-3-031-42209-6_10)</sup>。伸展树由丹尼·斯利特（Danny Sleator）和鲍勃·塔扬（Bob Tarjan）于 1981 年发现<sup>[13](https://jeffe.cs.illinois.edu/teaching/algorithms/notes/10-scapegoat-splay.pdf)</sup>，并在他们 1985 年《ACM 期刊》的论文中描述<sup>[7](https://dl.acm.org/doi/10.1145/3828.3835)</sup>。替罪羊树由[阿尔内·安德松](https://www.edgepedia.org/zh/arne-andersson)（Arne Andersson）于 1989 年发现，伊加尔·加尔佩林（Igal Galperin）和[罗恩·里维斯特](https://www.edgepedia.org/zh/ron-rivest)（Ron Rivest）于 1993 年独立发现<sup>[13](https://jeffe.cs.illinois.edu/teaching/algorithms/notes/10-scapegoat-splay.pdf)</sup>；一般平衡树的思想此前已以该名称被重新发现<sup>[10](https://doi.org/10.1007/978-3-031-42209-6_10)</sup>。树堆由赛德尔（Seidel）和阿拉贡（Aragon）于 1996 年提出，塞奇威克（Sedgewick）的左倾红黑树——将红色链接限制为向左倾斜以简化实现——出现于 2008 年<sup>[4](https://notes.amittai.studio/algorithms/data-structures/balanced-trees)</sup>。

## 秩平衡树与 WAVL 树

秩平衡树使用秩和秩差在单一框架中定义基于高度的平衡：秩差上的不同不变量产生 AVL 树、红黑树和其他类型的平衡树<sup>[6](https://dl.acm.org/doi/10.1145/2689412)</sup>。森（Sen）和塔扬 2009 年的秩平衡树将秩差限制为 1 或 2，他们 2010 年的松弛 AVL 树允许正秩差<sup>[22](https://www.cs.princeton.edu/courses/archive/spr11/cos423/Lectures/BalancedSearchTreesFinal.pdf)</sup>。

该框架最有用的产物是弱 AVL（WAVL）树，通过松弛 AVL 条件获得。插入或删除后的自底向上再平衡摊还时间为 O(1)，至多两次旋转；相比之下，红黑树删除可能需要多达三次旋转<sup>[6](https://dl.acm.org/doi/10.1145/2689412)</sup>。随着删除的积累，高度上界从 AVL 上界优雅退化，且永远不会比红黑树更差<sup>[6](https://dl.acm.org/doi/10.1145/2689412)</sup>，因此 WAVL 树真正地在 AVL 和红黑树行为之间插值。指数势函数表明再平衡步骤在秩上以指数级频率递减，将再平衡集中在树的底部，这一性质对并发应用很重要<sup>[6](https://dl.acm.org/doi/10.1145/2689412)</sup>。

## 近期研究

伸展树是由斯利特和塔扬开发和研究的自适应二叉搜索树<sup>[23](https://ar5iv.labs.arxiv.org/html/1306.0207)</sup><sup> • </sup><sup>[24](https://courses.csail.mit.edu/6.851/spring12/scribe/lec5.pdf)</sup>。

并发平衡树是一个活跃的前沿领域。最近的一篇预印本扩展了法图鲁（Fatourou）和鲁珀特（Ruppert）的增强技术，产生了一种名为 BAT（平衡增强树）的无锁增强平衡二叉搜索树，能够应对可能在任意位置改变树结构的旋转；它将此技术应用于布朗（Brown）、埃伦（Ellen）和鲁珀特 2014 年的无锁染色二叉搜索树——该树本身基于努尔米（Nurmi）和索伊萨隆-索伊宁（Soisalon-Soininen）1996 年的染色树方案——并展示了如何添加内存回收<sup>[25](https://arxiv.org/html/2601.05225v2)</sup>。另一篇近期预印本提出了一种用于并发二叉搜索树的类伸展旋转设计，它不将访问的节点移到根，而是使用两个深度阈值进行旋转，针对极端偏斜的工作负载（如 Zipf 分布）<sup>[26](https://arxiv.org/html/2606.28889)</sup>。再平衡集中在秩平衡树底部这一点与此直接相关，因为并发树中高处的旋转会使更多并发读取者失效<sup>[6](https://dl.acm.org/doi/10.1145/2689412)</sup>。

## References

1. [Introduction to Algorithms, 3rd ed., Chapter 13: Red-Black Trees (CLRS)](http://koclab.cs.ucsb.edu/teaching/cs130a/docx/07-redblack-chapter.pdf)
2. [Ben Pfaff, libavl documentation](https://stanford.edu/~blp/papers/libavl.pdf)
3. [Search Trees chapter (Goodrich/Tamassia-style), Swarthmore CS35](https://www.cs.swarthmore.edu/courses/CS35/F26/book/Chapter10.pdf)
4. [Balanced Search Trees, Amittai studio notes](https://notes.amittai.studio/algorithms/data-structures/balanced-trees)
5. [Open Data Structures (Pat Morin), Chapter 9: Red-Black Trees](https://opendatastructures.org/newhtml/ods/latex/redblack.html)
6. [Haeupler, Sen, Tarjan, Rank-Balanced Trees, ACM Transactions on Algorithms](https://dl.acm.org/doi/10.1145/2689412)
7. [Sleator & Tarjan, Self-adjusting binary search trees, Journal of the ACM](https://dl.acm.org/doi/10.1145/3828.3835)
8. [Cache Performance of Indexing Data Structures (Univ. of New Mexico)](https://www.cs.unm.edu/~saia/classes/361-s04/project/skiplist.pdf)
9. [DSABook, Section 10.2: Self-balancing trees (Chalmers/GU)](https://chalmersgu-dsa-course.github.io/dsabook/html/section-10.2.html)
10. [Balanced Binary Search Trees (survey)](https://doi.org/10.1007/978-3-031-42209-6_10)
11. [Goodrich & Tamassia, Algorithm Design and Applications, Balanced Search Trees chapter](https://barsky.ca/marina/WC/algoF2021/slides/BalancedBSTBookChapter.pdf)
12. [lib/rbtree.c, Linux kernel source](https://github.com/torvalds/linux/blob/master/lib/rbtree.c)
13. [Jeff Erickson, Algorithms lecture notes: Scapegoat and Splay Trees](https://jeffe.cs.illinois.edu/teaching/algorithms/notes/10-scapegoat-splay.pdf)
14. [Red-black Trees (rbtree) in Linux, kernel documentation](https://www.kernel.org/doc/html/latest/core-api/rbtree.html)
15. [Cache-Opt Skip List & Red-Black Tree, Linux kernel mailing list archive (2001)](https://www.cs.helsinki.fi/linux/linux-kernel/2001-39/0315.html)
16. [An Analysis of the Effects of Spatial Locality on the Cache Performance of Binary Search Trees](https://www.scitepress.org/Papers/2006/13159/13159.pdf)
17. [Comparative Performance of the AVL Tree and Three Variants of the Red-Black Tree, Software: Practice and Experience](https://onlinelibrary.wiley.com/doi/10.1002/spe.3437)
18. [A Comparison of Dictionary Implementations (UNC COMP 550)](https://www.cs.unc.edu/~plaisted/comp550/Neyer%20paper.pdf)
19. [Balanced BSTs (AVL & Red-Black Trees), Ironclad Academy](https://ironclad.academy/dsa/structures/balanced-bst)
20. [libstdc++ stl_tree.h, GCC source](https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/stl_tree.h)
21. [RBTree in kernel::rbtree, Rust kernel bindings 6.14](https://rust.docs.kernel.org/6.14/kernel/rbtree/struct.RBTree.html)
22. [Princeton COS 423 Lecture 4: Balanced Binary Search Trees](https://www.cs.princeton.edu/courses/archive/spr11/cos423/Lectures/BalancedSearchTreesFinal.pdf)
23. [In pursuit of the dynamic optimality conjecture (arXiv survey)](https://ar5iv.labs.arxiv.org/html/1306.0207)
24. [MIT 6.851 lecture notes on dynamic optimality](https://courses.csail.mit.edu/6.851/spring12/scribe/lec5.pdf)
25. [Concurrent Balanced Augmented Trees (BAT), arXiv preprint](https://arxiv.org/html/2601.05225v2)
26. [Concurrent Splay-Based Tree, arXiv preprint](https://arxiv.org/html/2606.28889)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; 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/self-balancing-binary-search-tree

English original: https://www.edgepedia.org/self-balancing-binary-search-tree
