コード例 #1
0
def main():
    root = Node(data=1)
    root = add_right(2, root)
    root.right = add_right(5, root.right)
    root.right.right = add_right(6, root.right.right)
    root.right.right = add_left(3, root.right.right)
    root.right.right.left = add_right(4, root.right.right.left)

    post_order(root)
    print()
コード例 #2
0
    def __rotate_left(self, a: Node) -> Node:
        b = a.right
        b.parent = a.parent
        a.right = b.left

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

        b.left = a
        a.parent = b

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

        self.__set_balances(a, b)
        return b
コード例 #3
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))
コード例 #4
0
 def __rotate_right_then_left(self, target: Node) -> Node:
     target.right = self.__rotate_right(target.right)
     return self.__rotate_left(target)