def test_rb_parentless_insert(self): keys = [random.randrange(1000) for _ in range(20)] tree = RedBlackTree() for key in keys: parentless_rb_insert(tree, Node(key)) assert_red_black_tree(tree, sentinel=tree.nil) actual_keys = get_binary_tree_keys(tree, sentinel=tree.nil) assert_that(actual_keys, contains_inanyorder(*keys))
def test_persistent_rb_insert(self): keys = [random.randrange(1000) for _ in range(20)] tree = RedBlackTree() for i, key in enumerate(keys): new_tree = persistent_rb_insert(tree, ParentlessNode(key)) 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=tree.nil) assert_that(actual_keys_before_insertion, contains_inanyorder(*keys[:i])) assert_that(actual_keys_after_insertion, contains_inanyorder(*keys[:i + 1])) tree = new_tree
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))
def test_rb_delete(self): tree, _, keys = get_random_red_black_tree() nodes = get_binary_tree_nodes(tree, sentinel=tree.nil) while nodes: node = random.choice(nodes) keys.remove(node.key) rb_delete(tree, node, 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)) nodes = get_binary_tree_nodes(tree, sentinel=tree.nil)
def test_rb_join(self): tree1, _, keys1 = get_random_red_black_tree(black_height=random.randint(0, 4), min_value=0, max_value=999, sentinel=None) tree1.bh = calculate_black_height(tree1.root) middle_key = random.randint(1000, 1999) x = Node(middle_key) tree2, _, keys2 = get_random_red_black_tree(black_height=random.randint(0, 4), min_value=2000, max_value=2999, sentinel=None) tree2.bh = calculate_black_height(tree2.root) actual_joined_tree = rb_join(tree1, x, tree2) assert_red_black_tree(actual_joined_tree) actual_keys = get_binary_tree_keys(actual_joined_tree) assert_that(actual_keys, contains_inanyorder(*(keys1 + [middle_key] + keys2)))
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)))
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)
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_rb_join(self): tree1, _, keys1 = get_random_red_black_tree( black_height=random.randint(0, 4), min_value=0, max_value=999, sentinel=None) tree1.bh = calculate_black_height(tree1.root) middle_key = random.randint(1000, 1999) x = Node(middle_key) tree2, _, keys2 = get_random_red_black_tree( black_height=random.randint(0, 4), min_value=2000, max_value=2999, sentinel=None) tree2.bh = calculate_black_height(tree2.root) actual_joined_tree = rb_join(tree1, x, tree2) assert_red_black_tree(actual_joined_tree) actual_keys = get_binary_tree_keys(actual_joined_tree) assert_that(actual_keys, contains_inanyorder(*(keys1 + [middle_key] + keys2)))