Пример #1
0
    def _rotate_right(self, node):

        assert_node(node)
        assert_node(node.left)

        if DEBUG:
            print(f'# Rotating right {node}...')

        parent = node.parent
        new = node.left

        # 1
        if parent:
            if node.is_left_child():
                parent.left = new
            else:
                parent.right = new
            new.parent = parent
        else:
            self.root = new
            new.parent = None

        # 2
        node.left = new.right
        if node.left:
            node.left.parent = node

        # 3
        new.right = node
        node.parent = new
Пример #2
0
    def _rebalance_left(self, node):

        assert_node(node)

        if DEBUG:
            print(f'# Rebalancing left {node}...')

        middle = node.right

        if middle.lheight > middle.rheight:
            self._rotate_right(middle)

        self._rotate_left(node)

        node.recompute_height()
        middle.recompute_height()
Пример #3
0
    def _rebalance(self, node):

        assert_node(node)

        if DEBUG:
            print(f'# Rebalancing {node}...')

        parent = node.parent

        if node.lheight > node.rheight + 1:
            self._rebalance_right(node)

        if node.rheight > node.lheight + 1:
            self._rebalance_left(node)

        node.recompute_height()

        if parent:
            self._rebalance(parent)