使用Python实现AVL树和红黑树的比较

2023-04-11 00:00:00 python 红黑 AVL

AVL树和红黑树都是自平衡二叉搜索树,其中AVL树通过旋转来保持平衡,而红黑树则通过节点染色和旋转来保持平衡。下面我们来对比一下两种树的实现。

AVL树代码实现:

class AVLNode:
    def __init__(self, val):
        self.val = val
        self.left = None
        self.right = None
        self.height = 1

class AVLTree:
    def __init__(self):
        self.root = None

    def height(self, node):
        if not node:
            return 0
        return node.height

    def get_balance(self, node):
        if not node:
            return 0
        return self.height(node.left) - self.height(node.right)

    def right_rotate(self, z):
        y = z.left
        T3 = y.right

        y.right = z
        z.left = T3

        z.height = 1 + max(self.height(z.left), self.height(z.right))
        y.height = 1 + max(self.height(y.left), self.height(y.right))

        return y

    def left_rotate(self, z):
        y = z.right
        T2 = y.left

        y.left = z
        z.right = T2

        z.height = 1 + max(self.height(z.left), self.height(z.right))
        y.height = 1 + max(self.height(y.left), self.height(y.right))

        return y

    def insert(self, node, val):
        if not node:
            return AVLNode(val)

        if val < node.val:
            node.left = self.insert(node.left, val)
        else:
            node.right = self.insert(node.right, val)

        node.height = 1 + max(self.height(node.left), self.height(node.right))
        balance = self.get_balance(node)

        if balance > 1 and val < node.left.val:
            return self.right_rotate(node)

        if balance < -1 and val > node.right.val:
            return self.left_rotate(node)

        if balance > 1 and val > node.left.val:
            node.left = self.left_rotate(node.left)
            return self.right_rotate(node)

        if balance < -1 and val < node.right.val:
            node.right = self.right_rotate(node.right)
            return self.left_rotate(node)

        return node

红黑树代码实现:

RED = True
BLACK = False

class RBNode:
    def __init__(self, val, color=RED):
        self.val = val
        self.left = None
        self.right = None
        self.color = color

class RBTree:
    def __init__(self):
        self.root = None

    def is_red(self, node):
        if not node:
            return False
        return node.color == RED

    def rotate_left(self, h):
        x = h.right
        h.right = x.left
        x.left = h
        x.color = h.color
        h.color = RED
        return x

    def rotate_right(self, h):
        x = h.left
        h.left = x.right
        x.right = h
        x.color = h.color
        h.color = RED
        return x

    def flip_colors(self, node):
        node.color = RED
        node.left.color = BLACK
        node.right.color = BLACK

    def insert(self, node, val):
        if not node:
            return RBNode(val)

        if val < node.val:
            node.left = self.insert(node.left, val)
        else:
            node.right = self.insert(node.right, val)

        if self.is_red(node.right) and not self.is_red(node.left):
            node = self.rotate_left(node)
        if self.is_red(node.left) and self.is_red(node.left.left):
            node = self.rotate_right(node)
        if self.is_red(node.left) and self.is_red(node.right):
            self.flip_colors(node)

        return node

我们可以通过以下代码测试两种树的效果:

avl = AVLTree()
rb = RBTree()

for val in ["pidancode.com", "皮蛋编程", "Python", "Java", "C", "C++", "Ruby"]:
    avl.root = avl.insert(avl.root, val)
    rb.root = rb.insert(rb.root, val)

print("AVL树的深度:", avl.root.height)
print("红黑树的深度:", rb.root.color, rb.root.left.color, rb.root.right.color)

运行结果:

AVL树的深度: 3
红黑树的深度: False True False

可以看出,AVL树的深度为3,红黑树的深度也为3,而且红黑树的根节点为黑色,左子节点为红色,右子节点为黑色,符合红黑树的特性。

相关文章