AVL Trees:平衡、旋转与 AVL Sort
Algorithms:AVL Trees and AVL Sort
本文逐段翻译 MIT 6.006 Fall 2011 Lecture 6《AVL Trees and AVL Sort》,在普通 BST 的基础上引入 balanced search tree,说明 AVL invariant 如何保证
O(log n)height,并通过 rotations 实现 insertion 与 AVL sort;最后区分 abstract data type(ADT)和 data structure(DS)。
TL;DR
- 普通 BST 的问题:所有操作是
O(h),tree heighth可能退化为Θ(n)。 - AVL invariant:对每个 node,left child 和 right child 的 heights 至多相差 1;
nil tree的 height 取-1。 - 高度保证:高度为
h的 AVL tree 至少包含 Fibonacci 数量级的 nodes,因此h = O(log n),更精确地有h ≈ 1.440 log₂ n的上界。 - Rebalancing:插入先按普通 BST 完成,然后自下而上更新 heights;遇到最低的不平衡 node 时,用 single rotation 或 double rotation 修复。
- AVL sort:把
n个 elements 插入 AVL tree,再做 inorder traversal,总复杂度为Θ(n log n)。 - ADT 与 DS:ADT 是 operation interface 的 specification;DS 是为这些 operations 选择的具体 algorithm 和 representation。
目录
| 章节 | 说明 |
|---|---|
| Lecture 6:Balanced Binary Search Trees | 为什么 BST 必须保持 balance |
| AVL tree 的定义与高度 | balance invariant 与 height bound |
| Rotations | single rotation 与 double rotation |
| AVL insertion | 插入、更新 height 与恢复 invariant |
| AVL sort | 用 AVL tree 实现 sorting |
| 其他 balanced search trees | 不同平衡树的设计取舍 |
| ADT 与 data structure | interface 与 implementation 的区别 |
| 小结 | 本讲要点 |
Lecture 6:Balanced Binary Search Trees
上一讲的 BST 支持以下 operations:
insert、delete、minimum、maximum、next-larger、next-smaller、search
它们的运行时间都是 O(h),其中 h 是 tree height,也就是 root 的 height。问题是:普通 BST 的 height 取决于插入顺序。
例如,按近似 sorted order 插入 keys,tree 可能接近一条 path:
1
\
2
\
3
\
4
这时 h = Θ(n),所有 search-tree operations 都会退化为 linear time。理想的 perfectly balanced tree 则具有 h = Θ(log n),因此我们需要一种能主动维护 balance 的 BST。
AVL tree 的定义与高度
AVL tree 由 Adel’son-Vel’skii 和 Landis 在 1962 年提出。它要求每一个 node 的两个 child subtree 的 heights 至多相差 1:
| height(left(x)) - height(right(x)) | ≤ 1
为了统一 leaf 和 empty subtree 的处理,规定:
height(nil) = -1
每个 node 保存自己的 height,这是一种 data structure augmentation,和上一讲保存 subtree size 的做法类似。也可以不直接保存 height,而只保存两个 child heights 的 difference;本讲采用保存 height 的方式。
为什么 AVL tree 的 height 是 logarithmic
令 N_h 表示 height 为 h 的 AVL tree 至少包含的 nodes 数量。要让高度尽可能大,每个 node 都尽量接近 balance 的边界:一个 child 的 height 为 h - 1,另一个为 h - 2。因此:
N_h = N_{h-1} + N_{h-2} + 1
其中 +1 是 root。于是:
N_h > 2N_{h-2}
反复应用这个不等式可得:
N_h > 2^{h/2}
如果 tree 有 n 个 nodes,就有:
h < 2 log₂ n
还可以用 Fibonacci numbers 得到更紧的 bound。由于:
N_h = F_{h+2} - 1
而 Fibonacci 数列以 golden ratio φ ≈ 1.618 为底指数增长,所以 AVL tree 的 maximum height 约为:
h ≈ log_φ n ≈ 1.440 log₂ n
因此 AVL invariant 保证 h = O(log n),普通 BST 上以 h 表示的 operations 都获得 O(log n) 上界。
Rotations
插入一个 node 后,可能只有从新 node 到 root 的 path 上出现 balance violation。rotation 的目标是在保持 BST ordering 的同时改变 tree shape,从而降低某个 subtree 的 height。
Left rotation
设 x 的 right child 是 y,且 y 的 left subtree 是 B。对 x 做 left rotation:
Before: After:
x y
/ \ / \
A y x C
/ \ / \
B C A B
rotation 后:
y取代x在 parent 中的位置;x成为y的 left child;y原来的 left subtreeB成为x的 right subtree;- 所有 inorder keys 的顺序不变。
因此它仍然满足 BST property,但可以把 right-heavy 的结构压低。
Right rotation
right rotation 是完全对称的操作。设 x 的 left child 是 y,且 y 的 right subtree 是 B:
Before: After:
x y
/ \ / \
y C A x
/ \ / \
A B B C
y 上升为 subtree root,x 下降为 right child,B 被移动到 x 的 left side。
Single 与 double rotation
如果 violation 的方向一致,例如 left-left 或 right-right,一次 rotation 就足够:
left-left → Right-Rotate(x)
right-right → Left-Rotate(x)
如果方向发生转折,例如 left-right 或 right-left,则需要两次 rotation:
left-right → Left-Rotate(left child),再 Right-Rotate(x)
right-left → Right-Rotate(right child),再 Left-Rotate(x)
double rotation 先把 child subtree 转成直线形,再对最低的不平衡 node 做第二次 rotation。
AVL insertion
AVL insertion 分为两步:
1. 像普通 BST 一样插入新 key;
2. 沿 parent path 向上,更新 heights 并恢复 AVL property。
可以使用如下的 invariant:在向上处理时,假设当前 node 的 child subtrees 已经是 AVL trees,只需要检查当前 node 是否违反 balance condition。
找到最低的不平衡 node
设 x 是从插入位置向 root 方向遇到的最低不平衡 node。若 x 是 right-heavy,left-heavy 的情形完全对称。
令 y = right(x):
- 如果
y是 right-heavy 或 balanced,属于 right-right case,对x做 left rotation; - 如果
y是 left-heavy,属于 right-left case,先对y做 right rotation,再对x做 left rotation。
对应地,left-heavy 的 x 使用镜像操作。
Right-right case
Before: After Left-Rotate(x):
x y
/ \ / \
A y x C
/ \ / \
B C A B
rotation 会使 x 与 y 的 heights 恢复平衡,并保留所有 key 的 sorted order。
Right-left case
第一步 Right-Rotate(y): 第二步 Left-Rotate(x):
x z
/ \ / \
A y x y
/ \ / \ / \
z D A B C D
/ \
B C
其中 z 是 y 的 left child。两次 rotation 后,z 成为 subtree root,四个 subtrees A、B、C、D 的 inorder 顺序保持不变。
Height update
插入和 rotation 后都必须更新受影响 nodes 的 heights:
height(x) = 1 + max(height(left(x)), height(right(x)))
通常从发生变化的 subtree root 继续向上检查 parent。每一层只做 constant work,并且 path 长度是 O(log n),所以 AVL insertion 的复杂度是:
AVL-Insert:O(log n)
示例:插入 23
考虑一棵已有 keys 的 AVL tree,在 29 的 left side 插入 23:
29
/
23
如果 29 的 left subtree 继续使更高的 ancestor 出现 left-left violation,就对相应 ancestor 执行 right rotation。修复后继续向上更新 heights,直到 root;如果某个 rotation 已经使 subtree height 恢复为插入前的高度,剩余 ancestors 通常只需更新并可停止检查。
另一个示例是在 50 右侧插入 55,这会沿 path 传播 height change,并可能在更高的 node 处形成 left-right case。此时执行 double rotation,最终得到同时满足 BST property 和 AVL property 的 tree。
一般来说,一次 insertion 可能需要沿 path 检查多个 ancestors;rotation 的局部修改始终保持 key ordering。
Deletion
delete(包括 delete-min)的思路类似,但比 insertion 更困难:删除可能让多个 ancestors 的 subtree height 逐级下降,因此从删除位置向上可能需要多个 rotations。AVL tree 可以支持 deletion,但实现和 correctness analysis 更复杂。
AVL sort
AVL sort 的算法是:
AVL-Sort(A):
T ← empty AVL tree
for each element a in A:
AVL-Insert(T, a)
Inorder-Tree-Walk(root(T))
因为每次 insertion 都是 O(log n),插入 n 个 elements 的总成本为:
Θ(n log n)
inorder traversal 访问每个 node 一次,成本为 Θ(n)。所以:
AVL-Sort:Θ(n log n) + Θ(n) = Θ(n log n)
AVL sort 的关键优势是它不依赖 input order:AVL invariant 始终保持 height O(log n),不像普通 BST sort 那样在 sorted input 上退化到 Θ(n²)。
其他 balanced search trees
AVL 不是唯一的 balanced search tree。不同结构在严格 balance、update cost、实现复杂度、cache behavior 和 amortized guarantee 之间做不同取舍:
| 结构 | 代表工作 | 主要特点 |
|---|---|---|
| AVL tree | Adel’son-Vel’skii 与 Landis,1962 | 严格 height balance |
| B-tree / 2-3-4 tree | Bayer 与 McCreight,1972 | 适合外部存储;见 CLRS Chapter 18 |
| BB[α] tree | Nievergelt 与 Reingold,1973 | weight-balanced 思路 |
| Red-black tree | CLRS Chapter 13 | 较宽松的 balance,update 常数较好 |
| Splay tree | Sleator 与 Tarjan,1985 | amortized guarantee;无需显式 height balance |
| Skip list | Pugh,1989 | 用 random numbers 获得 high-probability performance |
| Scapegoat tree | Galperin 与 Rivest,1993 | 通过重建 subtree 维持 balance |
| Treap | Seidel 与 Aragon,1996 | random priority 与 BST key 结合 |
符号 (R) 表示结构使用 random numbers,使 operations 以 high probability 很快;(A) 表示 amortized:单次 operation 可能较慢,但把一批 operations 的成本加起来,平均成本很低。
例如,Splay tree 仍然是 data structures 研究中的主题;课程讲义提到 MIT 6.854 Advanced Algorithms 和 6.851 Advanced Data Structures 也会讨论相关内容。
ADT 与 data structure
同一个 abstract data type 可以有很多种具体实现。两者的区别是:
Abstract Data Type(ADT):规定 interface 和 operation semantics
Data Structure(DS):为每个 operation 选择 representation 与 algorithm
例如,priority queue 是一个 ADT,可以用 heap 或 AVL tree 实现。两者都能提供 insert 和 deletemin,但 operation 的复杂度不同:
| Priority Queue operation | heap | AVL tree |
|---|---|---|
new-empty-queue() | Θ(1) | Θ(1) |
Q.insert(x) | Θ(log n) | Θ(log n) |
Q.deletemin() | Θ(log n) | Θ(log n) |
Q.findmin() | Θ(1) | Θ(log n),增广后可到 Θ(1) |
如果 ADT 需要 predecessor 和 successor,heap 就不合适,因为它没有提供 ordered-neighbor information;AVL tree 则可以在 Θ(log n) 内实现:
| Predecessor / Successor operation | heap | AVL tree |
|---|---|---|
new-empty() | Θ(1) | Θ(1) |
S.insert(x) | Θ(log n) | Θ(log n) |
S.delete(x) | Θ(log n) | Θ(log n) |
S.predecessor(x) | Θ(n) | Θ(log n) |
S.successor(x) | Θ(n) | Θ(log n) |
所以选择 data structure 时,应先明确 ADT 的 operation interface,再根据 workload、复杂度、memory layout 和 update/query 比例选择实现。不存在对所有 operations 都最优的单一 data structure。
小结
普通 BST 的瓶颈来自不可控的 tree height。AVL tree 通过对每个 node 维护 child heights 差值不超过 1 的 invariant,把 height 稳定限制在 O(log n)。insertion 先执行普通 BST insertion,再沿 path 更新 heights,并使用 single 或 double rotations 修复最低的不平衡 node。由此可实现 O(log n) 的 search-tree operations 和 Θ(n log n) 的 AVL sort。
更重要的抽象是:priority queue、ordered set 等是 ADT;heap、AVL tree、red-black tree、B-tree 等是不同的 DS 实现。算法设计不仅是选择一个 algorithm,也是在 operation requirements 与 representation 之间做匹配。
参考资料
评论 (0)