2022-12-27 18:34:12 +08:00
|
|
|
|
"""
|
|
|
|
|
File: avl_tree.py
|
|
|
|
|
Created Time: 2022-12-20
|
|
|
|
|
Author: a16su (lpluls001@gmail.com)
|
|
|
|
|
"""
|
|
|
|
|
|
2022-12-20 15:52:00 +08:00
|
|
|
|
import sys, os.path as osp
|
2023-04-09 05:05:35 +08:00
|
|
|
|
|
2022-12-20 15:52:00 +08:00
|
|
|
|
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
2023-03-03 03:07:22 +08:00
|
|
|
|
from modules import *
|
2022-12-20 15:52:00 +08:00
|
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
|
|
2022-12-20 15:52:00 +08:00
|
|
|
|
class AVLTree:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""AVL 树"""
|
|
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
|
def __init__(self, root: TreeNode | None = None):
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""构造方法"""
|
2023-03-12 18:49:52 +08:00
|
|
|
|
self.__root = root
|
|
|
|
|
|
|
|
|
|
@property
|
2023-03-23 18:51:56 +08:00
|
|
|
|
def root(self) -> TreeNode | None:
|
2023-03-12 18:49:52 +08:00
|
|
|
|
return self.__root
|
2022-12-20 15:52:00 +08:00
|
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
|
def height(self, node: TreeNode | None) -> int:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""获取节点高度"""
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 空节点高度为 -1 ,叶节点高度为 0
|
2022-12-20 15:52:00 +08:00
|
|
|
|
if node is not None:
|
|
|
|
|
return node.height
|
|
|
|
|
return -1
|
|
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
|
def __update_height(self, node: TreeNode | None):
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""更新节点高度"""
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 节点高度等于最高子树高度 + 1
|
2022-12-20 15:52:00 +08:00
|
|
|
|
node.height = max([self.height(node.left), self.height(node.right)]) + 1
|
|
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
|
def balance_factor(self, node: TreeNode | None) -> int:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""获取平衡因子"""
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 空节点平衡因子为 0
|
2022-12-20 15:52:00 +08:00
|
|
|
|
if node is None:
|
|
|
|
|
return 0
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 节点平衡因子 = 左子树高度 - 右子树高度
|
2022-12-20 15:52:00 +08:00
|
|
|
|
return self.height(node.left) - self.height(node.right)
|
|
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
|
def __right_rotate(self, node: TreeNode | None) -> TreeNode | None:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""右旋操作"""
|
2022-12-20 15:52:00 +08:00
|
|
|
|
child = node.left
|
|
|
|
|
grand_child = child.right
|
|
|
|
|
# 以 child 为原点,将 node 向右旋转
|
|
|
|
|
child.right = node
|
|
|
|
|
node.left = grand_child
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 更新节点高度
|
2022-12-20 15:52:00 +08:00
|
|
|
|
self.__update_height(node)
|
|
|
|
|
self.__update_height(child)
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 返回旋转后子树的根节点
|
2022-12-20 15:52:00 +08:00
|
|
|
|
return child
|
|
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
|
def __left_rotate(self, node: TreeNode | None) -> TreeNode | None:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""左旋操作"""
|
2022-12-20 15:52:00 +08:00
|
|
|
|
child = node.right
|
|
|
|
|
grand_child = child.left
|
|
|
|
|
# 以 child 为原点,将 node 向左旋转
|
|
|
|
|
child.left = node
|
|
|
|
|
node.right = grand_child
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 更新节点高度
|
2022-12-20 15:52:00 +08:00
|
|
|
|
self.__update_height(node)
|
|
|
|
|
self.__update_height(child)
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 返回旋转后子树的根节点
|
2022-12-20 15:52:00 +08:00
|
|
|
|
return child
|
|
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
|
def __rotate(self, node: TreeNode | None) -> TreeNode | None:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""执行旋转操作,使该子树重新恢复平衡"""
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 获取节点 node 的平衡因子
|
2022-12-20 15:52:00 +08:00
|
|
|
|
balance_factor = self.balance_factor(node)
|
|
|
|
|
# 左偏树
|
|
|
|
|
if balance_factor > 1:
|
|
|
|
|
if self.balance_factor(node.left) >= 0:
|
|
|
|
|
# 右旋
|
|
|
|
|
return self.__right_rotate(node)
|
|
|
|
|
else:
|
|
|
|
|
# 先左旋后右旋
|
|
|
|
|
node.left = self.__left_rotate(node.left)
|
|
|
|
|
return self.__right_rotate(node)
|
|
|
|
|
# 右偏树
|
|
|
|
|
elif balance_factor < -1:
|
|
|
|
|
if self.balance_factor(node.right) <= 0:
|
|
|
|
|
# 左旋
|
|
|
|
|
return self.__left_rotate(node)
|
|
|
|
|
else:
|
|
|
|
|
# 先右旋后左旋
|
|
|
|
|
node.right = self.__right_rotate(node.right)
|
|
|
|
|
return self.__left_rotate(node)
|
|
|
|
|
# 平衡树,无需旋转,直接返回
|
|
|
|
|
return node
|
|
|
|
|
|
2022-12-27 18:34:12 +08:00
|
|
|
|
def insert(self, val) -> TreeNode:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""插入节点"""
|
2023-03-12 18:49:52 +08:00
|
|
|
|
self.__root = self.__insert_helper(self.__root, val)
|
|
|
|
|
return self.__root
|
2022-12-20 15:52:00 +08:00
|
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
|
def __insert_helper(self, node: TreeNode | None, val: int) -> TreeNode:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""递归插入节点(辅助方法)"""
|
2022-12-20 15:52:00 +08:00
|
|
|
|
if node is None:
|
2022-12-27 18:34:12 +08:00
|
|
|
|
return TreeNode(val)
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 1. 查找插入位置,并插入节点
|
2022-12-20 15:52:00 +08:00
|
|
|
|
if val < node.val:
|
2022-12-27 18:34:12 +08:00
|
|
|
|
node.left = self.__insert_helper(node.left, val)
|
2022-12-20 15:52:00 +08:00
|
|
|
|
elif val > node.val:
|
2022-12-27 18:34:12 +08:00
|
|
|
|
node.right = self.__insert_helper(node.right, val)
|
2022-12-20 15:52:00 +08:00
|
|
|
|
else:
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 重复节点不插入,直接返回
|
2022-12-20 15:52:00 +08:00
|
|
|
|
return node
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 更新节点高度
|
2022-12-20 15:52:00 +08:00
|
|
|
|
self.__update_height(node)
|
|
|
|
|
# 2. 执行旋转操作,使该子树重新恢复平衡
|
2022-12-27 18:34:12 +08:00
|
|
|
|
return self.__rotate(node)
|
2022-12-20 15:52:00 +08:00
|
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
|
def remove(self, val: int) -> TreeNode | None:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""删除节点"""
|
2023-03-12 18:49:52 +08:00
|
|
|
|
self.__root = self.__remove_helper(self.__root, val)
|
2023-04-09 05:05:35 +08:00
|
|
|
|
return self.__root
|
2022-12-20 15:52:00 +08:00
|
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
|
def __remove_helper(self, node: TreeNode | None, val: int) -> TreeNode | None:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""递归删除节点(辅助方法)"""
|
2022-12-20 15:52:00 +08:00
|
|
|
|
if node is None:
|
|
|
|
|
return None
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 1. 查找节点,并删除之
|
2022-12-20 15:52:00 +08:00
|
|
|
|
if val < node.val:
|
2022-12-27 18:34:12 +08:00
|
|
|
|
node.left = self.__remove_helper(node.left, val)
|
2022-12-20 15:52:00 +08:00
|
|
|
|
elif val > node.val:
|
2022-12-27 18:34:12 +08:00
|
|
|
|
node.right = self.__remove_helper(node.right, val)
|
2022-12-20 15:52:00 +08:00
|
|
|
|
else:
|
|
|
|
|
if node.left is None or node.right is None:
|
|
|
|
|
child = node.left or node.right
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 子节点数量 = 0 ,直接删除 node 并返回
|
2022-12-20 15:52:00 +08:00
|
|
|
|
if child is None:
|
|
|
|
|
return None
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 子节点数量 = 1 ,直接删除 node
|
2022-12-20 15:52:00 +08:00
|
|
|
|
else:
|
|
|
|
|
node = child
|
2023-04-09 04:32:17 +08:00
|
|
|
|
else: # 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
|
2023-01-02 19:53:55 +08:00
|
|
|
|
temp = self.__get_inorder_next(node.right)
|
2022-12-27 18:34:12 +08:00
|
|
|
|
node.right = self.__remove_helper(node.right, temp.val)
|
2022-12-20 15:52:00 +08:00
|
|
|
|
node.val = temp.val
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 更新节点高度
|
2022-12-20 15:52:00 +08:00
|
|
|
|
self.__update_height(node)
|
|
|
|
|
# 2. 执行旋转操作,使该子树重新恢复平衡
|
2022-12-27 18:34:12 +08:00
|
|
|
|
return self.__rotate(node)
|
2022-12-20 15:52:00 +08:00
|
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
|
def __get_inorder_next(self, node: TreeNode | None) -> TreeNode | None:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况)"""
|
2022-12-20 15:52:00 +08:00
|
|
|
|
if node is None:
|
|
|
|
|
return None
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 循环访问左子节点,直到叶节点时为最小节点,跳出
|
2022-12-20 15:52:00 +08:00
|
|
|
|
while node.left is not None:
|
|
|
|
|
node = node.left
|
|
|
|
|
return node
|
|
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
|
def search(self, val: int) -> TreeNode | None:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""查找节点"""
|
2023-03-12 18:49:52 +08:00
|
|
|
|
cur = self.__root
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 循环查找,越过叶节点后跳出
|
2022-12-20 15:52:00 +08:00
|
|
|
|
while cur is not None:
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 目标节点在 cur 的右子树中
|
2022-12-20 15:52:00 +08:00
|
|
|
|
if cur.val < val:
|
|
|
|
|
cur = cur.right
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 目标节点在 cur 的左子树中
|
2022-12-20 15:52:00 +08:00
|
|
|
|
elif cur.val > val:
|
|
|
|
|
cur = cur.left
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 找到目标节点,跳出循环
|
2022-12-20 15:52:00 +08:00
|
|
|
|
else:
|
|
|
|
|
break
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 返回目标节点
|
2022-12-20 15:52:00 +08:00
|
|
|
|
return cur
|
|
|
|
|
|
|
|
|
|
|
2023-04-09 05:30:02 +08:00
|
|
|
|
"""Driver Code"""
|
2022-12-20 15:52:00 +08:00
|
|
|
|
if __name__ == "__main__":
|
2023-04-09 05:05:35 +08:00
|
|
|
|
|
2022-12-20 15:52:00 +08:00
|
|
|
|
def test_insert(tree: AVLTree, val: int):
|
|
|
|
|
tree.insert(val)
|
2023-04-09 04:32:17 +08:00
|
|
|
|
print("\n插入节点 {} 后,AVL 树为".format(val))
|
2022-12-20 15:52:00 +08:00
|
|
|
|
print_tree(tree.root)
|
|
|
|
|
|
|
|
|
|
def test_remove(tree: AVLTree, val: int):
|
|
|
|
|
tree.remove(val)
|
2023-04-09 04:32:17 +08:00
|
|
|
|
print("\n删除节点 {} 后,AVL 树为".format(val))
|
2022-12-20 15:52:00 +08:00
|
|
|
|
print_tree(tree.root)
|
|
|
|
|
|
|
|
|
|
# 初始化空 AVL 树
|
|
|
|
|
avl_tree = AVLTree()
|
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 插入节点
|
|
|
|
|
# 请关注插入节点后,AVL 树是如何保持平衡的
|
2022-12-20 15:52:00 +08:00
|
|
|
|
test_insert(avl_tree, 1)
|
|
|
|
|
test_insert(avl_tree, 2)
|
|
|
|
|
test_insert(avl_tree, 3)
|
|
|
|
|
test_insert(avl_tree, 4)
|
|
|
|
|
test_insert(avl_tree, 5)
|
|
|
|
|
test_insert(avl_tree, 8)
|
|
|
|
|
test_insert(avl_tree, 7)
|
|
|
|
|
test_insert(avl_tree, 9)
|
|
|
|
|
test_insert(avl_tree, 10)
|
|
|
|
|
test_insert(avl_tree, 6)
|
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 插入重复节点
|
2022-12-20 15:52:00 +08:00
|
|
|
|
test_insert(avl_tree, 7)
|
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 删除节点
|
|
|
|
|
# 请关注删除节点后,AVL 树是如何保持平衡的
|
|
|
|
|
test_remove(avl_tree, 8) # 删除度为 0 的节点
|
|
|
|
|
test_remove(avl_tree, 5) # 删除度为 1 的节点
|
|
|
|
|
test_remove(avl_tree, 4) # 删除度为 2 的节点
|
2022-12-20 15:52:00 +08:00
|
|
|
|
|
|
|
|
|
result_node = avl_tree.search(7)
|
2023-04-09 04:32:17 +08:00
|
|
|
|
print("\n查找到的节点对象为 {},节点值 = {}".format(result_node, result_node.val))
|