1. 为什么使用「平衡二叉树」
二叉搜索树能提高查询的效率 O(logn),但是当你插入 {1,2,3,4,5,6} 这种数据的时候,你的二叉树就像一个「链表」一样,搜索效率变为 O(n)
于是在 1962 年,一个姓 AV 的大佬(G. M. Adelson-Velsky) 和一个姓 L 的大佬( Evgenii Landis)提出「平衡二叉树」(AVL) 。
插入 {1,2,3,4,5,6} 这种数据结果如下图所示:
2. 判断「平衡二叉树」
判断「平衡二叉树」的 2 个条件:
-
- 是「二叉排序树」
-
- 任何一个节点的左子树或者右子树都是「平衡二叉树」(左右高度差小于等于 1)
(1)下图不是「平衡二叉树」因为它不是「二叉排序树」违反第 1 条件
(2)下图不是「平衡二叉树」因为有节点子树高度差大于 1 违法第 2 条件
(3)下图是「平衡二叉树」因为符合 1、2 条件
3. 相关概念
3.1 平衡因子 BF(Balance Factor)
- 定义: 左子树和右子树高度差
- 计算: 左子树高度 - 右子树高度的值(可能是负值,表示右子树高于左子树)
一般来说 BF 的绝对值大于 1,,平衡树二叉树就失衡,需要「旋转」纠正
3.2 最小不平衡子树
最小不平衡子树: 在新插入的结点向上查找,以第一个平衡因子的绝对值超过 1 的结点**(平衡因子 BF 变成了 -2 或 2)**为根的子树称为最小不平衡子树。也就是说,一棵失衡的树,是有可能有多棵子树同时失衡的。而这个时候,我们只要调整最小的不平衡子树,就能够将不平衡的树调整为平衡的树。
「旋转」纠正只需要纠正「最小不平衡子树」即可
4. 二种旋转方式
2 种「旋转」方式:
- 左旋
- 旧根节点为新根节点的左子树
- 新根节点的左子树(如果存在)为旧根节点的右子树
- 右旋:
- 旧根节点为新根节点的右子树
- 新根节点的右子树(如果存在)为旧根节点的左子树
右旋
左旋
5. 4 种「旋转」纠正类型:
- LL 型:插入左孩子的左子树导致不平衡,右旋
- RR 型:插入右孩子的右子树导致不平衡,左旋
- LR 型:插入左孩子的右子树导致不平衡,先左旋,再右旋
- RL 型:插入右孩子的左子树导致不平衡,先右旋,再左旋
5.1 LL 型失衡「右旋」
由于在被破坏节点的的左孩子 (L) 的左子树 (L) 上插入新结点,使原来平衡二叉树变得不平衡,此时 A 的平衡因子由 1 增至 2。
如下: 第三个节点「1」插入的 时候,BF(3) = 2,BF(2) = 1
,LL 型失衡,右旋,根节点顺时针旋转
5.2 RR 型失衡「左旋」
由于在被破坏节点的右孩子(R)的右子树(R)上插入了新结点,平衡因子由 -1 减至 -2,导致以被破坏节点为根的子树失去平衡,需要一次向左的旋转操作。
如下: 第三个节点「3」插入的 时候,BF(1)=-2 BF(2)=-1
,RR 型失衡,左旋,根节点逆时针旋转
5.3 LR 型
LR平衡旋转(先左后右双旋转)。由于在被破坏节点的左孩子(L)的右子树(R)上插入新结点,被破坏节点的平衡因子由1增至2,导致以被破坏节点为根的子树失去平衡,需要进行两次旋转操作,先左旋转后右旋转。先将被破坏结点的左孩子1的右子树的根结点2向左上旋转提升到1结点的位置,然后再把该2结点向右上旋转提升到1结点的位置
如下: 第三个节点「2」插入的 时候,BF(3)=2 BF(1)=-1
, LR 型失衡,先「左旋」变为 LL 型,再「右旋」
(1)最小不平衡子树左子树 {2,1} 先左旋
(2)最小不平衡子树 {3,2,1} 再右旋
5.4 RL 型
第三个节点「1」插入的 时候,BF(1)=-2 BF(3)=1
RL 型失衡,先「右旋」再「左旋」
(1)最小不平衡子树根节点右子树{3,2}先右旋
(2)最小不平衡子树 {1,2,3} 再左旋(L)
5. 实例
实例一
接下来以 {3,2,1,4,5,6,7,10,9,8} 为实例练习刚刚的 4 种插入方式
(1)依次插入 3、2、1 插入第三个点 1 的时候 BF(3)=2 BF(2)=1,LL 型失衡。对最小不平衡树 {3,2,1}进行「右旋」
(2)依次插入 4 ,5 插入 5 点的时候 BF(3) = -2 BF(4)=-1,RR 型失衡。对最小不平衡树 {3,4,5} 进行「左旋」
(3)插入 4 ,5 插入 5 点的时候 BF(2)=-2 BF(4)=-1 ,RR 型失衡 对最小不平衡树进行「左旋」
(4)插入 7 节点的时候 BF(5)=-2, BF(6)=-1 ,RR 型失衡,对最小不平衡树 进行「左旋」
(5)依次插入 10 ,9 。插入 9 点的时候 BF(10) = 1,BF(7) = -2 ,RL 型失衡,先「右旋」再「左旋」
(6)最后插入节点 8 ,BF(6)=-2 BF(9)=1,RL 型失衡,先「右旋」再「左旋」
结束
实例二
选取一组数据分别为2,1,0,3,4,5,6,9,8,7的10个结点来构造平衡二叉树。
首先数据为2的结点作为根结点插入,接着插入1,仍是平衡的,再插入0是,2的平衡因子变为2,此时出现了不平衡,因此需要进行调整,最低不平衡结点为2,属于LL型,调整过程如图所示。
接着插入3,是平衡的,再插入4,此时出现了不平衡,结点 1 和 2 的平衡因子都为 -2,结点2为最低不平衡结点,属于RR型,调整过程如图所示
接着插入5,此时结点 1 的平衡因子为 -2,导致不平衡,结点1为最低不平衡结点,属于RR型,调整如图所示。
接着插入6,此时结点4的平衡因子为 -2,导致不平衡,结点4为最低不平衡结点,属于RR型,调整如图所示。
接着插入9,是平衡的,再插入8,此时结点 3、5、6 的平衡因子都为 -2,导致不平衡,结点6为最低不平衡结点,属于RL型,调整如图所示。
插入7,此时结点3、5的平衡因子为 -2,导致不平衡,最低不平衡结点为5,属于RL型,调整如图所示。
6.代码实现
6.1 定义节点
public class AVLNode {
/** 数据 **/
public int data;
/** 相对高度 **/
public int height;
/** 父节点 **/
public AVLNode parent;
/** 左子树 **/
public AVLNode left;
/** 右子树 **/
public AVLNode right;
public AVLNode(int data) {
this.data = data;
this.height = 1;
}
}
6.2 计算高度
节点高度等于左子树和右子树最大高度 + 1
/** 通过子树高度 计算高度 **/
private int calcHeight(AVLNode root) {
if (root.left == null && root.right == null) {
return 1;
}
else if (root.right == null) {
return root.left.height + 1;
} else if (root.left == null) {
return root.right.height + 1;
}else {
return root.left.height > root.right.height ? root.left.height + 1 : root.right.height + 1;
}
}
6.3 计算 BF
BF(平衡因子)的值为:左子树高度 - 右子树高度
private int calcBF(AVLNode root) {
if (root == null){
return 0;
}
else if (root.left == null && root.right == null) {
return 0;
}
else if (root.right == null) {
return root.left.height ;
} else if (root.left == null) {
return - root.right.height;
}else {
return root.left.height - root.right.height;
}
}
6.4 旋转
2 种「旋转」方式:
- 左旋
- 旧根节点为新根节点的左子树
- 新根节点的左子树(如果存在)为旧根节点的右子树
- 右旋:
- 旧根节点为新根节点的右子树
- 新根节点的右子树(如果存在)为旧根节点的左子树
重点理解: 旋转之后通过需要刷新高度
高度变化只有: oldRoot 和 newRoot
但是它们子树的高度是不变的(这很关键)
我们可以通过它们 子树的高度计算他们的高度
使用不变的因数计算变化的因素是一个很好的思维
public AVLNode leftRotate(AVLNode root) {
AVLNode oldRoot = root;
AVLNode newRoot = root.right;
AVLNode parent = root.parent;
//1.newRoot 替换 oldRoot 位置
if (null != parent ) {
if (oldRoot.parent.data > oldRoot.data) {
parent.left = newRoot;
}else {
parent.right = newRoot;
}
}
newRoot.parent = parent;
//2.重新组装 oldRoot (将 newRoot 的左子树 给 oldRoot 的右子树)
oldRoot.right = newRoot.left;
if (newRoot.left != null) {
newRoot.left.parent = oldRoot;
}
//3. oldRoot 为 newRoot 的左子树
newRoot.left = oldRoot;
oldRoot.parent = newRoot;
//刷新高度
oldRoot.height = calcHeight(oldRoot);
newRoot.height = calcHeight(newRoot);
return newRoot;
}
public AVLNode rightRotate(AVLNode root) {
AVLNode oldRoot = root;
AVLNode newRoot = root.left;
AVLNode parent = root.parent;
//1.newRoot 替换 oldRoot 位置
if (null != parent ) {
if (oldRoot.parent.data > oldRoot.data) {
parent.left = newRoot;
}else {
parent.right = newRoot;
}
}
newRoot.parent = parent;
//2.重新组装 oldRoot (将 newRoot 的右子树 给 oldRoot 的左子树)
oldRoot.left = newRoot.right;
if (newRoot.right != null) {
newRoot.right.parent = oldRoot;
}
//3. oldRoot 为 newRoot 的左子树
newRoot.right = oldRoot;
oldRoot.parent = newRoot;
//刷新高度
oldRoot.height = calcHeight(oldRoot);
newRoot.height = calcHeight(newRoot);
return newRoot;
}
6.5 插入(总代码)
插入操作
- 递归插入新节点
- 刷新高度
- 旋转并再次刷新高度
public class ALVTree {
AVLNode root;
public void insert(int data) {
if (null == this.root) {
this.root = new AVLNode(data);
return;
}
this.root = insert(this.root, data);
}
public AVLNode insert(AVLNode root, int data) {
//插入左子树
if (data < root.data) {
if (null == root.left) {
root.left = new AVLNode(data);
root.left.parent = root;
}else {
insert(root.left,data);
}
}
//插入右子树
else if (data > root.data) {
if (null == root.right) {
root.right = new AVLNode(data);
root.right.parent = root;
} else {
insert(root.right,data);
}
}
//刷新高度
root.height = calcHeight(root);
//旋转
//1. LL 型 右旋转
if (calcBF(root) == 2){
//2. LR 型 先左旋转
if (calcBF(root.left) == -1) {
root.left = leftRotate(root.left);
}
root = rightRotate(root);
}
//3. RR型 左旋转
if (calcBF(root) == -2){
//4. RL 型 先右旋转
if (calcBF(root.right)== 1) {
root.right = rightRotate(root.right);
}
root = leftRotate(root);
}
return root;
}
public AVLNode leftRotate(AVLNode root) {
AVLNode oldRoot = root;
AVLNode newRoot = root.right;
AVLNode parent = root.parent;
//1.newRoot 替换 oldRoot 位置
if (null != parent ) {
if (oldRoot.parent.data > oldRoot.data) {
parent.left = newRoot;
}else {
parent.right = newRoot;
}
}
newRoot.parent = parent;
//2.重新组装 oldRoot (将 newRoot 的左子树 给 oldRoot 的右子树)
oldRoot.right = newRoot.left;
if (newRoot.left != null) {
newRoot.left.parent = oldRoot;
}
//3. oldRoot 为 newRoot 的左子树
newRoot.left = oldRoot;
oldRoot.parent = newRoot;
//刷新高度
oldRoot.height = calcHeight(oldRoot);
newRoot.height = calcHeight(newRoot);
return newRoot;
}
public AVLNode rightRotate(AVLNode root) {
AVLNode oldRoot = root;
AVLNode newRoot = root.left;
AVLNode parent = root.parent;
//1.newRoot 替换 oldRoot 位置
if (null != parent ) {
if (oldRoot.parent.data > oldRoot.data) {
parent.left = newRoot;
}else {
parent.right = newRoot;
}
}
newRoot.parent = parent;
//2.重新组装 oldRoot (将 newRoot 的右子树 给 oldRoot 的左子树)
oldRoot.left = newRoot.right;
if (newRoot.right != null) {
newRoot.right.parent = oldRoot;
}
//3. oldRoot 为 newRoot 的左子树
newRoot.right = oldRoot;
oldRoot.parent = newRoot;
//刷新高度
oldRoot.height = calcHeight(oldRoot);
newRoot.height = calcHeight(newRoot);
return newRoot;
}
/** 通过子树高度 计算高度 **/
private int calcHeight(AVLNode root) {
if (root.left == null && root.right == null) {
return 1;
}
else if (root.right == null) {
return root.left.height + 1;
} else if (root.left == null) {
return root.right.height + 1;
}else {
return root.left.height > root.right.height ? root.left.height + 1 : root.right.height + 1;
}
}
private int calcBF(AVLNode root) {
if (root == null){
return 0;
}
else if (root.left == null && root.right == null) {
return 0;
}
else if (root.right == null) {
return root.left.height ;
} else if (root.left == null) {
return - root.right.height;
}else {
return root.left.height - root.right.height;
}
}
}
AVL树的问题
既然AVL树可以保证二叉树的平衡,这就意味着AVL搜索的时候,它最坏情况的时间复杂度O(logn) ,要低于普通二叉树BST和链表的最坏情况O(n)。
那么HashMap直接使用AVL树来替换链表就好了,为什么选择用红黑树呢?
原因是:
由于AVL树必须保证左右子树平衡,Max(最大树高-最小树高) <= 1,
所以在插入的时候很容易出现不平衡的情况,一旦这样,就需要进行旋转以求达到平衡。
正是由于这种严格的平衡条件,导致AVL需要花大量时间在调整上,故AVL树一般使用场景在于查询场景, 而不是 增加删除 频繁的场景。
红黑树(rbt)做了什么优化呢?
红黑树(rbt)继承了AVL可自平衡的优点,
同时, 红黑树(rbt)在查询速率和平衡调整中寻找平衡,放宽了树的平衡条件,从而可以用于 增加删除 频繁的场景。
在实际应用中,红黑树的使用要多得多。