构造平衡二叉搜索树(AVL)(Java实现)
定义
在之前的博客中提到过,二叉搜索树虽可以缩短查找的效率,但如果数据有序或接近有序二叉搜索树将退化为单支树,查找元素相当于在顺序表中搜索元素,效率低下。因此,引入了平衡二叉搜索树。当向二叉搜索树中插入新结点后,保证每个结点的左右子树高度之差的绝对值不超过1。
特性
一棵AVL树或者是空树,或者是具有以下性质的二叉搜索树:
- 它的左右子树都是AVL树
- 左右子树高度之差(简称平衡因子)的绝对值不超过1(-1/0/1)
- 如果一棵二叉搜索树是高度平衡的,它就是AVL树。如果它有n个结点,其高度可保持在log2 n,搜索时间复杂度O(log2 n)
AVL树的插入
(1)按照二叉搜索树的方式插入新节点
(2)调整节点的平衡因子
新结点插入后,AVL树的平衡性可能会遭到破坏,此时就需要更新平衡因子,并检测是否破坏了AVL树的平衡性。本文bf = H(left) - H(right),来看一下插入结点后parent结点的bf的变化。
插入前parent.bf的值(平衡) | 插入位置 | 插入后parent.bf的值(可能不平衡) |
---|---|---|
-1 | left | 0 |
-1 | right | -2 |
0 | left | 1 |
0 | right | -1 |
1 | left | 2 |
1 | right | 0 |
当插入节点后,若平衡因子的值为**(-2或2),**那么就要对树的结构进行调整。根据结点插入位置的不同,AVL树的失衡情况分为四种 。
-
左左失衡 对失衡结点右旋
正常插入 调整树结构 -
左右失衡 先对失衡结点的left左旋,再对失衡结点右旋
正常插入 调整树结构 调整树结构 -
右右失衡 对失衡结点左旋
正常插入 调整树结构 -
右左失衡 先对失衡结点的right右旋,再对失衡结点左旋
正常插入 调整树结构 调整树结构
旋转
下面只对左旋进行分析。
正常插入 | 调整树结构 |
---|---|
在上图中出现了右右失衡,我们对失衡结点parent进行左旋,可以看到我们需要更改三对连接关系。
private void leftRotate(Node parent){
//定义需要更改连接的结点
Node parentOfParent = parent.parent;
Node right = parent.right;
Node leftOfRight = right.left;
//失衡结点的右子树上升
right.parent = parentOfParent;
if (parentOfParent == null){
root = right;
}else if (parent == parentOfParent.left){
parentOfParent.left = right;
}else {
parentOfParent.right = right;
}
right.left = parent;
parent.parent = right;
parent.right = leftOfRight;
if (leftOfRight != null){
leftOfRight.parent = parent;
}
}
具体代码实现
import java.util.Random;
import java.util.Stack;
// 定义节点
class Node{
int key;
int bf; // 平衡因子 等于H(left) - H(right)
Node left;
Node right;
Node parent;
public Node(int key,Node parent) {
this.key = key;
this.bf = 0;
this.left = null;
this.right = null;
this.parent = parent;
}
}
public class AVLTree {
private Node root = null;
private void insert(int key){
if (root == null){
root = new Node(key,null);
return;
}
Node parent = null;
Node cur = root;
while (cur != null){
if (cur.key == key){
throw new RuntimeException(key +"已经重复了");
}else if (cur.key > key){
parent = cur;
cur = cur.left;
}else {
parent = cur;
cur = cur.right;
}
}
if (key < parent.key){
cur = parent.left = new Node(key,parent);
}else{
cur = parent.right = new Node(key,parent);
}
//以上操作完成正常搜索树的插入过程
// cur 是新插入的结点 ,parent是要调整bf的结点
while (true){
//更新parent的 bf
if (cur == parent.left){
parent.bf++;
}else{
parent.bf--;
}
if (parent.bf == 0){
break;
}else if (parent.bf == 2){
//左左失衡
if (cur.bf == 1){
fixLeftLeftLoseBalance(parent);
}else{
//左右失衡
fixLeftRightLoseBalance(parent);
}
break;
}else if (parent.bf == -2){
//右右失衡
if (cur.bf == -1){
fixRightRightLoseBalance(parent);
}else {
//右左失衡
fixRightLeftLoseBalance(parent);
}
break;
} else if (parent == root) {
break;
}
// 继续向上蔓延
cur = parent;
parent = parent.parent;
}
}
// 左旋
private void leftRotate(Node parent){
Node parentOfParent = parent.parent;
Node right = parent.right;
Node leftOfRight = right.left;
right.parent = parentOfParent;
if (parentOfParent == null){
root = right;
}else if (parent == parentOfParent.left){
parentOfParent.left = right;
}else {
parentOfParent.right = right;
}
right.left = parent;
parent.parent = right;
parent.right = leftOfRight;
if (leftOfRight != null){
leftOfRight.parent = parent;
}
}
// 右旋
private void rightRotate(Node parent){
Node parentOfParent = parent.parent;
Node left = parent.left;
Node rightOfLeft = left.right;
left.parent = parentOfParent;
if (parentOfParent == null){
root = left;
}else if (parent == parentOfParent.left){
parentOfParent.left = left;
}else {
parentOfParent.right = left;
}
left.right = parent;
parent.parent = left;
parent.left = rightOfLeft;
if (rightOfLeft != null){
rightOfLeft.parent = parent;
}
}
// 左左失衡调整
private void fixLeftLeftLoseBalance(Node parent) {
Node node = parent;
Node leftOfNode = node.left;
//对失衡结点右旋
rightRotate(node);
node.bf = leftOfNode.bf = 0;
}
// 左右失衡调整
private void fixLeftRightLoseBalance(Node parent) {
Node node = parent;
Node leftOfNode = node.left;
Node rightOfLeftOfNode = leftOfNode.right;
// 先对失衡结点的左子树左旋
leftRotate(leftOfNode);
//对失衡结点右旋
rightRotate(node);
//更新各个结点的bf
if (rightOfLeftOfNode.bf == 1){
node.bf = -1;
leftOfNode.bf = 0;
rightOfLeftOfNode.bf = 0;
}else if (rightOfLeftOfNode.bf == -1){
node.bf = 0;
leftOfNode.bf = 1;
rightOfLeftOfNode.bf = 0;
}else{
node.bf = leftOfNode.bf = rightOfLeftOfNode.bf = 0;
}
}
// 右右失衡调整
private void fixRightRightLoseBalance(Node parent) {
Node node = parent;
Node rightOfNode = node.right;
//对失衡结点左旋
leftRotate(node);
node.bf = rightOfNode.bf = 0;
}
// 右左失衡调整
private void fixRightLeftLoseBalance(Node parent) {
Node node = parent;
Node rightOfNode = node.right;
Node leftOfRightOfNode = rightOfNode.left;
// 对失衡结点的右子树右旋
rightRotate(rightOfNode);
// 对失衡结点左旋
leftRotate(node);
if (leftOfRightOfNode.bf == 1){
rightOfNode.bf = -1;
parent.bf = 0;
leftOfRightOfNode.bf = 0;
}else if (leftOfRightOfNode.bf == -1){
node.bf = 1;
rightOfNode.bf = 0;
leftOfRightOfNode.bf = 0;
}else{
node.bf = rightOfNode.bf = leftOfRightOfNode.bf = 0;
}
}
private boolean contains(int key){
Node cur = root;
while (cur != null){
if (cur.key == key){
return true;
}else if (cur.key > key){
cur = cur.right;
}else{
cur = cur.left;
}
}
return false;
}
private void inOrder(){
Node cur = root;
Stack<Node> stack = new Stack<>();
while (!stack.isEmpty() || cur != null){
while (cur != null){
stack.push(cur);
cur = cur.left;
}
cur = stack.pop();
System.out.print(cur.key + " ");
cur = cur.right;
}
}
public static void main(String[] args) {
Random random = new Random(20210317);
AVLTree tree = new AVLTree();
for (int i = 0; i < 15; i++ ){
int r = random.nextInt(100);
try {
tree.insert(r);
}catch (RuntimeException e){
System.out.println(e.getMessage());
}
}
tree.inOrder();
}
}
AVL树性能分析
AVL树是一棵绝对平衡的二叉搜索树,其要求每个节点的左右子树高度差的绝对值都不超过1,这样可以保证查询时高效的时间复杂度,即O(log 2 (N))。但是如果要对AVL树做一些结构修改的操作,性能非常低下,比如:插入时要维护其绝对平衡,旋转的次数比较多,更差的是在删除时,有可能一直要让旋转持续到根的位置。因此:如果需要一种查询高效且有序的数据结构,而且数据的个数为静态的(即不会改变),可以考虑AVL树,但一个结构经常修改,就不太适合。