实现一个自平衡 AVL 树并支持插入、删除、查找操作。

实现一个自平衡的 AVL 树需要维护每个节点的平衡因子,并在插入或删除节点后通过旋转操作来保持树的平衡。下面是一个 Python 实现的 AVL 树,支持插入、删除和查找操作:

pythonCopy Codeclass TreeNode:    def __init__(self, key, height=1, left=None, right=None):
        self.key = key
        self.height = height
        self.left = left
        self.right = rightclass AVLTree:    def __init__(self):
        self.root = None
    def get_height(self, node):        if not node:            return 0
        return node.height    def get_balance(self, node):        if not node:            return 0
        return self.get_height(node.left) - self.get_height(node.right)    def right_rotate(self, y):
        x = y.left
        T2 = x.right
        x.right = y
        y.left = T2
        y.height = max(self.get_height(y.left), self.get_height(y.right)) + 1
        x.height = max(self.get_height(x.left), self.get_height(x.right)) + 1
        return x    def left_rotate(self, x):
        y = x.right
        T2 = y.left
        y.left = x
        x.right = T2
        x.height = max(self.get_height(x.left), self.get_height(x.right)) + 1
        y.height = max(self.get_height(y.left), self.get_height(y.right)) + 1
        return y    def insert(self, root, key):        if not root:            return TreeNode(key)        elif key < root.key:
            root.left = self.insert(root.left, key)        else:
            root.right = self.insert(root.right, key)
        root.height = 1 + max(self.get_height(root.left), self.get_height(root.right))
        balance = self.get_balance(root)        # Left Left Case
        if balance > 1 and key < root.left.key:            return self.right_rotate(root)        # Right Right Case
        if balance < -1 and key > root.right.key:            return self.left_rotate(root)        # Left Right Case
        if balance > 1 and key > root.left.key:
            root.left = self.left_rotate(root.left)            return self.right_rotate(root)        # Right Left Case
        if balance < -1 and key < root.right.key:
            root.right = self.right_rotate(root.right)            return self.left_rotate(root)        return root    def min_value_node(self, node):        if node is None or node.left is None:            return node        return self.min_value_node(node.left)    def delete(self, root, key):        if not root:            return root        if key < root.key:
            root.left = self.delete(root.left, key)        elif key > root.key:
            root.right = self.delete(root.right, key)        else:            if root.left is None:
                temp = root.right
                root = None
                return temp            elif root.right is None:
                temp = root.left
                root = None
                return temp
            temp = self.min_value_node(root.right)
            root.key = temp.key
            root.right = self.delete(root.right, temp.key)        if root is None:            return root
        root.height = 1 + max(self.get_height(root.left), self.get_height(root.right))
        balance = self.get_balance(root)        # Left Left Case
        if balance > 1 and self.get_balance(root.left) >= 0:            return self.right_rotate(root)        # Left Right Case
        if balance > 1 and self.get_balance(root.left) < 0:
            root.left = self.left_rotate(root.left)            return self.right_rotate(root)        # Right Right Case
        if balance < -1 and self.get_balance(root.right) <= 0:            return self.left_rotate(root)        # Right Left Case
        if balance < -1 and self.get_balance(root.right) > 0:
            root.right = self.right_rotate(root.right)            return self.left_rotate(root)        return root    def search(self, root, key):        if root is None or root.key == key:            return root        if key < root.key:            return self.search(root.left, key)        return self.search(root.right, key)    def insert_key(self, key):
        self.root = self.insert(self.root, key)    def delete_key(self, key):
        self.root = self.delete(self.root, key)    def search_key(self, key):        return self.search(self.root, key) is not None# 使用示例avl_tree = AVLTree()
avl_tree.insert_key(10)
avl_tree.insert_key(20)
avl_tree.insert_key(30)
avl_tree.insert_key(40)
avl_tree.insert_key(50)
avl_tree.insert_key(25)print(avl_tree.search_key(25))  # 输出: Trueprint(avl_tree.search_key(15))  # 输出: Falseavl_tree.delete_key(20)print(avl_tree.search_key(20))  # 输出: False

代码说明:

  • TreeNode类‌:表示树的节点,包含键值、高度、左子节点和右子节点。
  • AVLTree类‌:实现AVL树,包含插入、删除、查找、旋转和平衡操作。
  • 旋转操作‌:包括左旋和右旋,用于保持树的平衡。
  • 插入和删除‌:在插入或删除节点后,通过调整平衡因子和旋转操作来保持树的平衡。
  • 查找操作‌:递归查找给定键值的节点。

这个实现提供了基本的AVL树功能,可以根据需要进行扩展和优化。


http://wangluo.zgjzs.org

http://zhineng.zgjzs.org

http://gaosu.zgjzs.org

http://anquan.zgjzs.org

http://wangzhan.zgjzs.org

http://jiaoyu.zgjzs.org

http://guanggao.zgjzs.org

http://shuju.zgjzs.org

http://zhifu.zgjzs.org

http://yingyong.zgjzs.org

http://yonghu.zgjzs.org

http://houtai.zgjzs.org

http://fenxi.zgjzs.org

http://jiekou.zgjzs.org

http://fenxiang.zgjzs.org

http://neirong.zgjzs.org

http://zhuce.zgjzs.org

http://tongji.zgjzs.org

http://ditu.zgjzs.org

http://tuisong.zgjzs.org

http://gouwu.zgjzs.org

http://tuiguang.zgjzs.org

http://shejiao.zgjzs.org

http://zhongguo.zgjzs.org

http://wangguan.zgjzs.org

http://yule.zgjzs.org

http://pingtai.zgjzs.org

http://jichu.zgjzs.org

http://wangjie.zgjzs.org

http://zhishi.zgjzs.org

http://shangcheng.zgjzs.org

http://dianzi.zgjzs.org

http://yinyue.zgjzs.org

http://tupian.zgjzs.org

http://shipin.zgjzs.org

http://wuliu.zgjzs.org

http://gongju.zgjzs.org

http://anquan.zgjzs.org

http://sousuo.zgjzs.org

http://mima.zgjzs.org

http://jiankong.zgjzs.org

http://ceshi.zgjzs.org

http://wangyou.zgjzs.org

http://zixun.zgjzs.org

http://chajian.zgjzs.org

http://kuaidi.zgjzs.org

http://liuliang.zgjzs.org

http://zhengce.zgjzs.org

http://peizhi.zgjzs.org

http://baocun.zgjzs.org

http://yonghu.shangpin.org

http://houtai.shangpin.org

http://pingtai.shangpin.org

http://wangzhan.shangpin.org

http://zhifu.shangpin.org

http://guanggao.shangpin.org

http://yinyue.shangpin.org

http://zixun.shangpin.org

http://shejiao.shangpin.org

http://tuisong.shangpin.org

http://wangguan.shangpin.org

http://zhineng.shangpin.org

http://wangluo.shangpin.org

http://anquan.shangpin.org

http://zhongguo.shangpin.org

http://shuju.shangpin.org

http://jiekou.shangpin.org

http://zhishi.shangpin.org

http://gongneng.shangpin.org

http://tongji.shangpin.org

http://shipin.shangpin.org

http://jiaoyu.shangpin.org

http://ditu.shangpin.org

http://dianzi.shangpin.org

http://mima.shangpin.org

http://liuliang.shangpin.org

http://gouwu.shangpin.org

http://zhengce.shangpin.org

http://yule.shangpin.org

http://baocun.shangpin.org

http://jichu.shangpin.org

http://fenxi.shangpin.org

http://fenxiang.shangpin.org

http://shangcheng.shangpin.org

http://kuaidi.shangpin.org

http://peizhi.shangpin.org

http://wuliu.shangpin.org

http://ceshi.shangpin.org

http://tupian.shangpin.org

http://wangyou.shangpin.org

http://chajian.shangpin.org

http://tuiguang.shangpin.org

http://yingyong.shangpin.org

http://zhuce.shangpin.org

http://sousuo.shangpin.org

http://jiankong.shangpin.org

http://neirong.shangpin.org

http://gongju.shangpin.org

http://zonghe.shangpin.org

http://zhifu.snjrw.org

http://houtai.snjrw.org

http://yule.snjrw.org

http://wangluo.snjrw.org

http://guanggao.snjrw.org

http://shuju.snjrw.org

http://pingtai.snjrw.org

http://ditu.snjrw.org

http://wangzhan.snjrw.org

http://shejiao.snjrw.org

http://jiekou.snjrw.org

http://zhishi.snjrw.org

http://zhineng.snjrw.org

http://jiaoyu.snjrw.org

http://zhongguo.snjrw.org

http://yinyue.snjrw.org

http://yonghu.snjrw.org

http://tuiguang.snjrw.org

http://gouwu.snjrw.org

http://baocun.snjrw.org

http://fenxiang.snjrw.org

http://fenxi.snjrw.org

http://gongneng.snjrw.org

http://wangguan.snjrw.org

http://zixun.snjrw.org

http://dianzi.snjrw.org

http://tupian.snjrw.org

http://wangyou.snjrw.org

http://chajian.snjrw.org

http://wuliu.snjrw.org

http://mima.snjrw.org

http://liuliang.snjrw.org

http://zhuce.snjrw.org

http://zhengce.snjrw.org

http://peizhi.snjrw.org

http://shipin.snjrw.org

http://jichu.snjrw.org

http://tongji.snjrw.org

http://kuaidi.snjrw.org

http://yingyong.snjrw.org

http://sousuo.snjrw.org

http://jiankong.snjrw.org

http://neirong.snjrw.org

http://gongju.snjrw.org

http://tuisong.snjrw.org

http://shangcheng.snjrw.org

http://anli.snjrw.org

http://dianhua.snjrw.org

http://fuwu.snjrw.org

请使用浏览器的分享功能分享到微信等