Пример #1
0
    def test_recursive_tree_maximum(self):
        tree, nodes, keys = get_random_binary_search_tree()

        actual_maximum = recursive_tree_maximum(tree.root)

        assert_that(actual_maximum, is_in(nodes))
        assert_that(actual_maximum.key, is_(equal_to(max(keys))))
Пример #2
0
    def test_balance_subtree(self):
        tree, nodes, _ = get_random_binary_search_tree()
        assign_size_attributes(tree.root)
        node = random.choice(nodes)

        actual_balanced_node = balance_subtree(tree, node)

        assert_binary_search_tree(tree)
        assert_subtree_weight_balanced(actual_balanced_node)
Пример #3
0
    def test_balance_subtree(self):
        tree, nodes, _ = get_random_binary_search_tree()
        assign_size_attributes(tree.root)
        node = random.choice(nodes)

        actual_balanced_node = balance_subtree(tree, node)

        assert_binary_search_tree(tree)
        assert_subtree_weight_balanced(actual_balanced_node)
Пример #4
0
    def test_stackless_inorder_tree_walk(self):
        tree, _, keys = get_random_binary_search_tree()
        captured_output = io.StringIO()

        with redirect_stdout(captured_output):
            stackless_inorder_tree_walk(tree)

        actual_output = [int(x) for x in captured_output.getvalue().splitlines()]
        expected_output = sorted(keys)
        assert_that(actual_output, is_(equal_to(expected_output)))
    def test_inorder_tree_walk_(self):
        tree, nodes, keys = get_random_binary_search_tree(min_size=0)
        captured_output = io.StringIO()

        with redirect_stdout(captured_output):
            inorder_tree_walk_(tree)

        actual_output = [int(x) for x in captured_output.getvalue().splitlines()]
        expected_output = sorted(keys)
        assert_that(actual_output, is_(equal_to(expected_output)))
Пример #6
0
    def test_iterative_tree_search(self):
        tree, nodes, keys = get_random_binary_search_tree(min_size=10, max_size=20, max_value=20)
        key_to_find = random.randint(0, 20)

        actual_node = iterative_tree_search(tree.root, key_to_find)

        if key_to_find in keys:
            assert_that(actual_node, is_in(nodes))
            assert_that(key_to_find, is_(equal_to(actual_node.key)))
        else:
            assert_that(actual_node, is_(none()))
Пример #7
0
    def test_tree_predecessor(self):
        tree, nodes, keys = get_random_binary_search_tree()
        given_node = random.choice(nodes)

        actual_predecessor = tree_predecessor(given_node)

        if actual_predecessor is None:
            assert_that(given_node.key, is_(equal_to(min(keys))))
        else:
            assert_that(actual_predecessor, is_in(nodes))
            assert_that(actual_predecessor.key, is_(less_than_or_equal_to(given_node.key)))
            for node in nodes:
                assert_that(node.key, is_not(all_of(greater_than(actual_predecessor.key), less_than(given_node.key))))
Пример #8
0
    def test_safe_tree_delete(self):
        tree, nodes, keys = get_random_binary_search_tree()
        random.shuffle(nodes)

        for node in nodes:
            keys.remove(node.key)

            safe_tree_delete(tree, node)

            assert_binary_search_tree(tree)
            assert_parent_pointers_consistent(tree)
            actual_keys = get_binary_tree_keys(tree)
            assert_that(actual_keys, contains_inanyorder(*keys))
Пример #9
0
    def test_safe_tree_delete(self):
        tree, nodes, keys = get_random_binary_search_tree()
        random.shuffle(nodes)

        for node in nodes:
            keys.remove(node.key)

            safe_tree_delete(tree, node)

            assert_binary_search_tree(tree)
            assert_parent_pointers_consistent(tree)
            actual_keys = get_binary_tree_keys(tree)
            assert_that(actual_keys, contains_inanyorder(*keys))
Пример #10
0
    def test_iterative_tree_search(self):
        tree, nodes, keys = get_random_binary_search_tree(min_size=10,
                                                          max_size=20,
                                                          max_value=20)
        key_to_find = random.randint(0, 20)

        actual_node = iterative_tree_search(tree.root, key_to_find)

        if key_to_find in keys:
            assert_that(actual_node, is_in(nodes))
            assert_that(key_to_find, is_(equal_to(actual_node.key)))
        else:
            assert_that(actual_node, is_(none()))
Пример #11
0
    def test_tree_delete(self):
        tree, _, keys = get_random_binary_search_tree()
        nodes = get_binary_tree_nodes(tree)

        while nodes:
            node = random.choice(nodes)
            keys.remove(node.key)

            tree_delete(tree, node)

            assert_binary_search_tree(tree)
            assert_parent_pointers_consistent(tree)
            actual_keys = get_binary_tree_keys(tree)
            assert_that(actual_keys, contains_inanyorder(*keys))
            nodes = get_binary_tree_nodes(tree)
Пример #12
0
    def test_tree_predecessor(self):
        tree, nodes, keys = get_random_binary_search_tree()
        given_node = random.choice(nodes)

        actual_predecessor = tree_predecessor(given_node)

        if actual_predecessor is None:
            assert_that(given_node.key, is_(equal_to(min(keys))))
        else:
            assert_that(actual_predecessor, is_in(nodes))
            assert_that(actual_predecessor.key,
                        is_(less_than_or_equal_to(given_node.key)))
            for node in nodes:
                assert_that(
                    node.key,
                    is_not(
                        all_of(greater_than(actual_predecessor.key),
                               less_than(given_node.key))))