def test_min_gap_tree(self):
        _, keys = get_random_unique_array()
        tree = RedBlackTree()
        tree.nil.min_key = tree.nil.min_gap = math.inf
        tree.nil.max_key = -math.inf

        for key in keys:
            min_gap_insert(tree, Node(key))

        nodes = get_binary_tree_nodes(tree, sentinel=tree.nil)

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

            actual_found = min_gap_search(tree, key)
            assert_that(actual_found.key, is_(equal_to(key)))

            min_gap_delete(tree, node)

            actual_found = min_gap_search(tree, key)
            assert_that(actual_found, is_(tree.nil))

            actual_min_gap = min_gap(tree)

            expected_min_gap = get_expected_min_gap(keys)
            assert_that(actual_min_gap, is_(equal_to(expected_min_gap)))
            nodes = get_binary_tree_nodes(tree, sentinel=tree.nil)
    def test_effective_os_tree(self):
        _, keys = get_random_array()
        tree = RedBlackTree(sentinel=OSNode(None))
        tree.nil.min = tree.nil.max = tree.nil.pred = tree.nil.succ = tree.nil

        for key in keys:
            effective_os_insert(tree, OSNode(key))

        nodes = get_binary_tree_nodes(tree, sentinel=tree.nil)

        while nodes:
            actual_minimum = effective_os_minimum(tree)
            actual_maximum = effective_os_maximum(tree)
            expected_minimum = rb_minimum(tree.root, sentinel=tree.nil)
            expected_maximum = rb_maximum(tree.root, sentinel=tree.nil)
            assert_that(actual_minimum, is_(expected_minimum))
            assert_that(actual_maximum, is_(expected_maximum))

            node = random.choice(nodes)
            actual_predecessor = effective_os_predecessor(tree, node)
            actual_successor = effective_os_successor(tree, node)
            expected_predecessor = rb_predecessor(node, sentinel=tree.nil)
            expected_successor = rb_successor(node, sentinel=tree.nil)
            assert_that(actual_predecessor, is_(expected_predecessor))
            assert_that(actual_successor, is_(expected_successor))

            effective_os_delete(tree, node)

            nodes = get_binary_tree_nodes(tree, sentinel=tree.nil)
Beispiel #3
0
    def test_interval_delete(self):
        tree, _, keys = get_random_interval_tree()
        nodes = get_binary_tree_nodes(tree, sentinel=tree.nil)

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

            interval_delete(tree, node)

            assert_interval_tree(tree)
            assert_parent_pointers_consistent(tree, sentinel=tree.nil)
            actual_keys = get_binary_tree_keys(tree, sentinel=tree.nil)
            assert_that(actual_keys, contains_inanyorder(*keys))
            nodes = get_binary_tree_nodes(tree, sentinel=tree.nil)
Beispiel #4
0
    def test_interval_delete(self):
        tree, _, keys = get_random_interval_tree()
        nodes = get_binary_tree_nodes(tree, sentinel=tree.nil)

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

            interval_delete(tree, node)

            assert_interval_tree(tree)
            assert_parent_pointers_consistent(tree, sentinel=tree.nil)
            actual_keys = get_binary_tree_keys(tree, sentinel=tree.nil)
            assert_that(actual_keys, contains_inanyorder(*keys))
            nodes = get_binary_tree_nodes(tree, sentinel=tree.nil)
Beispiel #5
0
    def test_persistent_rb_delete(self):
        tree, _, keys = get_random_red_black_tree()
        transform_tree_to_parentless_tree(tree)
        nodes = get_binary_tree_nodes(tree, sentinel=tree.nil)

        while nodes:
            node = random.choice(nodes)

            new_tree = persistent_rb_delete(tree, node)

            assert_red_black_tree(new_tree, sentinel=tree.nil)
            actual_keys_before_insertion = get_binary_tree_keys(tree, sentinel=tree.nil)
            actual_keys_after_insertion = get_binary_tree_keys(new_tree, sentinel=new_tree.nil)
            assert_that(actual_keys_before_insertion, contains_inanyorder(*keys))
            keys.remove(node.key)
            assert_that(actual_keys_after_insertion, contains_inanyorder(*keys))
            tree = new_tree
            nodes = get_binary_tree_nodes(tree, sentinel=tree.nil)
Beispiel #6
0
    def test_joinable_rb_delete(self):
        tree, _, keys = get_random_red_black_tree(sentinel=None)
        tree.bh = calculate_black_height(tree.root)
        nodes = get_binary_tree_nodes(tree, sentinel=tree.nil)

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

            joinable_rb_delete(tree, node)

            assert_red_black_tree(tree)
            assert_parent_pointers_consistent(tree)
            actual_keys = get_binary_tree_keys(tree)
            assert_that(actual_keys, contains_inanyorder(*keys))
            actual_black_height = calculate_black_height(tree.root)
            assert_that(tree.bh, is_(equal_to(actual_black_height)))
            nodes = get_binary_tree_nodes(tree, sentinel=tree.nil)
Beispiel #7
0
    def test_joinable_rb_delete(self):
        tree, _, keys = get_random_red_black_tree(sentinel=None)
        tree.bh = calculate_black_height(tree.root)
        nodes = get_binary_tree_nodes(tree, sentinel=tree.nil)

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

            joinable_rb_delete(tree, node)

            assert_red_black_tree(tree)
            assert_parent_pointers_consistent(tree)
            actual_keys = get_binary_tree_keys(tree)
            assert_that(actual_keys, contains_inanyorder(*keys))
            actual_black_height = calculate_black_height(tree.root)
            assert_that(tree.bh, is_(equal_to(actual_black_height)))
            nodes = get_binary_tree_nodes(tree, sentinel=tree.nil)
    def test_os_successor(self):
        tree, nodes, keys = get_random_os_tree()
        inorder_nodes = get_binary_tree_nodes(tree, sentinel=tree.nil)
        j = random.randrange(len(inorder_nodes))
        i = random.randrange(0, len(nodes) - j)

        actual_successor = os_successor(tree, inorder_nodes[j], i)

        expected_successor = inorder_nodes[j + i]
        assert_that(actual_successor, is_(expected_successor))
    def test_os_successor(self):
        tree, nodes, keys = get_random_os_tree()
        inorder_nodes = get_binary_tree_nodes(tree, sentinel=tree.nil)
        j = random.randrange(len(inorder_nodes))
        i = random.randrange(0, len(nodes) - j)

        actual_successor = os_successor(tree, inorder_nodes[j], i)

        expected_successor = inorder_nodes[j + i]
        assert_that(actual_successor, is_(expected_successor))