Esempio n. 1
0
def create_bst(lst, start, end):
    """
    Create a BST with minimal height
    Args:
        lst(List): A sorted array with unique integer elements
        start(int): Starting index
        end(int): Last index
    Returns:
        Integer/None: Value of root of each subtree
    """
    if end < start:
        return None
    mid = (start + end) // 2
    root = BinaryTree(lst[mid])
    root.left_child = create_bst(lst, start, mid - 1)
    root.right_child = create_bst(lst, mid + 1, end)
    # post-order traversal
    print(root.get_root_val())
    return root
Esempio n. 2
0
        return -1
    else:
        return max(left_height, right_height) + 1


def is_balanced(root):
    """
    Check if tree is balanced
    root(BinaryTree): The root of the tree
    Returns:
         Boolean: Indicates if tree is balanced
    """
    if check_height(root) == -1:
        return False
    else:
        return True


r = BinaryTree('a')
print(r.get_root_val())
print(r.get_left_child())
r.insert_left('b')
print(r.get_left_child())
print(r.get_left_child().get_root_val())
r.insert_right('c')
print(r.get_right_child())
print(r.get_right_child().get_root_val())
r.get_right_child().set_root_val('hello')
print(r.get_right_child().get_root_val())

print(is_balanced(r))