1. AVL树概述
AVL树的名字来源于它的发明作者G.M. Adelson-Velsky 和 E.M.
Landis。AVL树是最先发明的自平衡二叉查找树(Self-Balancing Binary Search Tree,简称平衡二叉树)。平衡二叉树定义(AVL): 它或者是一颗空树,或者具有以下性质的二叉排序树:它的左子树和右子树的深度之差(平衡因子)的绝对值不超过1,且它的左子树和右子树都是一颗平衡二叉树
一棵AVL树有如下必要条件:
- 条件一:它必须是二叉查找树。
- 条件二:每个节点的左子树和右子树的高度差至多为1。
AVL树的查找、插入、删除操作在平均和最坏的情况下都是O(logn),这得益于它时刻维护着二叉树的平衡
AVL树有两个重要概念
① 平衡因子: 将二叉树上节点的左子树高度减去右子树高度的值称为该节点的平衡因子BF(Balance Factor)
② 最小不平衡子树: 距离插入节点最近的,且平衡因子的绝对值大于1的节点为根的子树
public class AVLTree <Key extends Comparable<Key>, Value> {
private Node root;
private class Node{
private Key key;
private Value value;
private int height;
private Node left, right;
}
public int height(Node n){
if(n == null){
return 0;
}
return n.height;
}
public Value get(Key key){
//todo
return null;
}
public void put(Key key, Value value){
//todo
}
//max(),min(),floor(),ceiling()
//select(),rank()
//delete(),deleteMin(),deleteMax()
2. AVL树的平衡调整
如果在AVL树中进行插入或删除节点后,可能导致AVL树失去平衡。这种失去平衡的可以概括为4种姿态:LL(左左),LR(左右),RR(右右)和RL(右左)
平衡二叉树详解
2.1 LL
型调整
由于在A的左孩子(L)的左子树(L)上插入新结点,使原来平衡二叉树变得不平衡,此时A的平衡因子由1增至2。下面图1是LL型的最简单形式。显然,按照大小关系,结点B应作为新的根结点,其余两个节点分别作为左右孩子节点才能平衡,A结点就好像是绕结点B顺时针旋转一样
LL型调整的一般形式如下图2所示,表示在A的左孩子B的左子树BL(不一定为空)中插入结点(图中阴影部分所示)而导致不平衡( h 表示子树的深度)。这种情况调整如下:①将A的左孩子B提升为新的根结点;②将原来的根结点A降为B的右孩子;③各子树按大小关系连接(BL和AR不变,BR调整为A的左子树)
代码实现
如下所示,y是失衡节点,所以对y进行右旋
public Node llRotate(Node y){
Node x = y.left;
y.left = x.right;
x.right = y;
y.height = Math.max(height(y.left), height(y.right))+1;
x.height = Math.max(height(x.left), height(x.right))+1;
return x;
}
2.2 RR
型调整
由于在A的右孩子®的右子树®上插入新结点,使原来平衡二叉树变得不平衡,此时A的平衡因子由-1变为-2。图3是RR型的最简单形式。显然,按照大小关系,结点B应作为新的根结点,其余两个节点分别作为左右孩子节点才能平衡,A结点就好像是绕结点B逆时针旋转一样
RR型调整的一般形式如下图4所示,表示在A的右孩子B的右子树BR(不一定为空)中插入结点(图中阴影部分所示)而导致不平衡( h 表示子树的深度)。这种情况调整如下:
① 将A的右孩子B提升为新的根结点;
② 将原来的根结点A降为B的左孩子
③ 各子树按大小关系连接(AL和BR不变,BL调整为A的右子树)。
代码实现
public Node rrRotate(Node y){
Node x = y.right;
y.right = x.left;
x.left = y;
y.height = Math.max(height(y.left), height(y.right))+1;
x.height = Math.max(height(x.left), height(x.right))+1;
return x;
}
2.3 LR
型调整
由于在A的左孩子(L)的右子树®上插入新结点,使原来平衡二叉树变得不平衡,此时A的平衡因子由1变为2。图5是LR型的最简单形式。显然,按照大小关系,结点C应作为新的根结点,其余两个节点分别作为左右孩子节点才能平衡
LR型调整的一般形式如下图6所示,表示在A的左孩子B的右子树(根结点为C,不一定为空)中插入结点(图中两个阴影部分之一)而导致不平衡( h 表示子树的深度)。这种情况调整如下:①将B的左孩子C提升为新的根结点;②将原来的根结点A降为C的右孩子;③各子树按大小关系连接(BL和AR不变,CL和CR分别调整为B的右子树和A的左子树)。
代码实现
public Node lrRotate(Node y){
Node x = y.left;
y.left = rrRotate(x);
return llRotate(y);
}
2.4 RL
型调整
由于在A的右孩子®的左子树(L)上插入新结点,使原来平衡二叉树变得不平衡,此时A的平衡因子由-1变为-2。图7是RL型的最简单形式。显然,按照大小关系,结点C应作为新的根结点,其余两个节点分别作为左右孩子节点才能平衡
RL型调整的一般形式如下图8所示,表示在A的右孩子B的左子树(根结点为C,不一定为空)中插入结点(图中两个阴影部分之一)而导致不平衡( h 表示子树的深度)。这种情况调整如下:①将B的左孩子C提升为新的根结点;②将原来的根结点A降为C的左孩子;③各子树按大小关系连接(AL和BR不变,CL和CR分别调整为A的右子树和B的左子树)
代码实现
public Node rlRotate(Node y){
Node x = y.right;
y.right = llRotate(x);
return rrRotate(y);
}
3. AVL树节点平衡判断
public int getBalance(Node n){
if(n == null){
return 0;
}
return height(n.left) -height(n.right);
}
4. 插入操作
public void put(Key key, Value value){
root = put(key, value, root);
}
private Node put(Key key, Value value, Node node){
if(node == null){
return new Node(key,value,1);
}
int cmp = key.compareTo(node.key);
if(cmp<0){
node.left = put(key,value,node.left);
}else if(cmp>0){
node.right = put(key,value,node.right);
}else {
node.value = value;
return node;
}
int balance = getBalance(node);
if(balance > 1 && key.compareTo(node.left.key)<0){//LL型
return llRotate(node);
}
if(balance < -1 && key.compareTo(node.right.key)>0){//RR型
return rrRotate(node);
}
if(balance > 1 && key.compareTo(node.left.key)>0){//LR型
return lrRotate(node);
}
if(balance < -1 && key.compareTo(node.right.key)<0){//RL型
return rlRotate(node);
}
return node;
}
5. 查找操作
5.1 查找最小值
public Value min(){
return min(root).value;
}
private Node min(Node node){
if(node.left == null){
return node;
}
return min(node.left);
}
5.2 查找指定key
public Value get(Key key){
return get(root, key);
}
private Value get(Node x, Key key){
if(x == null){
return null;
}
int cmp = key.compareTo(x.key);
if(cmp<0){//往左子树找
return get(x.left, key);
}else if(cmp>0){//往右子树找
return get(x.right, key);
}else{
return x.value;
}
}
6. 删除操作
6.1 删除最小值
public void deleteMin(){
root = deleteMin(root);
}
private Node deleteMin(Node node){
if(node.left == null){
return node.right;
}
node.left = deleteMin(node.left);
node.height = Math.max(height(node.left), height(node.right))+1;
return node;
}
6.2 删除指定key
public void delete(Key key){
root = delete(root, key);
}
private Node delete(Node x, Key key){
if(x == null){
return null;
}
int cmp = x.key.compareTo(key);
if (cmp > 0) {
//查找
x.left = delete(x.left, key);
}else if(cmp < 0){
//查找
x.right = delete(x.right, key);
}else{
if(x.right == null){
return x.left;
}
if(x.left == null){
return x.right;
}
Node t = x;
//后继结点
x = min(t.right);
x.right = deleteMin(t.right);
x.left = t.left;
}
x.height = Math.max(height(x.left), height(x.right))+1;
int balance = getBalance(x);
if(balance > 1 && key.compareTo(x.left.key)<0){//LL型
return llRotate(x);
}
if(balance < -1 && key.compareTo(x.right.key)>0){//RR型
return rrRotate(x);
}
if(balance > 1 && key.compareTo(x.left.key)>0){//LR型
return lrRotate(x);
}
if(balance < -1 && key.compareTo(x.right.key)<0){//RL型
return rlRotate(x);
}
return x;
}