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()
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
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))
def __rotate_right_then_left(self, target: Node) -> Node: target.right = self.__rotate_right(target.right) return self.__rotate_left(target)