def test_in_balanced_returns_true_when_tree_is_balanced():
    root = TreeNode(5)
    root.left = TreeNode(3)
    root.left.left = TreeNode(2)
    root.right = TreeNode(6)

    assert_true(is_balanced(root))
예제 #2
0
def test_is_binary_search_tree_returns_true_given_valid_bst():
    bst = TreeNode(5)
    bst.left = TreeNode(3)
    bst.right = TreeNode(6)
    bst.left.left = TreeNode(2)
    bst.left.right = TreeNode(4)

    assert_true(is_binary_search_tree(bst))
def test_in_balance_returns_false_when_tree_not_balanced():
    root = TreeNode(5)
    root.left = TreeNode(3)
    root.left.left = TreeNode(2)
    root.left.left.left = TreeNode(1)
    root.right = TreeNode(6)

    assert_false(is_balanced(root))
예제 #4
0
def test_is_binary_search_tree_returns_false_given_invalid_bst():
    bst = TreeNode(5)
    bst.left = TreeNode(3)
    bst.right = TreeNode(6)
    bst.left.left = TreeNode(2)
    bst.left.right = TreeNode(
        40
    )  # This node violates the order property since it is greater than the root

    assert_false(is_binary_search_tree(bst))
예제 #5
0
def test_list_of_depths_returns_populates_correctly_from_stick_tree():
    root = TreeNode(5)
    root.left = TreeNode(4)
    root.left.left = TreeNode(3)
    root.left.left.left = TreeNode(2)
    root.left.left.left.left = TreeNode(1)

    result = list_of_depths(root)
    expected = [[root], [root.left], [root.left.left], [root.left.left.left],
                [root.left.left.left.left]]

    assert_equal(result, expected)
예제 #6
0
def build_minimal_tree(arr):
    """Builds a tree of minimal height by recursively partitioning the array

    Args:
        arr: list from which to build the binary tree

    Returns:
        Binary Search Tree of minimal height
    """
    if len(arr) > 0:
        root_index = len(arr) // 2
        root = TreeNode(arr[root_index])
        root.left = build_minimal_tree(arr[:root_index])
        root.right = build_minimal_tree(arr[root_index + 1:])
        return root
    return None
예제 #7
0
def test_list_of_depths_populates_from_multi_level_tree():
    root = TreeNode(5)
    root.left = TreeNode(2)
    root.left.left = TreeNode(1)
    root.left.right = TreeNode(3)
    root.left.right.right = TreeNode(4)
    root.right = TreeNode(6)

    result = list_of_depths(root)
    expected = [
        [root],
        [root.left, root.right],
        [root.left.left, root.left.right],
        [root.left.right.right],
    ]

    assert_equal(result, expected)