AVL树
在计算机科学中,AVL树是最先发明的自平衡二叉查找树。在AVL树中任何节点的两个子树的高度最大差别为1,所以它也被称为高度平衡树。增加和删除可能需要通过一次或多次树旋转来重新平衡这个树。
特点
1.本身首先是一棵二叉搜索树。
2.带有平衡条件:每个结点的左右子树的高度之差的绝对值(平衡因子)最多为1。也就是说,AVL树,本质上是带了平衡功能的二叉查找树(二叉排序树,二叉搜索树)
平衡因子
某结点的左子树与右子树的高度(深度)差即为该结点的平衡因子(BF,Balance Factor)。平衡二叉树上所有结点的平衡因子只可能是 -1,0 或 1。
高度(height)
为了方便计算每一结点的平衡因子我们可以为每个节点赋予height这一属性,表示此节点的高度。这里的高度是由叶子节点从下向上计算的,叶子节点的height是1,叶子节点的父节点height是2,以此类推。
旋转
AVL树的基本操作一般涉及运做同在不平衡的二叉查找树所运做的同样的算法。但是要进行预先或随后做一次或多次所谓的"AVL 旋转"。
假设由于在二叉排序树上插入结点而失去平衡的最小子树根结点的指针为a(即a是离插入点最近,且平衡因子绝对值超过1的祖先结点),则失去平衡后进行进行的规律可归纳为下列四种情况:
单向右旋平衡处理LL:由于在a的左子树根结点的左子树上插入结点,a的平衡因子由1增至2,致使以a为根的子树失去平衡,则需进行一次右旋转操作;
单向左旋平衡处理RR:由于在a的右子树根结点的右子树上插入结点,a的平衡因子由-1变为-2,致使以a为根的子树失去平衡,则需进行一次左旋转操作;
双向旋转(先左后右)平衡处理LR:由于在a的左子树根结点的右子树上插入结点,a的平衡因子由1增至2,致使以a为根的子树失去平衡,则需进行两次旋转(先左旋后右旋)操作。
双向旋转(先右后左)平衡处理RL:由于在a的右子树根结点的左子树上插入结点,a的平衡因子由-1变为-2,致使以a为根的子树失去平衡,则需进行两次旋转(先右旋后左旋)操作。
AVLTree.java(AVL树)
import java.util.ArrayList;
//AVL树
public class AVLTree<K extends Comparable<K>, V> {
private class Node {
public K key;
public V value;
public Node left, right;//左子树 右子树
public int height;// 高度 从根节点开始
public Node(K key, V value) {
this.key = key;
this.value = value;
left = null;
right = null;
height = 1;
}
}
private Node root;//根节点
private int size;//容量
public AVLTree() {
root = null;
size = 0;
}
public int getSize() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
// 获得节点node的高度
private int getHeight(Node node) {
if (node == null)
return 0;
return node.height;
}
// 右旋转
// 对节点y进行向右旋转操作,返回旋转后新的根节点x
// y x
// / \ / \
// x T4 向右旋转 (y) z y
// / \ - - - - - - - -> / \ / \
// z T3 T1 T2 T3 T4
// / \
// T1 T2
private Node rightRotate(Node y) {
Node x = y.left;
Node T3 = x.right;
// 向右旋转
x.right = y;
y.left = T3;
// 更新height
y.height = Math.max(getHeight(y.left), getHeight(y.right)) + 1;
x.height = Math.max(getHeight(x.left), getHeight(x.right)) + 1;
return x;// 返回新的根节点
}
// 左旋转
// 对节点y进行向左旋转操作,返回旋转后新的根节点x
// y x
// / \ / \
// T1 x 向左旋转 (y) y z
// / \ - - - - - - - -> / \ / \
// T2 z T1 T2 T3 T4
// / \
// T3 T4
public Node leftRotate(Node y) {
Node x = y.right;
Node T2 = x.left;
// 向左旋转
x.left = y;
y.right = T2;
// 更新height
y.height = Math.max(getHeight(y.left), getHeight(y.right)) + 1;
x.height = Math.max(getHeight(x.left), getHeight(x.right)) + 1;
return x;// 返回新的根节点
}
// 获得节点node的平衡因子
// 平衡因子即左子树与右子树高度差
private int getBalanceFactor(Node node) {
if (node == null)
return 0;
return getHeight(node.left) - getHeight(node.right);
}
// 向二分搜索树中添加新的元素(key, value)
public void add(K key, V value) {
root = add(root, key, value);
}
// 向以node为根的二分搜索树中插入元素(key, value),递归算法
// 返回插入新节点后二分搜索树的根
private Node add(Node node, K key, V value) {
if (node == null) {
size++;
return new Node(key, value);
}
if (key.compareTo(node.key) < 0)
node.left = add(node.left, key, value);
else if (key.compareTo(node.key) > 0)
node.right = add(node.right, key, value);
else // key.compareTo(node.key) == 0
node.value = value;
// 更新height
// 根节点的height = 1+左右子树最大高度
node.height = 1 + Math.max(getHeight(node.left), getHeight(node.right));
// 计算平衡因子
int balanceFactor = getBalanceFactor(node);
if (Math.abs(balanceFactor) > 1)// 如果平衡因子大于1 即不满足平衡二叉树
System.out.println("unbalanced : " + balanceFactor);
// 平衡维护
// LL
if (balanceFactor > 1 && getBalanceFactor(node.left) >= 0)// 向左偏斜
return rightRotate(node);// 右旋转
// RR
if (balanceFactor < -1 && getBalanceFactor(node.right) <= 0)// 向右偏斜
return leftRotate(node);// 左旋转
// LR
if (balanceFactor > 1 && getBalanceFactor(node.left) < 0) {
node.left = leftRotate(node.left);// 变为LL
return rightRotate(node);// 右旋转
}
// RL
if (balanceFactor < -1 && getBalanceFactor(node.right) > 0) {
node.right = rightRotate(node.right);// 变为RR
return leftRotate(node);// 左旋转
}
return node;
}
// 返回以node为根节点的二分搜索树中,key所在的节点
private Node getNode(Node node, K key) {
if (node == null)
return null;
if (key.equals(node.key))
return node;
else if (key.compareTo(node.key) < 0)
return getNode(node.left, key);
else // if(key.compareTo(node.key) > 0)
return getNode(node.right, key);
}
//查看树中是否包含元素key
public boolean contains(K key) {
return getNode(root, key) != null;
}
//获取元素key
public V get(K key) {
Node node = getNode(root, key);
return node == null ? null : node.value;
}
//修改元素key
public void set(K key, V newValue) {
Node node = getNode(root, key);
if (node == null)
throw new IllegalArgumentException(key + " doesn't exist!");
node.value = newValue;
}
// 返回以node为根的二分搜索树的最小值所在的节点
private Node minimum(Node node) {
if (node.left == null)
return node;
return minimum(node.left);
}
// 判断该二叉树是否是一棵二分搜索树
public boolean isBST() {
ArrayList<K> keys = new ArrayList<>();
inOrder(root, keys);
for (int i = 0; i < keys.size(); i++) {
if (keys.get(i - 1).compareTo(keys.get(i)) > 0)// 二分搜索树应该符合从小到大
return false;
}
return true;
}
// 中序遍历存储在keys中
private void inOrder(Node node, ArrayList<K> keys) {
// TODO Auto-generated method stub
if (node == null)
return;
inOrder(node.left, keys);
keys.add(node.key);
inOrder(node.right, keys);
}
// 从二分搜索树中删除键为key的节点
public V remove(K key) {
Node node = getNode(root, key);
if (node != null) {
root = remove(root, key);
return node.value;
}
return null;
}
//在以node为根节点的树中删除key 递归算法
private Node remove(Node node, K key) {
if (node == null)
return null;
Node retNode;
if (key.compareTo(node.key) < 0) {
node.left = remove(node.left, key);
retNode = node;
} else if (key.compareTo(node.key) > 0) {
node.right = remove(node.right, key);
retNode = node;
} else { // key.compareTo(node.key) == 0
// 待删除节点左子树为空的情况
if (node.left == null) {
Node rightNode = node.right;
node.right = null;
size--;
retNode = rightNode;
}
// 待删除节点右子树为空的情况
else if (node.right == null) {
Node leftNode = node.left;
node.left = null;
size--;
retNode = leftNode;
} else {
// 待删除节点左右子树均不为空的情况
// 找到比待删除节点大的最小节点, 即待删除节点右子树的最小节点
// 用这个节点顶替待删除节点的位置
Node successor = minimum(node.right);
successor.right = remove(node.right, successor.key);
successor.left = node.left;
node.left = node.right = null;
retNode = successor;
}
}
if (retNode == null)// 删除之后根节点为null
return null;
// 更新height
// 根节点的height = 1+左右子树最大高度
retNode.height = 1 + Math.max(getHeight(retNode.left), getHeight(retNode.right));
// 计算平衡因子
int balanceFactor = getBalanceFactor(retNode);
if (Math.abs(balanceFactor) > 1)// 如果平衡因子大于1 即不满足平衡二叉树
System.out.println("unbalanced : " + balanceFactor);
// 平衡维护
// LL
if (balanceFactor > 1 && getBalanceFactor(retNode.left) >= 0)// 向左偏斜
return rightRotate(retNode);// 右旋转
// RR
if (balanceFactor < -1 && getBalanceFactor(retNode.right) <= 0)// 向右偏斜
return leftRotate(retNode);// 左旋转
// LR
if (balanceFactor > 1 && getBalanceFactor(retNode.left) < 0) {
retNode.left = leftRotate(retNode.left);// 变为LL
return rightRotate(retNode);// 右旋转
}
// RL
if (balanceFactor < -1 && getBalanceFactor(retNode.right) > 0) {
retNode.right = rightRotate(retNode.right);// 变为RR
return leftRotate(retNode);// 左旋转
}
return retNode;//返回删除key后,经过平衡维护的根节点
}
// 判断该二叉树是否是一棵平衡二叉树
public boolean isBalanced() {
return isBalanced(root);
}
// 递归算法
private boolean isBalanced(Node node) {
// TODO Auto-generated method stub
if (node == null)// 递归终止条件
return true;
int balanceFactor = getBalanceFactor(node);
if (Math.abs(balanceFactor) > 1)// 平衡因子>1
return false;
// 当左右子树的平衡因子都<=1时,返回true
return isBalanced(node.left) && isBalanced(node.right);
}
}