PERSISTENT-TREE-INSERT(T, z) Let T' be a new tree if T.root == NIL T'.root = z return T' y = NIL x = COPY-NODE(T.root) T'.root = x while x != NIL y = x if z.key < x.key x = x.left if x != NIL x = COPY-NODE(x) y.left = x else x = x.right if x != NIL x = COPY-NODE(x) y.right = x if z.key < y.key y.left = z else y.right = z return T'
PERSISTENT-RB-INSERT(T, z) // P是从T.root到z的一条路径。并且这里对T'.nil和T.nil不做区分。 Let T' be a new tree if T.root == T.nil T'.root = z return T' y = T.nil x = COPY-NODE(T.root) T'.root = x let P be new array INSERT(P, T'.nil) INSERT(P, T'.root) while x != T.nil y = x if z.key < x.key x = x.left if x != NIL x = COPY-NODE(x) y.left = x INSERT(P, x) else x = x.right if x != NIL x = COPY-NODE(x) y.right = x INSERT(P, x) if z.key < y.key y.left = z else y.right = z z.left = T'.nil z.right = T'.nil z.color = RED INSERT(P, z) PERSISTENT-RB-INSERT-FIXUP(T', P) return T'
// P是从T.root到z中路径的所有节点 PERSISTENT-RB-INSERT-FIXUP(T, P) i = P.size - 1 while i > 0 and P[i - 1].color == RED if P[i - 1] == P[i - 2].left y = P[i - 2].right if y.color == RED P[i - 1].color = BLACK // 兄弟节点y不在路径上,它是原来的节点,需要被克隆。 y = COPY-NODE(y) y.color = BLACK P[i - 2].color = RED P[i - 2].right = y i = i - 2 else if P[i] == P[i - 1].right LEFT-ROTATE'(T, P[i - 2], P[i - 1]) exchange P[i] with P[i - 1] P[i - 1].color = BLACK P[i - 2].color = RED RIGHT-ROTATE'(T, P[i - 3], P[i - 2]) // 此时路径序列已经改变,不过也没有所谓,因为之后由于P[i - 1].color == BLACK使得这个循环被跳出。 else y = P[i - 2].left if y.color == RED P[i - 1].color = BLACK y = COPY-NODE(y) y.color = BLACK P[i - 2].color = RED P[i - 2].right = y i = i - 2 else if P[i] == P[i - 1].left RIGHT-ROTATE'(T, P[i - 2], P[i - 1]) exchange P[i] with P[i - 1] P[i - 1].color = BLACK P[i - 2].color = RED LEFT-ROTATE'(T, P[i - 3], P[i - 2])
ITERATIVE-SEARCH-PARENT(T, k) x = T.root y = T.nil while x != T.nil and k != x.key y = x if k < x.key x = x.left else x = x.right return y
// p是u的父亲节点 RB-TRANSPLANT'(T, p, u, v) if T.root == u T.root = v else if p.left == u p.left = v else p.right = v
PERSISTENT-RB-DELETE(T, z) Let T' be new tree Let P be new array x = T.root x = COPY-NODE(x) INSERT(P, T'.nil) INSERT(P, x) p = T.nil while x.key != z.key p = x if z.key < x.key x = x.left x = COPY-NODE(x) p.left = x else x = x.right x = COPY-NODE(x) p.right = y INSERT(P, x) // 目前P中存储了从T.root到z的路径,这其中的所有节点都被克隆。当前x是z的克隆版本。 z = x y = z y-original-color = y.color parent-z = ITERATIVE-SEARCH-PARENT(T', z) if z.left == T'.nil x = z.right RB-TRANSPLANT'(T', parent-z, z, z.right) INSERT(P, x) else if z.right == T.nil x = z.left RB-TRANSPLANT'(T', parent-z, z, z.left) INSERT(P, x) else // y = TREE-MINIMUM(z.right) // 继续往下补充,补充从z到x的路径,同样的是,x只需要是旧版即可。 y = z p = z.right while p != T.nil p = COPY-NODE(p) INSERT(P, p) y = p p = p.left y-original-color = y.color x = y.right INSERT(P, x) if y ≠ z.right parent-y = ITERATIVE-SEARCH-PARENT(T, y) RB-TRANSPLANT(T', parent-y, y, y.right) y.right = z.right RB-TRANSPLANT(T', parent-z, z, y) y.left = z.left y.color = z.color if y-original-color == BLACK PERSISTENT-RB-DELETE-FIXUP(T', P) return T'
// P是从T.root到x中路径的所有节点 PERSISTENT-RB-DELETE-FIXUP(T, P) i = P.size - 1 while i > 1 and P[i].color == BLACK if P[i] == P[i - 1].left w = P[i - 1].right // Case 1 if w.color == RED w = COPY-NODE(w) P[i - 1].right = w w.color = BLACK P[i - 1].color = RED LEFT-ROTATE'(T, P[i - 2], P[i - 1]) P[i - 1] = w if i == P.size - 1 INSERT(P, P[i]) else P[i + 1] = P[i] P[i] = P[i - 1] P[i - 1] = w i = i + 1 w = P[i - 1].right // Case 2 if w.left.color == BLACK and w.right.color == BLACK w = COPY-NODE(w) P[i - 1].right = w w.color = RED i = i - 1 else // Case 3 if w.right.color == BLACK w = COPY-NODE(w) P[i - 1].right = w v = w.left v = COPY-NODE(v) w.left = v v.color = BLACK w.color = RED RIGHT-ROTATE'(T, P[i - 1], w) w = P[i - 1].right // Case 4 w = COPY-NODE(w) P[i - 1].right = w v = w.right v = COPY-NODE(v) w.right = v w.color = P[i - 1].color P[i - 1].color = BLACK v.color = BLACK LEFT-ROTATE'(T, P[i - 2], P[i - 1]) i = 0 else w = P[i - 1].left // Case 1 if w.color == RED w = COPY-NODE(w) P[i - 1].right = w w.color = BLACK P[i - 1].color = RED RIGHT-ROTATE'(T, P[i - 2], P[i - 1]) P[i - 1] = w if i == P.size - 1 INSERT(P, P[i]) else P[i + 1] = P[i] P[i] = P[i - 1] P[i - 1] = w i = i + 1 w = P[i - 1].right // Case 2 if w.left.color == BLACK and w.right.color == BLACK w = COPY-NODE(w) P[i - 1].right = w w.color = RED i = i - 1 else // Case 3 if w.left.color == BLACK w = COPY-NODE(w) P[i - 1].right = w v = w.right v = COPY-NODE(v) w.left = v v.color = BLACK w.color = RED LEFT-ROTATE'(T, P[i - 1], w) w = P[i - 1].left // Case 4 w = COPY-NODE(w) P[i - 1].left = w v = w.left v = COPY-NODE(v) w.left = v w.color = P[i - 1].color P[i - 1].color = BLACK v.color = BLACK RIGHT-ROTATE'(T, P[i - 2], P[i - 1]) i = 0
CAL-MAX-NODE-EQ-BLACK-HEIGHT(T1, T2) k = T1.bh - T2.bh y = T1.root while y != T1.nil if y.color == BLACK if k == 0 break else k = k - 1 y = y.right return y
将\(x\)染成红色即可。按照题目13-2-b的算法,在\(T_1\)中找到一个黑高为\(T_2.bh\)的最大节点\(y\),并且按照题目\(c\)的方式,构造出了一棵黑高为\(T_2.bh\)的松弛红黑树\(T=T_y\cup \{x\}\cup T_2\)(注意\(T\)的根节点\(x\)是红色的),接下来调用RB-TRANSPLANT(T1, y, x)将树\(T\)的根\(x\)覆盖在树\(T_1\)在\(y\)这个位置上。那么在拼接前\(y\)的父节点\(y.p\)看来,左右两边的子树的黑高都是\(T_2.bh\),因此性质5仍然成立;由于叶子节点\(T.nil\)并未有任何的修改,因此性质3仍然成立;性质1也仍然成立。
CAL-MIN-NODE-EQ-BLACK-HEIGHT(T1, T2) k = T2.bh - T1.bh z = T2.root while z != T2.nil if z.color == BLACK if k == 0 break else k = k - 1 z = z.left return z
RB-JOIN(T1, x, T2) if T1.bh >= T2.bh y = CAL-MAX-NODE-EQ-BLACK-HEIGHT(T1, T2) x.left = y x.right = T2.root y.p = x T2.root.p = x RB-TRANSPLANT(T1, y, x) RB-INSERT-FIXUP(T1, x) else z = CAL-MAX-NODE-EQ-BLACK-HEIGHT(T1, T2) x.left = T1.root x.right = z T1.root.p = x z.p = x RB-TRANSPLANT(T2, z, x) RB-INSERT-FIXUP(T2, x)
AVL-INSERT(T, z) x = T.root y = T.nil while x ≠ T.nil y = x if z.key < x.key x = x.left else x = x.right z.p = y if y == T.nil T.root = z else if z.key < y.key y.left = z else y.right = z z.h = 0 x = z.p // 自底向上保持平衡,并且旋转后,$y.h$将会被正确计算出来。 while |x.left.h - x.right.h| > 1 y = BALANCE(x) x = y.p