コード例 #1
0
def small_test():
    root = BinaryTreeNode(5)
    assert not pair_includes_ancestor_and_descendant_of_m(root, root, root)
    root.left = BinaryTreeNode(2)
    root.left.right = BinaryTreeNode(4)
    assert not pair_includes_ancestor_and_descendant_of_m(root, root.left,
                                                          root.left.right)
    assert pair_includes_ancestor_and_descendant_of_m(root, root.left.right,
                                                      root.left)

    # Example of the first figure of BST chapter.
    root = BinaryTreeNode(19)
    root.left = BinaryTreeNode(7)
    root.left.left = BinaryTreeNode(3)
    root.left.left.left = BinaryTreeNode(2)
    root.left.left.right = BinaryTreeNode(5)
    root.left.right = BinaryTreeNode(11)
    root.left.right.right = BinaryTreeNode(17)
    root.left.right.right.left = BinaryTreeNode(13)
    root.right = BinaryTreeNode(43)
    root.right.left = BinaryTreeNode(23)
    root.right.left.right = BinaryTreeNode(37)
    root.right.left.right.left = BinaryTreeNode(29)
    root.right.left.right.left.right = BinaryTreeNode(31)
    root.right.left.right.right = BinaryTreeNode(41)
    root.right.right = BinaryTreeNode(47)
    root.right.right.right = BinaryTreeNode(53)
    assert not pair_includes_ancestor_and_descendant_of_m(root.right, root.left,
                                                          root.right.left)
    assert pair_includes_ancestor_and_descendant_of_m(
        root, root.right.left.right.left.right, root.right.left)
コード例 #2
0
def main():
    #      3
    #    2   5
    #  1    4 6
    tree = BinaryTreeNode(3)
    assert has_path_sum(tree, 3)
    tree.left = BinaryTreeNode(2)
    assert has_path_sum(tree, 5)
    tree.left.left = BinaryTreeNode(1)
    assert has_path_sum(tree, 6)
    tree.right = BinaryTreeNode(5)
    assert has_path_sum(tree, 8)
    assert not has_path_sum(tree, 7)
    tree.right.left = BinaryTreeNode(4)
    assert has_path_sum(tree, 12)
    assert not has_path_sum(tree, 1)
    assert not has_path_sum(tree, 3)
    assert not has_path_sum(tree, 5)
    tree.right.right = BinaryTreeNode(6)
    assert has_path_sum(tree, 6)
    assert not has_path_sum(tree, 7)
    assert has_path_sum(tree, 14)
    assert not has_path_sum(tree, -1)
    assert not has_path_sum(tree, 2**64 - 1)
    assert not has_path_sum(tree, -2**64)
コード例 #3
0
def main():
    #      3
    #    2   5
    #  1    4 6
    tree = BinaryTreeNode(3)
    tree.left = BinaryTreeNode(2)
    tree.left.left = BinaryTreeNode(1)
    tree.right = BinaryTreeNode(5)
    tree.right.left = BinaryTreeNode(4)
    tree.right.right = BinaryTreeNode(6)
    # should output 3
    x = lca(tree, tree.left, tree.right)
    assert x.data == 3
    print(x.data)
    # should output 5
    x = lca(tree, tree.right.left, tree.right.right)
    assert x.data == 5
    print(x.data)
    # should output 5
    x = lca(tree, tree.right, tree.right.right)
    assert x.data == 5
    print(x.data)
    # should output 3
    x = lca(tree, tree, tree.left.left)
    assert x.data == 3
    print(x.data)
    # should output 2
    x = lca(tree, tree.left, tree.left.left)
    assert x.data == 2
    print(x.data)
コード例 #4
0
def simple_test():
    symm_tree = BinaryTreeNode()
    assert is_symmetric(symm_tree)
    symm_tree.left = BinaryTreeNode()
    assert not is_symmetric(symm_tree)
    symm_tree.right = BinaryTreeNode()
    assert is_symmetric(symm_tree)
    symm_tree.right.right = BinaryTreeNode()
    assert not is_symmetric(symm_tree)
コード例 #5
0
def main():
    #      3
    #    2   5
    #  1    4 6
    tree = BinaryTreeNode(3)
    tree.left = BinaryTreeNode(2)
    tree.left.left = BinaryTreeNode(1)
    tree.right = BinaryTreeNode(5)
    tree.right.left = BinaryTreeNode(4)
    tree.right.right = BinaryTreeNode(6)
    tree_traversal(tree)
def main():
    #      3
    #    2   5
    #  1    4 6
    tree = BinaryTreeNode(3)
    tree.left = BinaryTreeNode(2)
    tree.left.left = BinaryTreeNode(1)
    tree.right = BinaryTreeNode(5)
    tree.right.left = BinaryTreeNode(4)
    tree.right.right = BinaryTreeNode(6)
    res = postorder_traversal(tree)
    golden_res = generate_postorder(tree)
    assert list(res) == golden_res
コード例 #7
0
def main():
    #      3
    #    2   5
    #  1    4 6
    tree = BinaryTreeNode(3)
    tree.left = BinaryTreeNode(2)
    tree.left.left = BinaryTreeNode(1)
    tree.right = BinaryTreeNode(5)
    tree.right.left = BinaryTreeNode(4)
    tree.right.right = BinaryTreeNode(6)
    # should output 1 2 3 4 5 6
    inorder_traversal(tree)
    golden_res = [1, 2, 3, 4, 5, 6]
    assert result == golden_res
コード例 #8
0
def main():
    #      3
    #    2   5
    #  1    4 7
    root = BinaryTreeNode(3)
    root.left = BinaryTreeNode(2)
    root.left.left = BinaryTreeNode(1)
    root.right = BinaryTreeNode(5)
    root.right.left = BinaryTreeNode(4)
    root.right.right = BinaryTreeNode(7)
    assert find_first_greater_than_k(root, 1) is root.left
    assert find_first_greater_than_k(root, 5) is root.right.right
    assert find_first_greater_than_k(root, 6) is root.right.right
    assert not find_first_greater_than_k(root, 7)
コード例 #9
0
def main():
    #      3
    #    2   5
    #  1    4 6
    root = BinaryTreeNode(3)
    root.left = BinaryTreeNode(2)
    root.left.left = BinaryTreeNode(1)
    root.right = BinaryTreeNode(5)
    root.right.left = BinaryTreeNode(4)
    root.right.right = BinaryTreeNode(6)
    k = 0
    ans = find_k_unbalanced_node(root, k)
    assert ans.data == 2
    if ans:
        print(ans.data)
コード例 #10
0
def main():
    #      1
    #    1   0
    #  0    1 0
    root = BinaryTreeNode(1)
    assert sum_root_to_leaf(root) == 1
    root.left = BinaryTreeNode(1)
    assert sum_root_to_leaf(root) == 3
    root.left.left = BinaryTreeNode(0)
    assert sum_root_to_leaf(root) == 6
    root.right = BinaryTreeNode(0)
    assert sum_root_to_leaf(root) == 8
    root.right.left = BinaryTreeNode(1)
    assert sum_root_to_leaf(root) == 11
    root.right.right = BinaryTreeNode(0)
    assert sum_root_to_leaf(root) == 15
コード例 #11
0
def main():
    #      3
    #    2   5
    #  1    4 6
    root = BinaryTreeNode(3)
    root.left = BinaryTreeNode(2)
    root.left.left = BinaryTreeNode(1)
    root.right = BinaryTreeNode(5)
    root.right.left = BinaryTreeNode(4)
    root.right.right = BinaryTreeNode(6)
    L = bst_to_doubly_linked_list(root)
    curr = L
    pre = float('-inf')
    while curr:
        assert pre <= curr.data
        print(curr.data)
        pre = curr.data
        curr = curr.right
コード例 #12
0
def main():
    #  balanced binary tree test
    #      3
    #    2   5
    #  1    4 6
    tree = BinaryTreeNode()
    tree.left = BinaryTreeNode()
    tree.left.left = BinaryTreeNode()
    tree.right = BinaryTreeNode()
    tree.right.left = BinaryTreeNode()
    tree.right.right = BinaryTreeNode()
    assert is_balanced_binary_tree(tree)
    print(is_balanced_binary_tree(tree))
    # Non-balanced binary tree test.
    tree = BinaryTreeNode()
    tree.left = BinaryTreeNode()
    tree.left.left = BinaryTreeNode()
    assert not is_balanced_binary_tree(tree)
    print(is_balanced_binary_tree(tree))
コード例 #13
0
def main():
    #      3
    #    2   5
    #  1    4 6
    # 10
    # 13
    tree = BinaryTreeNode(3)
    tree.left = BinaryTreeNode(2)
    tree.left.left = BinaryTreeNode(1)
    tree.left.left.left = BinaryTreeNode(10)
    tree.left.left.left.right = BinaryTreeNode(13)
    tree.right = BinaryTreeNode(5)
    tree.right.left = BinaryTreeNode(4)
    tree.right.right = BinaryTreeNode(6)
    # should output 3
    #               2 5
    #               1 4 6
    #               10
    #               13
    assert binary_tree_depth_order(tree) == [[3], [2, 5], [1, 4, 6], [10], [13]]
コード例 #14
0
def main():
    simple_test()
    #        3
    #    2      5
    #  1  0    4 6
    #   -1 -2
    tree = BinaryTreeNode(3)
    assert create_output_list(exterior_binary_tree(tree)) == [3]

    tree.left = BinaryTreeNode(2)
    assert create_output_list(exterior_binary_tree(tree)) == [3, 2]

    tree.left.right = BinaryTreeNode(0)
    tree.left.right.left = BinaryTreeNode(-1)
    tree.left.right.right = BinaryTreeNode(-2)
    tree.left.left = BinaryTreeNode(1)
    tree.right = BinaryTreeNode(5)
    tree.right.left = BinaryTreeNode(4)
    tree.right.right = BinaryTreeNode(6)
    assert create_output_list(
        exterior_binary_tree(tree)) == [3, 2, 1, -1, -2, 4, 6, 5]
コード例 #15
0
def main():
    #      3
    #    2   5
    #  1    4 6
    tree = BinaryTreeNode(3)
    tree.left = BinaryTreeNode(2)
    L = create_list_of_leaves(tree)
    assert len(L) == 1 and L[0].data == 2

    tree.left.left = BinaryTreeNode(1)
    tree.right = BinaryTreeNode(5)
    tree.right.left = BinaryTreeNode(4)
    tree.right.right = BinaryTreeNode(6)
    L = create_list_of_leaves(tree)
    output = []
    # should output 1, 4, 6
    for l in L:
        output.append(l.data)
        print(l.data)
    golden_res = [1, 4, 6]
    assert output == golden_res
def main():
    #      3
    #    2   6
    #  1    4 6
    root = BinaryTreeNode(3)
    root.left = BinaryTreeNode(2)
    root.left.left = BinaryTreeNode(1)
    root.right = BinaryTreeNode(6)
    root.right.left = BinaryTreeNode(4)
    root.right.right = BinaryTreeNode(6)
    assert not find_first_equal_k(root, 7)
    assert (find_first_equal_k(root, 6).data == 6 and
            find_first_equal_k(root, 6).right.data == 6)

    #      3
    #    3   5
    #  2    5 6
    root = BinaryTreeNode(3)
    root.left = BinaryTreeNode(3)
    root.left.left = BinaryTreeNode(2)
    root.right = BinaryTreeNode(5)
    root.right.left = BinaryTreeNode(5)
    root.right.right = BinaryTreeNode(6)
    assert not find_first_equal_k(root, 7)
    assert find_first_equal_k(root, 3) is root.left
    assert find_first_equal_k(root, 5) is root.right.left
    assert find_first_equal_k(root, 6).data == 6
コード例 #17
0
def main():
    #      3
    #    2   5
    #  1    4 6
    tree = BinaryTreeNode(3)
    tree.left = BinaryTreeNode(2)
    tree.left.left = BinaryTreeNode(1)
    tree.right = BinaryTreeNode(5)
    tree.right.left = BinaryTreeNode(4)
    tree.right.right = BinaryTreeNode(6)
    # should output True.
    assert is_binary_tree_BST(tree) == True
    print(is_binary_tree_BST(tree))
    #      10
    #    2   5
    #  1    4 6
    tree.data = 10
    # should output False.
    assert not is_binary_tree_BST(tree)
    print(is_binary_tree_BST(tree))
    # should output True.
    assert is_binary_tree_BST(None) == True
    print(is_binary_tree_BST(None))
コード例 #18
0
def main():
    small_test()
    #      3
    #    2   5
    #  1    4 6
    root = BinaryTreeNode(3)
    root.left = BinaryTreeNode(2)
    root.left.left = BinaryTreeNode(1)
    root.right = BinaryTreeNode(5)
    root.right.left = BinaryTreeNode(4)
    root.right.right = BinaryTreeNode(6)
    assert pair_includes_ancestor_and_descendant_of_m(root, root.right.right,
                                                      root.right)
    assert pair_includes_ancestor_and_descendant_of_m(root.right.right, root,
                                                      root.right)
    assert not pair_includes_ancestor_and_descendant_of_m(root, root.right,
                                                          root.right.right)
    assert not pair_includes_ancestor_and_descendant_of_m(root.right, root,
                                                          root.right.right)
    assert (not pair_includes_ancestor_and_descendant_of_m(
        root.right.left, root.right.right, root.right))
    assert (not pair_includes_ancestor_and_descendant_of_m(
        root.right.left, root.left.left, root.right))
コード例 #19
0
def test():
    #      3
    #    2   5
    #  1    4 6
    tree = BinaryTreeNode(3)
    tree.left = BinaryTreeNode(2)
    tree.left.left = BinaryTreeNode(1)
    tree.right = BinaryTreeNode(5)
    tree.right.left = BinaryTreeNode(4)
    tree.right.right = BinaryTreeNode(6)
    # should output True.
    assert is_binary_tree_bst(tree)
    assert is_binary_tree_bst_alternative(tree)
    #      10
    #    2   5
    #  1    4 6
    tree.data = 10
    # should output False.
    assert not is_binary_tree_bst(tree)
    assert not is_binary_tree_bst_alternative(tree)
    # should output True.
    assert is_binary_tree_bst(None)
    assert is_binary_tree_bst_alternative(None)
コード例 #20
0
def main():
    #      3
    #    2   5
    #  1    4 6
    L = BinaryTreeNode(3)
    L.left = BinaryTreeNode(2)
    L.left.left = BinaryTreeNode(1)
    L.right = BinaryTreeNode(5)
    L.right.left = BinaryTreeNode(4)
    L.right.right = BinaryTreeNode(6)
    #      7
    #    2   8
    #  0
    R = BinaryTreeNode(7)
    R.left = BinaryTreeNode(2)
    R.left.left = BinaryTreeNode(0)
    R.right = BinaryTreeNode(8)
    root = merge_two_bsts(L, R)
    # should output 0 1 2 2 3 4 5 6 7 8
    print_bst_inorder(root, float('-inf'))
コード例 #21
0
def main():
    simple_test()
    # Non symmetric tree test.
    #      3
    #    2   5
    #  1    4 6
    non_symm_tree = BinaryTreeNode()
    non_symm_tree.left = BinaryTreeNode()
    non_symm_tree.left.left = BinaryTreeNode()
    non_symm_tree.right = BinaryTreeNode()
    non_symm_tree.right.left = BinaryTreeNode()
    non_symm_tree.right.right = BinaryTreeNode()
    assert not is_symmetric(non_symm_tree)
    print(is_symmetric(non_symm_tree))
    # Symmetric tree test.
    symm_tree = BinaryTreeNode()
    symm_tree.left = BinaryTreeNode()
    symm_tree.right = BinaryTreeNode()
    assert is_symmetric(symm_tree)
    print(is_symmetric(symm_tree))
    # Empty tree test.
    symm_tree = None
    assert is_symmetric(symm_tree)
    print(is_symmetric(symm_tree))
コード例 #22
0
def main():
    #      3
    #    2   5
    #  1    4 6
    tree = BinaryTreeNode(3)
    tree.left = BinaryTreeNode(2)
    tree.left.left = BinaryTreeNode(1)
    tree.right = BinaryTreeNode(5)
    tree.right.left = BinaryTreeNode(4)
    tree.right.right = BinaryTreeNode(6)
    if len(sys.argv) == 2:
        k = int(sys.argv[1])
    else:
        k = random.randint(1, 6)
    print('k =', k)
    ans = find_k_largest_in_bst(tree, k)
    print(*ans, sep='\n')
    for i in range(1, len(ans)):
        assert ans[i - 1] >= ans[i]
    ans = find_k_largest_in_bst(tree, 2)
    assert ans[0] == 6
    assert ans[1] == 5

    #      3
    #    3   4
    #  1    4 6
    tree = BinaryTreeNode(3)
    tree.left = BinaryTreeNode(3)
    tree.left.left = BinaryTreeNode(1)
    tree.right = BinaryTreeNode(4)
    tree.right.left = BinaryTreeNode(4)
    tree.right.right = BinaryTreeNode(6)
    ans = find_k_largest_in_bst(tree, 3)
    assert ans[0] == 6
    assert ans[1] == 4
    assert ans[2] == 4