Exemple #1
0
def create_balanced_bst_helper(arr: List[int], tree: BinarySearchTree) -> None:
    # based on the fact that a sorted array middle element has approximately (with at
    # most difference of 1) equal number of elements on its either side
    if len(arr) == 0:
        return
    mid = len(arr) // 2
    tree.add(arr[mid])
    create_balanced_bst_helper(arr[:mid], tree)
    create_balanced_bst_helper(arr[mid + 1 :], tree)
Exemple #2
0
def bst_from_postorder(postorder: List[int]) -> BinarySearchTree:
    tree = BinarySearchTree()
    if postorder:
        tree.add(postorder[-1])
        for val in postorder[-2::-1]:
            tree.add(val)
    return tree
Exemple #3
0
            + get_sum_over_range_helper(node.right, low, high)
        )
    elif low > node.val:
        return get_sum_over_range_helper(node.right, low, high)
    return get_sum_over_range_helper(node.left, low, high)


def get_sum_over_range(tree: BinarySearchTree, sum_range: Tuple[int, int]) -> int:
    if tree.root is None:
        return 0
    low, high = sum_range
    return get_sum_over_range_helper(tree.root, low, high)


if __name__ == "__main__":
    tree = BinarySearchTree()

    tree.add(5)
    tree.add(3)
    tree.add(8)
    tree.add(2)
    tree.add(4)
    tree.add(6)
    tree.add(10)

    print(get_sum_over_range(tree, (4, 9)))


"""
SPECS:
if __name__ == "__main__":
    a = Node(10)
    b = Node(5)
    c = Node(30)
    d = Node(22)
    e = Node(35)

    a.left = b
    a.right = c
    c.left = d
    c.right = e

    b.parent = a
    c.parent = a
    d.parent = c
    e.parent = c

    tree = BinarySearchTree()
    tree.root = a

    print(tree)
    print(inorder_successor(d))
    print(inorder_successor(a))
"""
SPECS:

TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(1)
"""
Exemple #5
0
def create_balanced_bst(arr: List[int]) -> BinarySearchTree:
    tree = BinarySearchTree()
    create_balanced_bst_helper(arr, tree)
    return tree
    if node.left is None and node.right is None:
        return True
    elif (node.left
          and node.left.val > node.val) or (node.right
                                            and node.right.val < node.val):
        return False
    return is_binary_search_tree_helper(
        node.left) and is_binary_search_tree_helper(node.right)


def is_binary_search_tree(tree: BinaryTree) -> bool:
    return is_binary_search_tree_helper(tree.root)


if __name__ == "__main__":
    tree1 = BinarySearchTree()

    tree1.add(5)
    tree1.add(9)
    tree1.add(1)
    tree1.add(4)
    tree1.add(10)
    tree1.add(3)
    tree1.add(2)
    tree1.add(10)
    tree1.add(7)

    print(is_binary_search_tree(tree1))

    tree2 = BinaryTree()
    tree2.root = Node(5)
    else:
        if node.left:
            return get_floor(node.left, value)
        return None


def get_floor_and_ceiling(tree: BinarySearchTree,
                          value: int) -> Tuple[Optional[int], Optional[int]]:
    # function to get the ceiling and floor of the input in a binary search tree
    if tree.root:
        return get_floor(tree.root, value), get_ceiling(tree.root, value)
    return None, None


if __name__ == "__main__":
    tree = BinarySearchTree()

    tree.add(4)
    tree.add(2)
    tree.add(1)
    tree.add(3)
    tree.add(6)

    print(get_floor_and_ceiling(tree, 2))
    print(get_floor_and_ceiling(tree, 7))
    print(get_floor_and_ceiling(tree, -1))
    print(get_floor_and_ceiling(tree, 5))
"""
SPECS:

TIME COMPLEXITY: O(log(n))