if u.p == T.nil T.root = v else if u == u.p.left u.p.left = v else u.p.right = v v.p = u.p
y = z y-original-color = z.color if z.left == T.nil //左子树为空 x = z.left RB-TRANSPLANT(T, z, z.left) //左孩子结点替换z的位置 else if z.right == T.nil //右子树为空 x = z.right RB-TRANSPLANT(T, z, z.right) //右孩子结点替换z的位置 else y = TREE-MINIMUM(z.right) //左右孩子都不为空的情况下,找到z的右孩子的最左边的孩子 y-original-color = y.color x = y.right //记住y的右孩子 if y.p == z //z.right的最左孩子是本身时 x.p = y else RB-TRANSPLANT(T, y, x) //用x去替换y的位置 y.right = z.right y.right.p = y RB-TRANSPLANT(T, z, y) //用y去替换z的位置 y.left = z.left y.left.p = y y.color = z.color if y-original-color == BLACK //如果y是红色则不影响 RB-DELETE-FIXUP(T,x)
while x != T.nil and x.color == BLACK if x == x.p.left w = x.p.right if w.color == RED //x的叔叔结点是红色 w.color = BLACK //case 1 x.p.color = RED //case 1 RB-ROTATE-LEFT(T, x.p) //case 1 w = x.p.right //case 1 if w.right.color == BLACK and w.left.color == BLACK //w的两个子结点都是黑色 w.color = RED //case 2 x = x.p //case 2 else if w.right.color == BLACK //w的左孩子是红色,右孩子是黑色 w.left.color = BLACK //case 3 w.color = RED //case 3 RB-ROTATE-RIGHT(T, w) //case 3 w = x.p.right //case 3 else //w的两个孩子都是红色或者是右孩子是红色 w.right.color = BLACK //case 4 w.color = x.p.color //case 4 x.p.color = BLACK //case 4 RB-ROTATE-LEFT(T, x.p) //case 4 x = T.root //case 4 else w = x.p.left if w.color == RED x.p.color = RED w.color = BLACK RIGHT-ROTATE(T, x.p) w = x.p.left if w.right.color == BLACK and w.left.color == BLACK w.color = RED x = x.p else if w.left.color == BLACK w.right.color = BLACK w.color = RED LEFT-ROTATE(T, w) w = x.p.left else w.left.color = BLACK w.color = x.p.color x.p.color = BLACK RIGHT-ROTATE(T, x.p) x = T.root x.color = BLACK
原文地址:http://blog.csdn.net/qianligaoshan/article/details/25011391