def test_basic_structures(): for use_case, *expected_result in [ ( SIMPLE_TREE, True, True, 3 ), ( (1, [(2, [3, 4, 5, 6]), (10, [11])]), False, False, 3 ), ]: tree = BinarySearchTree(BinarySearchTree.build(use_case)) is_binary_tree, is_binary_search_tree, depth = expected_result assert tree.is_binary_tree == is_binary_tree, \ "{} != {}".format(tree.is_binary_tree, is_binary_tree) assert tree.is_binary_search_tree == is_binary_search_tree, \ "{} != {}".format(tree, is_binary_search_tree) assert tree.depth == depth, \ "{} != {}".format(tree.depth, depth)
def test_find_method_in_BST(): bst = BinarySearchTree(BinarySearchTree.build(SIMPLE_TREE)) for use_case, expected_result in [ (8, True), (20, True), (-1, False), ]: result = bst.find(use_case) assert result == expected_result, "{} != {}".format(result, expected_result)
def test_insert_method_for_BST(): bst = BinarySearchTree(BinarySearchTree.build(SIMPLE_TREE)) for use_case, *expected_result in [ (1, True, """ -- 8 | |-- 4 | | |-- 2 | | | |-- 1 | | |-- 6 | |-- 10 | | |-- 20"""), (15, True, """ -- 8 | |-- 4 | | |-- 2 | | | |-- 1 | | |-- 6 | |-- 10 | | |-- 20 | | | |-- 15"""), (15, False, """ -- 8 | |-- 4 | | |-- 2 | | | |-- 1 | | |-- 6 | |-- 10 | | |-- 20 | | | |-- 15"""), ]: insert_result, tree_repr = expected_result result = bst.insert(use_case) assert result == insert_result, "{} != {}".format(result, insert_result) assert str(bst) == tree_repr, "{} != {}".format(str(bst), tree_repr)
for count, (tree, expected_result) in enumerate([ [[2, [1, 3]], [[2, 1, 3], [2, 3, 1]]], [[ 2, [(1, [(0, [5, 6]), (3, [-1, -2])]), (6, [(5, [10, 11]), (7, [13, 14])])] ], [[[2, 1, 0, 5, 6], [2, 1, 0, 6, 5], [2, 1, 3, -1, -2], [2, 1, 3, -2, -1], [2, 1, 3, -1, -2], [2, 1, 3, -2, -1], [2, 1, 0, 5, 6], [2, 1, 0, 6, 5], [2, 6, 5, 10, 11], [2, 6, 5, 11, 10], [2, 6, 7, 13, 14], [2, 6, 7, 14, 13], [2, 6, 7, 13, 14], [2, 6, 7, 14, 13], [2, 6, 5, 10, 11], [2, 6, 5, 11, 10]], [[2, 6, 5, 10, 11], [2, 6, 5, 11, 10], [2, 6, 7, 13, 14], [2, 6, 7, 14, 13], [2, 6, 7, 13, 14], [2, 6, 7, 14, 13], [2, 6, 5, 10, 11], [2, 6, 5, 11, 10], [2, 1, 0, 5, 6], [2, 1, 0, 6, 5], [2, 1, 3, -1, -2], [2, 1, 3, -2, -1], [2, 1, 3, -1, -2], [2, 1, 3, -2, -1], [2, 1, 0, 5, 6], [2, 1, 0, 6, 5]]]], ]): bst = BinarySearchTree(BinarySearchTree.build(tree)) result = [] folded = _generate_weaved_nodes(bst.root) for fold in folded: r = unfold(fold) result.append(unfold(fold)) assert result == expected_result, "{} != {}".format( result, expected_result)