AVL 树是一种平衡二叉树,得名于其发明者的名字。平衡二叉树递归定义如下
- 左右子树的高度差小于等于 1。
- 其每一个子树均为平衡二叉树。
实现原理
为了保证二叉树的平衡, AVL 树引入了所谓监督机制,就是在树的某一部分的不平衡度超过一个阈值后触发相应的平衡操作。保证树的平衡度在可以接受的范围内。
引入了高度属性的 AVL 树的节点定义如下:
static class Node {
int value;
int height;
Node left;
Node right;
}
高度的调整:
public static void updateHeight(Node root) {
if (root == null) {
return;
}
root.height = Math.max(root.left.height, root.right.height) + 1;
}
树的平衡化操作
二叉树的平衡化有两大基础操作: 左旋和右旋。左旋,即是逆时针旋转;右旋,即是顺时针旋转。这种旋转在整个平衡化过程中可能进行一次或多次,这两种操作都是从失去平衡的最小子树根结点开始的(即离插入结点最近且平衡因子超过1的祖结点)。
右旋操作
所谓右旋操作,就是把上图中的 B 节点和 C 节点进行所谓 ”父子交换” 。
-
当 B 节点处存在右孩子时,将其右孩子和旋转后的节点 C 相连,成为节点 C 的左孩子。
private Node avlRotateRight(Node root) { Node left = root.left; // 将要被抛弃的节点连接为旋转后的 root 的左孩子 root.left = left.right; // 调换父子关系 left.right = root; updateHeight(root); updateHeight(left); return left; }
左旋操作
左旋操作和右旋操作十分类似,唯一不同的就是需要将左右互换下。
private Node avlRotateLeft(Node root) {
Node right = root.right;
// 将要被抛弃的节点连接为旋转后的 root 的右孩子
root.right = right.left;
// 调换父子关系
right.left = root;
updateHeight(root);
updateHeight(right);
return right;
}
需要平衡的四种情况
1、LL 型
![img](https://pic3.zhimg.com/v2-58c02894bce9e580481c075efc35f226_b.jpg)
所谓 LL 型就是上图左边那种情况,即因为在根节点的左孩子的左子树添加了新节点,导致树的高度差变为 +2,二叉树失去平衡。对于这种情况,对节点 n 右旋一次即可。
2、RR 型
![img](https://pic3.zhimg.com/v2-58c02894bce9e580481c075efc35f226_b.jpg)
RR 型的情况和 LL 型完全对称。只需要对节点 n 进行一次左旋即可修正。
3、LR 型
![img](https://pic3.zhimg.com/v2-acc2de5f4d30d41a6b39b0c5a9b2dbbe_b.jpg)
LR 就是将新的节点插入到了 n 的左孩子的右子树上导致的不平衡的情况。这时我们需要的是先对 i 进行一次左旋再对 n 进行一次右旋。
4、RL型
RL 就是将新的节点插入到了 n 的右孩子的左子树上导致的不平衡的情况。这时我们需要的是先对 i 进行一次右旋再对 n 进行一次左旋。
平衡操作的判断及实现
也很简单,我们可以根据破坏树的平衡性(子树高度差的绝对值大于 1)的节点以及其子节点的平衡因子来判断平衡化类型。
private Node reBalance(Node root) {
int factor = getBalanceFactor(root); // root 子树高度差
if (factor > 1) {
int leftFactor = getBalanceFactor(root.left);
if (leftFactor > 0) {
// LL
return avlRotateRight(root);
} else {
// LR
root.left = avlRotateLeft(root.left);
return avlRotateRight(root);
}
} else if (factor < -1){
int rightFactor = getBalanceFactor(root.right);
if (rightFactor <= 0) {
// RR
return avlRotateLeft(root);
} else {
// RL
root.right = avlRotateRight(root.right);
return avlRotateLeft(root);
}
} else {
// 无事发生
updateHeight(root);
return root;
}
}
AVL 树的插入和删除操作
基于上文的再平衡操作,现在我们可以写出完整的 AVL 树的插入/删除操作。
插入操作
/**
* 插入节点
*
* @param root 树的根节点
* @param value 要插入节点的值
* @return 返回调整后的根节点
*/
public static Node insert(Node root, int value) {
Node newNode;
if (root == null) {
newNode = new Node();
newNode.value = value;
return newNode;
} else if (root.value == value) {
// 不允许插入重复值
return root;
} else {
if (root.value < value) {
root.right = insert(root.right, value);
} else {
root.left = insert(root.left, value);
}
return reBalance(root);
}
}
删除操作
/**
* 删除节点
* @param root 树的根节点
* @param value 删除的值
* @return 新根节点
*/
public static Node delete(Node root, int value) {
if (root == null) {
return null;
} else if (root.value == value) {
// 当前root节点就是要删除的节点
if (root.left == null && root.right == null) {
// 要删除的节点无左右子孩子
return null;
} else if (root.left == null) {
// 要删除的节点无左孩子,有右孩子
return root.right;
} else if (root.right == null) {
// 要删除的节点无右孩子,有左孩子
return root.left;
} else {
// 想删除的节点既有左子树,也有右子树
if (root.right.left == null) {
// 右节点就是最小节点
root.right.left = root.left;
return reBalance(root.right);
} else {
// 右节点不是最小节点,把右子树的最小值节点与当前节点替换(值替换)
root.value = deleteMinNode(root.right.left, root.right);
root.right = reBalance(root.right);
return reBalance(root);
}
}
} else {
if (root.value < value) {
root.right = delete(root.right, value);
} else {
root.left = delete(root.left, value);
}
return reBalance(root);
}
}
全部代码实现
public class Avl {
static class Node {
int value;
int height;
Node left;
Node right;
public Node() {
height = 1;
left = null;
right = null;
}
}
/**
* 右旋以root为根的树,返回右旋后树的根节点
*/
private static Node avlRotateRight(Node root) {
Node left = root.left;
// 将要被抛弃的节点连接为旋转后的 root 的左孩子
root.left = left.right;
// 调换父子关系
left.right = root;
updateHeight(root);
updateHeight(left);
return left;
}
/**
* 左旋以root为根的树,返回左旋后树的根节点
*/
private static Node avlRotateLeft(Node root) {
Node right = root.right;
// 将要被抛弃的节点连接为旋转后的 root 的右孩子
root.right = right.left;
// 调换父子关系
right.left = root;
updateHeight(root);
updateHeight(right);
return right;
}
/**
* 高度调整
*/
private static void updateHeight(Node root) {
if (root == null) {
return;
}
if (root.left == null && root.right == null) {
root.height = 1;
} else if (root.left == null) {
root.height = root.right.height + 1;
} else if (root.right == null) {
root.height = root.left.height + 1;
} else {
root.height = Math.max(root.left.height, root.right.height) + 1;
}
}
/**
* 获取树的平衡因子,也就是子树高度差
*/
private static int getBalanceFactor(Node root) {
if (root == null) {
return 0;
}
int leftHeight = root.left == null ? 0 : root.left.height;
int rightHeight = root.right == null ? 0 : root.right.height;
return leftHeight - rightHeight;
}
/**
* 平衡化操作,返回平衡后的根节点
*/
private static Node reBalance(Node root) {
int factor = getBalanceFactor(root);
if (factor > 1) {
int leftFactor = getBalanceFactor(root.left);
if (leftFactor > 0) {
// LL
return avlRotateRight(root);
} else {
// LR
root.left = avlRotateLeft(root.left);
return avlRotateRight(root);
}
} else if (factor < -1) {
int rightFactor = getBalanceFactor(root.right);
if (rightFactor <= 0) {
// RR
return avlRotateLeft(root);
} else {
// RL
root.right = avlRotateRight(root.right);
return avlRotateLeft(root);
}
} else {
// 无事发生
updateHeight(root);
return root;
}
}
/**
* 删除当前树的最小节点,并返回最小值
* @param node parent的左节点
* @param parent 要删除节点
* @return 最小值
*/
private static int deleteMinNode(Node node, Node parent) {
assert node != null;
if (node.left == null) {
parent.left = node.right;
return node.value;
} else {
int min = deleteMinNode(node.left, node);
parent.left = reBalance(node);
return min;
}
}
/**
* 插入节点
*
* @param root 树的根节点
* @param value 要插入节点的值
* @return 返回调整后的根节点
*/
public static Node insert(Node root, int value) {
Node newNode;
if (root == null) {
newNode = new Node();
newNode.value = value;
return newNode;
} else if (root.value == value) {
// 不允许插入重复值
return root;
} else {
if (root.value < value) {
root.right = insert(root.right, value);
} else {
root.left = insert(root.left, value);
}
return reBalance(root);
}
}
/**
* 删除节点
* @param root 树的根节点
* @param value 删除的值
* @return 新根节点
*/
public static Node delete(Node root, int value) {
if (root == null) {
return null;
} else if (root.value == value) {
// 当前root节点就是要删除的节点
if (root.left == null && root.right == null) {
// 要删除的节点无左右子孩子
return null;
} else if (root.left == null) {
// 要删除的节点无左孩子,有右孩子
return root.right;
} else if (root.right == null) {
// 要删除的节点无右孩子,有左孩子
return root.left;
} else {
// 想删除的节点既有左子树,也有右子树
if (root.right.left == null) {
// 右节点就是最小节点
root.right.left = root.left;
return reBalance(root.right);
} else {
root.value = deleteMinNode(root.right.left, root.right);
root.right = reBalance(root.right);
return reBalance(root);
}
}
} else {
if (root.value < value) {
root.right = delete(root.right, value);
} else {
root.left = delete(root.left, value);
}
return reBalance(root);
}
}
}