示例#1
0
    def __rotate_right(self, a: Node) -> Node:
        b = a.left
        b.parent = a.parent
        a.left = b.right

        if a.left is not None:
            a.left.parent = a

        b.right = a
        a.parent = b

        if b.parent is not None:
            if b.parent.right != a:
                b.parent.left = b
            else:
                b.parent.right = b

        self.__set_balances(a, b)
        return b
示例#2
0
height

"""

from tree_node import Node


# Get tree height
def get_height(root):
    if root is None:
        return 0

    left = get_height(root.left)
    right = get_height(root.right)

    return 1 + max(left, right)


if __name__ == '__main__':
    root = Node(10)
    root.left = Node(7)
    root.right = Node(8)
    root.left.left = Node(1)
    root.left.right = Node(3)
    root.right.left = Node(5)
    root.right.right = Node(7)
    root.left.left.left = Node(-1)
    root.left.right.right = Node(2)
    root.left.right.right.right = Node(4)
    print(get_height(root))
示例#3
0
 def __rotate_left_then_right(self, target: Node) -> Node:
     target.left = self.__rotate_left(target.left)
     return self.__rotate_right(target)