Exemple #1
0
    def test_create_red_black_tree(self):
        left = Node(3)
        right = Node(20)
        root = Node(17, left=left, right=right)
        tree = RedBlackTree(root)

        assert_that(tree.root, is_(root))
        assert_that(root.left, is_(left))
        assert_that(root.right, is_(right))
        assert_parent_pointers_consistent(tree, sentinel=tree.nil)
    def test_recursive_tree_insert(self):
        keys = [random.randrange(1000) for _ in range(20)]
        tree = BinaryTree()

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

            assert_binary_search_tree(tree)
            assert_parent_pointers_consistent(tree)

        actual_keys = get_binary_tree_keys(tree)
        assert_that(actual_keys, contains_inanyorder(*keys))
Exemple #3
0
    def test_recursive_tree_insert(self):
        keys = [random.randrange(1000) for _ in range(20)]
        tree = BinaryTree()

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

            assert_binary_search_tree(tree)
            assert_parent_pointers_consistent(tree)

        actual_keys = get_binary_tree_keys(tree)
        assert_that(actual_keys, contains_inanyorder(*keys))
    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))
    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))
Exemple #6
0
    def test_treap_insert(self):
        keys = [random.randrange(1000) for _ in range(20)]
        treap = Treap()

        for key in keys:

            treap_insert(treap, tp.Node(key))

            assert_treap(treap)
            assert_parent_pointers_consistent(treap)

        actual_keys = get_binary_tree_keys(treap)
        assert_that(actual_keys, contains_inanyorder(*keys))
    def test_interval_insert(self):
        keys = [random.randrange(949) for _ in range(20)]
        tree = RedBlackTree(sentinel=IntervalNode(None, None))

        for key in keys:

            interval_insert(tree, IntervalNode(key, Interval(key, key + random.randint(0, 50))))

            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))
    def test_rb_insert(self):
        _, keys = get_random_array()
        tree = RedBlackTree()

        for key in keys:

            rb_insert(tree, Node(key), sentinel=tree.nil)

            assert_red_black_tree(tree, sentinel=tree.nil)
            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))
Exemple #9
0
    def test_os_insert(self):
        keys = [random.randrange(1000) for _ in range(20)]
        tree = RedBlackTree(sentinel=OSNode(None))

        for i, key in enumerate(keys):

            os_insert(tree, OSNode(key))

            assert_os_tree(tree)
            assert_that(tree.root.size, is_(equal_to(i + 1)))
            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))
Exemple #10
0
    def test_create_binary_tree(self):
        left = Node(3)
        right = Node(20)
        root = Node(17, left=left, right=right)
        tree = BinaryTree(root)

        assert_that(tree.root, is_(root))
        assert_that(root.left, is_(left))
        assert_that(root.right, is_(right))
        assert_that(left.left, is_(none()))
        assert_that(left.right, is_(none()))
        assert_that(right.left, is_(none()))
        assert_that(right.right, is_(none()))
        assert_parent_pointers_consistent(tree)
Exemple #11
0
    def test_create_binary_tree(self):
        left = Node(3)
        right = Node(20)
        root = Node(17, left=left, right=right)
        tree = BinaryTree(root)

        assert_that(tree.root, is_(root))
        assert_that(root.left, is_(left))
        assert_that(root.right, is_(right))
        assert_that(left.left, is_(none()))
        assert_that(left.right, is_(none()))
        assert_that(right.left, is_(none()))
        assert_that(right.right, is_(none()))
        assert_parent_pointers_consistent(tree)
Exemple #12
0
    def test_os_insert(self):
        keys = [random.randrange(1000) for _ in range(20)]
        tree = RedBlackTree(sentinel=OSNode(None))

        for i, key in enumerate(keys):

            os_insert(tree, OSNode(key))

            assert_os_tree(tree)
            assert_that(tree.root.size, is_(equal_to(i + 1)))
            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))
Exemple #13
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)
Exemple #14
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)
Exemple #15
0
    def test_interval_insert(self):
        keys = [random.randrange(949) for _ in range(20)]
        tree = RedBlackTree(sentinel=IntervalNode(None, None))

        for key in keys:

            interval_insert(
                tree,
                IntervalNode(key, Interval(key, key + random.randint(0, 50))))

            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))
Exemple #16
0
    def test_joinable_rb_insert(self):
        keys = [random.randrange(1000) for _ in range(20)]
        tree = RedBlackTree(sentinel=None)
        tree.bh = 0

        for key in keys:

            joinable_rb_insert(tree, Node(key))

            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)))
Exemple #17
0
    def test_joinable_rb_insert(self):
        keys = [random.randrange(1000) for _ in range(20)]
        tree = RedBlackTree(sentinel=None)
        tree.bh = 0

        for key in keys:

            joinable_rb_insert(tree, Node(key))

            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)))
Exemple #18
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)
Exemple #19
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)