Python中的树形数据结构: AVL-T树

2023-04-11 00:00:00 python 数据结构 AVL

AVL树是一种自平衡二叉查找树。它的基本思想是对于每个节点,其左右子树的高度差不超过1,通过旋转操作来保持树的平衡。

T树是一种类似于B树的数据结构,它的每个节点可以有多个子节点,且节点存储的数据是有序的。T树中,每个节点保存了一组(key, value)键值对,其中key是用于比较节点大小的标准。

将两种树结合起来,就得到了AVL-T树,是一种自平衡的多叉查找树,既能够支持快速插入和删除操作,又能够保持数据有序。它的主要优点是在执行插入、删除和查找操作时,时间复杂度为O(log n)。

下面是AVL-T树的Python实现代码:

class AVLTreeNode:
    def __init__(self, key=None, value=None):
        self.key = key
        self.value = value
        self.height = 1
        self.left = None
        self.right = None

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

    def insert(self, key, value):
        self.root = self._insert(self.root, key, value)

    def _insert(self, node, key, value):
        if node is None:
            return AVLTreeNode(key, value)
        elif key < node.key:
            node.left = self._insert(node.left, key, value)
        else:
            node.right = self._insert(node.right, key, value)

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

        balance_factor = self.get_balance_factor(node)

        if balance_factor > 1 and key < node.left.key:
            return self.rotate_right(node)

        if balance_factor < -1 and key > node.right.key:
            return self.rotate_left(node)

        if balance_factor > 1 and key > node.left.key:
            node.left = self.rotate_left(node.left)
            return self.rotate_right(node)

        if balance_factor < -1 and key < node.right.key:
            node.right = self.rotate_right(node.right)
            return self.rotate_left(node)

        return node

    def delete(self, key):
        self.root = self._delete(self.root, key)

    def _delete(self, node, key):
        if node is None:
            return node
        elif key < node.key:
            node.left = self._delete(node.left, key)
        elif key > node.key:
            node.right = self._delete(node.right, key)
        else:
            if node.left is None:
                temp = node.right
                node = None
                return temp
            elif node.right is None:
                temp = node.left
                node = None
                return temp
            temp = self.get_min_value_node(node.right)
            node.key = temp.key
            node.value = temp.value
            node.right = self._delete(node.right, temp.key)

        if node is None:
            return node

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

        balance_factor = self.get_balance_factor(node)

        if balance_factor > 1 and self.get_balance_factor(node.left) >= 0:
            return self.rotate_right(node)

        if balance_factor < -1 and self.get_balance_factor(node.right) <= 0:
            return self.rotate_left(node)

        if balance_factor > 1 and self.get_balance_factor(node.left) < 0:
            node.left = self.rotate_left(node.left)
            return self.rotate_right(node)

        if balance_factor < -1 and self.get_balance_factor(node.right) > 0:
            node.right = self.rotate_right(node.right)
            return self.rotate_left(node)

        return node

    def get_min_value_node(self, node):
        if node is None or node.left is None:
            return node

        return self.get_min_value_node(node.left)

    def get_height(self, node):
        if node is None:
            return 0

        return node.height

    def get_balance_factor(self, node):
        if node is None:
            return 0

        return self.get_height(node.left) - self.get_height(node.right)

    def rotate_left(self, node):
        new_root = node.right
        node.right = new_root.left
        new_root.left = node

        node.height = 1 + max(self.get_height(node.left), self.get_height(node.right))
        new_root.height = 1 + max(self.get_height(new_root.left), self.get_height(new_root.right))

        return new_root

    def rotate_right(self, node):
        new_root = node.left
        node.left = new_root.right
        new_root.right = node

        node.height = 1 + max(self.get_height(node.left), self.get_height(node.right))
        new_root.height = 1 + max(self.get_height(new_root.left), self.get_height(new_root.right))

        return new_root

使用范例:

avl_tree = AVLTree()
avl_tree.insert("pidancode.com", "1")
avl_tree.insert("皮蛋编程", "2")
avl_tree.insert("google.com", "3")
avl_tree.insert("apple.com", "4")
avl_tree.insert("microsoft.com", "5")

avl_tree.delete("google.com")

print(avl_tree.root.key) # "pidancode.com"
print(avl_tree.root.left.key) # "apple.com"
print(avl_tree.root.right.key) # "microsoft.com"

相关文章