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_persistent_tree_insert(self): keys = [random.randrange(1000) for _ in range(20)] tree = BinaryTree() for i, key in enumerate(keys): new_tree = persistent_tree_insert(tree, key) assert_binary_search_tree(new_tree) actual_keys_before_insertion = get_binary_tree_keys(tree) actual_keys_after_insertion = get_binary_tree_keys(new_tree) 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_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_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_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_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_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_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_interval_left_rotate(self): tree, nodes, keys = get_random_interval_tree() node = pick_node_with_right_child(nodes, tree) # node is for sure != tree.nil as the tree has black_height = 3 interval_left_rotate(tree, node) actual_inorder_keys = get_binary_tree_keys(tree, sentinel=tree.nil) assert_that(actual_inorder_keys, is_(equal_to(sorted(keys)))) expected_node_max = max(node.int.high, node.left.max, node.right.max) assert_that(node.max, is_(equal_to(expected_node_max))) node_parent = node.p expected_node_parent_max = max(node_parent.int.high, node.max, node_parent.right.max) assert_that(node_parent.max, is_(equal_to(expected_node_parent_max)))
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_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_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_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_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))
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))
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)
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)
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_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_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_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_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)))