平衡二叉树
平衡二叉搜索树(Self-balancing binary search tree)又被称为AVL树,且具有以下性质:
- 本身首先是一棵二叉搜索树
- 带有平衡条件:每个节点的左右子树的高度之差的绝对值(平衡因子)最多为1
也就是说,AVL树,本质上是带有平衡功能的二叉树(二叉排序树,二叉搜索树)
但AVL树也是一种特殊的二叉搜索树,在相对于数据极端情况下二叉搜索树会退化成单链表,AVL树定义旋转操作,在平衡因子大于2时,AVL树会旋转来调整树的结构,来满足平衡因子小于2
右边的是AVL树
AVL的结构
// AVL树的节点(内部类)
class AVLTreeNode<T extends Comparable<T>> {
T element; // 值
int height; // 高度
AVLTreeNode<T> left; // 左孩子
AVLTreeNode<T> right; // 右孩子
public AVLTreeNode(T key, AVLTreeNode<T> left, AVLTreeNode<T> right) {
this.element = key;
this.left = left;
this.right = right;
this.height = 0;
}
}
AVL的不平衡情况
AVL树大部分操作都和BST树相同, 只有在插入删除结点时, 有可能造成AVL树失去平衡, 而且只有那些在被插入/删除结点到根节点的路径上的结点有可能出现失衡, 因为只有那些结点的子树结构发生了变化
当插入新结点导致不平衡时, 我们需要找到距离新节点最近的不平衡结点为轴来转动AVL树来达到平衡
左子树的左节点插入节点(左左)
插入节点2,导致节点6失衡(节点2相对于节点6为左子树的左子树),那么就需要旋转6,使平衡因子重新满足AVL树的条件
/**
* LL:左左对应的情况(左单旋转)。
* @param k2
* @return 旋转后的根节点
*/
private AVLTreeNode<T> leftLeftRotation(AVLTreeNode<T> k2) {
AVLTreeNode<T> k1;
k1 = k2.left;
k2.left = k1.right;
k1.right = k2;
k2.height = Math.max(height(k2.left), height(k2.right)) + 1;
k1.height = Math.max(height(k1.left), k2.height) + 1;
return k1;
}
右子树的右子树插入节点(右右)
private AVLTreeNode<T> rightRightRotation(AVLTreeNode<T> k1) {
AVLTreeNode<T> k2;
k2 = k1.right;
k1.right = k2.left;
k2.left = k1;
k1.height = Math.max(height(k1.left), height(k1.right)) + 1;
k2.height = Math.max(height(k2.right), k1.height) + 1;
return k2;
}
左子树的右子树插入节点 (左右)
/**
* LR:左右对应的情况(左双旋转)。
* @param k3
* @return 旋转后的根节点
*/
private AVLTreeNode<T> leftRightRotation(AVLTreeNode<T> k3) {
k3.left = rightRightRotation(k3.left);
return leftLeftRotation(k3);
}
右子树的左子树插入节点 (右左)
/**
* RL:右左对应的情况(右双旋转)。
* @param k1
* @return 旋转后的根节点
*/
private AVLTreeNode<T> rightLeftRotation(AVLTreeNode<T> k1) {
k1.right = leftLeftRotation(k1.right);
return rightRightRotation(k1);
}
插入
public void insert(T key) {
mRoot = insert(mRoot, key);
}
/**
* 将结点插入到AVL树中,并返回根节点
*
* @param tree AVL树的根结点
* @param key 插入的结点的键值
* @return 根节点
*/
private AVLTreeNode<T> insert(AVLTreeNode<T> tree, T key) {
if (tree == null) {
// 新建节点
return tree = new AVLTreeNode<T>(key, null, null);
}
int cmp = key.compareTo(tree.element);
if (cmp < 0) {// 将key插入到"tree的左子树"的情况
tree.left = insert(tree.left, key);
} else if (cmp > 0) { // 将key插入到"tree的右子树"的情况
tree.right = insert(tree.right, key);
}
return balance(tree);
}
当失去平衡是通过balance()进行调节
private static final int ALLOWED_IMBALANCE = 1;
private AVLTreeNode<T> balance(AVLTreeNode<T> tree) {
if (tree == null) {
return tree;
}
// 插入节点后,若AVL树失去平衡,则进行相应的调节。
if (height(tree.left) - height(tree.right) > ALLOWED_IMBALANCE) {
if (height(tree.left.left) >= height(tree.left.right)) {
leftLeftRotation(tree);
} else {
leftRightRotation(tree);
}
} else if (height(tree.right) - height(tree.left) > ALLOWED_IMBALANCE) {
if (height(tree.right.right) >= height(tree.right.left)) {
rightRightRotation(tree);
} else {
rightLeftRotation(tree);
}
}
tree.height = Math.max( height(tree.left), height(tree.right)) + 1;
return tree;
}
删除
public void remove(T key) {
AVLTreeNode<T> z;
mRoot = remove(mRoot, z);
}
/**
* 删除结点(z),返回根节点
*
* @param tree AVL树的根结点
* @param z 待删除的结点
* @return 根节点
*/
private AVLTreeNode<T> remove(AVLTreeNode<T> tree, AVLTreeNode<T> z) {
if (tree == null)
return tree;
int cmp = z.element.compareTo(tree.element);
if (cmp > 0) {
tree.right = remove(tree.right, z);
} else if (cmp < 0) {
tree.left = remove(tree.left, z);
} else if (tree.left != null && tree.right != null) {
tree.element = findMin(tree.right).element;
tree.right = remove(tree.element, tree.right);
} else {
tree = (tree.left != null) ? tree.left : tree.right;
}
return balance(tree);
}
private AVLTreeNode<T> findMin(AVLTreeNode<T> node) {
if (node != null) {
while (node.left != null) {
node = node.left;
}
}
return node;
}
public AVLTreeNode<T> remove(T t, AVLTreeNode<T> node) {
if (node == null) {
return node;
}
int compareResult = t.compareTo(node.element);
if (compareResult > 0) {
node.right = remove(t, node.right);
} else if (compareResult < 0) {
node.left = remove(t, node.left);
} else if (node.left != null && node.right != null) {
node.element = findMin(node.right).element;
node.right = remove(node.element, node.right);
} else {
node = (node.left != null) ? node.left : node.right;
}
return node;
}
完整代码
public class AVLTree<T extends Comparable<T>> {
private AVLTreeNode<T> mRoot; // 根结点
// AVL树的节点(内部类)
class AVLTreeNode<T extends Comparable<T>> {
T element; // 值
int height; // 高度
AVLTreeNode<T> left; // 左孩子
AVLTreeNode<T> right; // 右孩子
public AVLTreeNode(T key, AVLTreeNode<T> left, AVLTreeNode<T> right) {
this.element = key;
this.left = left;
this.right = right;
this.height = 0;
}
}
/*
* 获取树的高度
*/
private int height(AVLTreeNode<T> tree) {
if (tree != null)
return tree.height;
return 0;
}
public int height() {
return height(mRoot);
}
/*
* LL:左左对应的情况(左单旋转)。
*
* 返回值:旋转后的根节点
*/
/**
* LL:左左对应的情况(左单旋转)。
*
* @param k2
* @return 旋转后的根节点
*/
private AVLTreeNode<T> leftLeftRotation(AVLTreeNode<T> k2) {
AVLTreeNode<T> k1;
k1 = k2.left;
k2.left = k1.right;
k1.right = k2;
k2.height = Math.max(height(k2.left), height(k2.right)) + 1;
k1.height = Math.max(height(k1.left), k2.height) + 1;
return k1;
}
/**
* 右右对应的情况(右单旋转)。
*
* @param k1
* @return 旋转后的根节点
*/
private AVLTreeNode<T> rightRightRotation(AVLTreeNode<T> k1) {
AVLTreeNode<T> k2;
k2 = k1.right;
k1.right = k2.left;
k2.left = k1;
k1.height = Math.max(height(k1.left), height(k1.right)) + 1;
k2.height = Math.max(height(k2.right), k1.height) + 1;
return k2;
}
/**
* LR:左右对应的情况(左双旋转)。
*
* @param k3
* @return 旋转后的根节点
*/
private AVLTreeNode<T> leftRightRotation(AVLTreeNode<T> k3) {
k3.left = rightRightRotation(k3.left);
return leftLeftRotation(k3);
}
/**
* RL:右左对应的情况(右双旋转)。
*
* @param k1
* @return 旋转后的根节点
*/
private AVLTreeNode<T> rightLeftRotation(AVLTreeNode<T> k1) {
k1.right = leftLeftRotation(k1.right);
return rightRightRotation(k1);
}
public void insert(T key) {
mRoot = insert(mRoot, key);
}
/**
* 将结点插入到AVL树中,并返回根节点
*
* @param tree AVL树的根结点
* @param key 插入的结点的键值
* @return 根节点
*/
private AVLTreeNode<T> insert(AVLTreeNode<T> tree, T key) {
if (tree == null) {
// 新建节点
return tree = new AVLTreeNode<T>(key, null, null);
}
int cmp = key.compareTo(tree.element);
if (cmp < 0) {// 将key插入到"tree的左子树"的情况
tree.left = insert(tree.left, key);
} else if (cmp > 0) { // 将key插入到"tree的右子树"的情况
tree.right = insert(tree.right, key);
}
return balance(tree);
}
private static final int ALLOWED_IMBALANCE = 1;
private AVLTreeNode<T> balance(AVLTreeNode<T> tree) {
if (tree == null) {
return tree;
}
// 插入节点后,若AVL树失去平衡,则进行相应的调节。
if (height(tree.left) - height(tree.right) > ALLOWED_IMBALANCE) {
if (height(tree.left.left) >= height(tree.left.right)) {
leftLeftRotation(tree);
} else {
leftRightRotation(tree);
}
} else if (height(tree.right) - height(tree.left) > ALLOWED_IMBALANCE) {
if (height(tree.right.right) >= height(tree.right.left)) {
rightRightRotation(tree);
} else {
rightLeftRotation(tree);
}
}
tree.height = Math.max(height(tree.left), height(tree.right)) + 1;
return tree;
}
public void remove(T key) {
AVLTreeNode<T> z;
if ((z = search(mRoot, key)) != null)
mRoot = remove(mRoot, z);
}
/*
* (递归实现)查找"AVL树x"中键值为key的节点
*/
private AVLTreeNode<T> search(AVLTreeNode<T> x, T key) {
if (x == null)
return x;
int cmp = key.compareTo(x.element);
if (cmp < 0)
return search(x.left, key);
else if (cmp > 0)
return search(x.right, key);
else
return x;
}
public AVLTreeNode<T> search(T key) {
return search(mRoot, key);
}
/**
* 删除结点(z),返回根节点
*
* @param tree AVL树的根结点
* @param z 待删除的结点
* @return 根节点
*/
private AVLTreeNode<T> remove(AVLTreeNode<T> tree, AVLTreeNode<T> z) {
if (tree == null)
return tree;
int cmp = z.element.compareTo(tree.element);
if (cmp > 0) {
tree.right = remove(tree.right, z);
} else if (cmp < 0) {
tree.left = remove(tree.left, z);
} else if (tree.left != null && tree.right != null) {
tree.element = findMin(tree.right).element;
tree.right = remove(tree.element, tree.right);
} else {
tree = (tree.left != null) ? tree.left : tree.right;
}
return balance(tree);
}
private AVLTreeNode<T> findMin(AVLTreeNode<T> node) {
if (node != null) {
while (node.left != null) {
node = node.left;
}
}
return node;
}
public AVLTreeNode<T> remove(T t, AVLTreeNode<T> node) {
if (node == null) {
return node;
}
int compareResult = t.compareTo(node.element);
if (compareResult > 0) {
node.right = remove(t, node.right);
} else if (compareResult < 0) {
node.left = remove(t, node.left);
} else if (node.left != null && node.right != null) {
node.element = findMin(node.right).element;
node.right = remove(node.element, node.right);
} else {
node = (node.left != null) ? node.left : node.right;
}
return node;
}
/*
* 打印"二叉查找树"
*
* key -- 节点的键值
* direction -- 0,表示该节点是根节点;
* -1,表示该节点是它的父结点的左孩子;
* 1,表示该节点是它的父结点的右孩子。
*/
private void print(AVLTreeNode<T> tree, T key, int direction) {
if(tree != null) {
if(direction==0) // tree是根节点
System.out.printf("%2d is root\n", tree.element, key);
else // tree是分支节点
System.out.printf("%2d is %2d's %6s child\n", tree.element, key, direction==1?"right" : "left");
print(tree.left, tree.element, -1);
print(tree.right,tree.element, 1);
}
}
public void print() {
if (mRoot != null)
print(mRoot, mRoot.element, 0);
}
}
完整测试代码
public class AVLTreeTest {
private static int arr[] = {3, 2, 1, 4, 5, 6, 7, 16, 15, 14, 13, 12, 11, 10, 8, 9};
public static void main(String[] args) {
int i;
AVLTree<Integer> tree = new AVLTree<Integer>();
System.out.printf("== 依次添加: ");
for (i = 0; i < arr.length; i++) {
System.out.printf("%d ", arr[i]);
tree.insert(arr[i]);
}
System.out.printf("== 高度: %d\n", tree.height());
System.out.printf("== 树的详细信息: \n");
tree.print();
i = 6;
System.out.printf("\n== 删除根节点: %d", i);
tree.remove(i);
System.out.printf("\n== 高度: %d", tree.height());
System.out.printf("\n== 树的详细信息: \n");
tree.print();
}
}