def rb_join(T1, x, T2): T = RedBlackTree(sentinel=None) if T1.bh >= T2.bh: if T2.root is None: joinable_rb_insert(T1, x) return T1 T.root = x T.bh = T1.bh y = rb_join_point(T1, T2) x.left = y x.right = T2.root if y is not T1.root: if y is y.p.left: y.p.left = x else: y.p.right = x T.root = T1.root x.p = y.p T2.root.p = y.p = x else: if T1.root is None: joinable_rb_insert(T2, x) return T2 T.root = x T.bh = T2.bh y = rb_symmetric_join_point(T1, T2) x.right = y x.left = T1.root if y is not T2.root: if y is y.p.right: y.p.right = x else: y.p.left = x T.root = T2.root x.p = y.p T1.root.p = y.p = x x.color = Red joinable_rb_insert_fixup(T, x) return T
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)))