时间:2023-02-09 13:13:57 | 栏目:JAVA代码 | 点击:次
动机:二叉查找树的操作实践复杂度由树高度决定,所以希望控制树高,左右子树尽可能平衡。
平衡二叉树(AVL树):称一棵二叉查找树为高度平衡树,当且仅当或由单一外结点组成,或由两个子树形 Ta 和 Tb 组成,并且满足:
即:每个结点的左子树和右子树的高度最多差 1 的 二叉查找树。
key
:关键字的值
value
:关键字的存储信息
height
:树的高度(只有一个结点的树的高度为 1
)
left
:左子树根结点的的引用
right
:右子树根结点的引用
class AVLNode<K extends Comparable<K>, V> { public K key; public V value; public int height; public AVLNode<K, V> left; public AVLNode<K, V> right; public AVLNode(K key, V value, int height) { this.key = key; this.value = value; this.height = height; } }
同二叉查找树的查找算法:Java数据结构之二叉查找树的实现
AVL 树是一种二叉查找树,故可以使用二叉查找树的插入方法插入结点,但插入一个新结点时,有可能破坏 AVL 树的平衡性。
如果发生这种情况,就需要在插入结点后对平衡树进行调整,恢复平衡的性质。实现这种调整的操作称为“旋转”。
在插入一个新结点 X 后,应调整失去平衡的最小子树,即从插入点到根的路径向上找第一个不平衡结点 A。
平衡因子:该结点的左子树高度和右子树高度的差值。如果差值的绝对值小于等于 1
,则说明该结点平衡,如果差值的绝对值为 2
(不会出现其他情况),则说明该结点不平衡,需要做平衡处理。
造成结点 A 不平衡的的原因以及调整方式有以下几种情况。
A 结点的平衡因子为 2
,说明该结点是最小不平衡结点,需要对 A 结点进行调整。问题发生在 A 结点左子结点的左子结点,所以为 LL 型。
扁担原理:右旋
private AVLNode<K, V> rightRotate(AVLNode<K, V> a) { AVLNode<K, V> b = a.left; a.left = b.right; b.right = a; a.height = Math.max(getHeight(a.left), getHeight(a.right)) + 1; b.height = Math.max(getHeight(b.left), getHeight(b.left)) + 1; return b; }
A 结点的平衡因子为 2
,说明该结点是最小不平衡结点,需要对 A 结点进行调整。问题发生在 A 结点右子结点的右子结点,所以为 RR 型。
扁担原理:左旋
private AVLNode<K, V> leftRotate(AVLNode<K, V> a) { AVLNode<K, V> b = a.right; a.right = b.left; b.left = a; a.height = Math.max(getHeight(a.left), getHeight(a.right)) + 1; b.height = Math.max(getHeight(b.left), getHeight(b.left)) + 1; return b; }
A 结点的平衡因子为 2
,说明该结点是最小不平衡结点,需要对 A 结点进行调整。问题发生在 A 结点左子结点的右子结点,所以为 LR 型。
private AVLNode<K, V> leftRightRotate(AVLNode<K, V> a) { a.left = leftRotate(a.left); // 对 B 左旋 return rightRotate(a); // 对 A 右旋 }
A 结点的平衡因子为 2
,说明该结点是最小不平衡结点,需要对 A 结点进行调整。问题发生在 A 结点右子结点的左子结点,所以为 RL 型。
private AVLNode<K, V> rightLeftRotate(AVLNode<K, V> a) { a.right = rightRotate(a.right); return leftRotate(a); }
根结点默认高度为 1
某结点的左右子树高度差的绝对值为 2
,则需要进行平衡处理
I.左子树高
key
小于 root.left.key
:LL型,进行右旋
key
大于 root.left.key
:LR型,进行左右旋
II.右子树高
key
大于 root.right.key
:RR型,进行左旋
key
小于 root.right.key
:RR型,进行右左旋
public void insert(K key, V value) { root = insert(root, key, value); } private AVLNode<K, V> insert(AVLNode<K, V> t, K key, V value) { if (t == null) { return new AVLNode<>(key, value, 1); } else if (key.compareTo(t.key) < 0) { t.left = insert(t.left, key, value); t.height = Math.max(getHeight(t.left), getHeight(t.right)) + 1; // 平衡因子判断 if (getHeight(t.left) - getHeight(t.right) == 2) { if (key.compareTo(root.left.key) < 0) // 左左:右旋 t = rightRotate(t); else // 左右:先左旋,再右旋 t = leftRightRotate(t); } } else if (key.compareTo(t.key) > 0) { t.right = insert(t.right, key, value); t.height = Math.max(getHeight(t.left), getHeight(t.right)) + 1; // 平衡因子判断 if (getHeight(t.left) - getHeight(t.right) == -2) { if (key.compareTo(root.right.key) > 0) // 右右:左旋 t = leftRotate(t); else // 右左:先右旋,再左旋 t = rightLeftRotate(t); } } else { t.value = value; } return t; }
O(logn)
次旋转。O(1)
次旋转。下面举个删除的例子:
删除以下平衡二叉树中的 16 结点
16 为叶子,将其删除即可,如下图。
指针 g 指向实际被删除节点 16 之父 25,检查是否失衡,25 节点失衡,用 g 、u 、v 记录失衡三代节点(从失衡节点沿着高度大的子树向下找三代),判断为 RL 型,进行 RL 旋转调整平衡,如下图所示。
继续向上检查,指针 g 指向 g 的双亲 69,检查是否失衡,69 节点失衡,用 g 、u 、v 记录失衡三代节点,判断为 RR 型,进行 RR 旋转调整平衡,如下图所示。
代码描述:
1.若当前结点为空, 则返回该节点
2.若关键值小于当前结点的关键值,则递归处理该结点的左子树
3.若关键值大于当前结点的关键值,则递归处理该结点的右子树
4.若关键值等于当前结点的关键值
5.更新结点高度
6.若该结点左子树高度更高,且处于不平衡状态
7.若该结点右子树高度更高,且处于不平衡状态
8.返回该结点
public void remove(K key) { this.root = delete(root, key); } public AVLNode<K, V> delete(AVLNode<K, V> t, K key) { if (t == null) return t; if (key.compareTo(t.key) < 0) { t.left = delete(t.left, key); } else if (key.compareTo(t.key) > 0) { t.right = delete(t.right, key); } else { if(t.left == null) return t.right; else if(t.right == null) return t.left; else { // t.left != null && t.right != null AVLNode<K, V> pre = t.left; while (pre.right != null) { pre = pre.right; } t.key = pre.key; t.value = pre.value; t.left = delete(t.left, t.key); } } if (t == null) return t; t.height = Math.max(getHeight(t.left), getHeight(t.right)) + 1; if(getHeight(t.left) - getHeight(t.right) >= 2) { if(getHeight(t.left.left) > getHeight(t.left.right)) { return rightRotate(t); } else { return leftRightRotate(t); } } else if(getHeight(t.left) - getHeight(t.right) <= -2) { if(getHeight(t.right.left) > getHeight(t.right.right)) { return rightLeftRotate(t); } else { return leftRotate(t); } } return t; }
class AVLNode<K extends Comparable<K>, V> { public K key; public V value; public int height; public AVLNode<K, V> left; public AVLNode<K, V> right; public AVLNode(K key, V value, int height) { this.key = key; this.value = value; this.height = height; } } class AVLTree<K extends Comparable<K>, V> { public AVLNode<K, V> root; public int getHeight(AVLNode<K, V> t) { return t == null ? 0 : t.height; } public void insert(K key, V value) { root = insert(root, key, value); } public void remove(K key) { this.root = delete(root, key); } public AVLNode<K, V> delete(AVLNode<K, V> t, K key) { if (t == null) return t; if (key.compareTo(t.key) < 0) { t.left = delete(t.left, key); } else if (key.compareTo(t.key) > 0) { t.right = delete(t.right, key); } else { if(t.left == null) return t.right; else if(t.right == null) return t.left; else { // t.left != null && t.right != null AVLNode<K, V> pre = t.left; while (pre.right != null) { pre = pre.right; } t.key = pre.key; t.value = pre.value; t.left = delete(t.left, t.key); } } if (t == null) return t; t.height = Math.max(getHeight(t.left), getHeight(t.right)) + 1; if(getHeight(t.left) - getHeight(t.right) >= 2) { if(getHeight(t.left.left) > getHeight(t.left.right)) { return rightRotate(t); } else { return leftRightRotate(t); } } else if(getHeight(t.left) - getHeight(t.right) <= -2) { if(getHeight(t.right.left) > getHeight(t.right.right)) { return rightLeftRotate(t); } else { return leftRotate(t); } } return t; } private AVLNode<K, V> insert(AVLNode<K, V> t, K key, V value) { if (t == null) { return new AVLNode<>(key, value, 1); } if (key.compareTo(t.key) < 0) { t.left = insert(t.left, key, value); // 平衡因子判断 if (getHeight(t.left) - getHeight(t.right) == 2) { if (key.compareTo(t.left.key) < 0) // 左左:右旋 t = rightRotate(t); else // 左右:先左旋,再右旋 t = leftRightRotate(t); } } else if (key.compareTo(t.key) > 0) { t.right = insert(t.right, key, value); // 平衡因子判断 if (getHeight(t.left) - getHeight(t.right) == -2) { if (key.compareTo(t.right.key) > 0) // 右右:左旋 t = leftRotate(t); else // 右左:先右旋,再左旋 t = rightLeftRotate(t); } } else { t.value = value; } t.height = Math.max(getHeight(t.left), getHeight(t.right)) + 1; return t; } private AVLNode<K, V> rightLeftRotate(AVLNode<K, V> a) { a.right = rightRotate(a.right); return leftRotate(a); } private AVLNode<K, V> leftRightRotate(AVLNode<K, V> a) { a.left = leftRotate(a.left); return rightRotate(a); } private AVLNode<K, V> leftRotate(AVLNode<K, V> a) { AVLNode<K, V> b = a.right; a.right = b.left; b.left = a; a.height = Math.max(getHeight(a.left), getHeight(a.right)) + 1; b.height = Math.max(getHeight(b.left), getHeight(b.right)) + 1; return b; } private AVLNode<K, V> rightRotate(AVLNode<K, V> a) { AVLNode<K, V> b = a.left; a.left = b.right; b.right = a; a.height = Math.max(getHeight(a.left), getHeight(a.right)) + 1; b.height = Math.max(getHeight(b.left), getHeight(b.right)) + 1; return b; } private void inorder(AVLNode<K, V> root) { if (root != null) { inorder(root.left); System.out.print("(key: " + root.key + " , value: " + root.value + " , height: " + root.height + ") "); inorder(root.right); } } private void preorder(AVLNode<K, V> root) { if (root != null) { System.out.print("(key: " + root.key + " , value: " + root.value + " , height: " + root.height + ") "); preorder(root.left); preorder(root.right); } } private void postorder(AVLNode<K, V> root) { if (root != null) { postorder(root.left); postorder(root.right); System.out.print("(key: " + root.key + " , value: " + root.value + " , height: " + root.height + ") "); } } public void postorderTraverse() { System.out.print("后序遍历:"); postorder(root); System.out.println(); } public void preorderTraverse() { System.out.print("先序遍历:"); preorder(root); System.out.println(); } public void inorderTraverse() { System.out.print("中序遍历:"); inorder(root); System.out.println(); } }
方法测试
public static void main(String[] args) { AVLTree<Integer, Integer> tree = new AVLTree<>(); tree.insert(69, 1); tree.insert(25, 1); tree.insert(80, 1); tree.insert(16, 1); tree.insert(56, 1); tree.insert(75, 1); tree.insert(90, 1); tree.insert(30, 1); tree.insert(78, 1); tree.insert(85, 1); tree.insert(98, 1); tree.insert(82, 1); tree.remove(16); tree.preorderTraverse(); tree.inorderTraverse(); tree.postorderTraverse(); }
输出
先序遍历:(key: 80 , value: 1 , height: 4) (key: 69 , value: 1 , height: 3) (key: 30 , value: 1 , height: 2) (key: 25 , value: 1 , height: 1) (key: 56 , value: 1 , height: 1) (key: 75 , value: 1 , height: 2) (key: 78 , value: 1 , height: 1) (key: 90 , value: 1 , height: 3) (key: 85 , value: 1 , height: 2) (key: 82 , value: 1 , height: 1) (key: 98 , value: 1 , height: 1)
中序遍历:(key: 25 , value: 1 , height: 1) (key: 30 , value: 1 , height: 2) (key: 56 , value: 1 , height: 1) (key: 69 , value: 1 , height: 3) (key: 75 , value: 1 , height: 2) (key: 78 , value: 1 , height: 1) (key: 80 , value: 1 , height: 4) (key: 82 , value: 1 , height: 1) (key: 85 , value: 1 , height: 2) (key: 90 , value: 1 , height: 3) (key: 98 , value: 1 , height: 1)
后序遍历:(key: 25 , value: 1 , height: 1) (key: 56 , value: 1 , height: 1) (key: 30 , value: 1 , height: 2) (key: 78 , value: 1 , height: 1) (key: 75 , value: 1 , height: 2) (key: 69 , value: 1 , height: 3) (key: 82 , value: 1 , height: 1) (key: 85 , value: 1 , height: 2) (key: 98 , value: 1 , height: 1) (key: 90 , value: 1 , height: 3) (key: 80 , value: 1 , height: 4)