红黑树的删除

以下是红黑树删除的伪代码:

 

RB-DELETE(T, z)
 1 if left[z] = nil[T] or right[z] = nil[T]
 2    then y  z
 3    else y  TREE-SUCCESSOR(z)
 4 if left[y]  nil[T]
 5    then x  left[y]
 6    else x  right[y]
 7 p[x]  p[y]
 8 if p[y] = nil[T]
 9    then root[T]  x
10    else if y = left[p[y]]
11            then left[p[y]]  x
12            else right[p[y]]  x
13 if y 3 z
14    then key[z]  key[y]
15         copy y's satellite data into z
16 if color[y] = BLACK
17    then RB-DELETE-FIXUP(T, x)
18 return y

 

RB-DELETE-FIXUP(T, x)

 1 while x root[T] and color[x] = BLACK

 2     do if x = left[p[x]]

 3           then w right[p[x]]

 4                if color[w] = RED

 5                   then color[w] BLACK                          Case 1

 6                        color[p[x]] RED                         Case 1

 7                        LEFT-ROTATE(T, p[x])                      Case 1

 8                        w right[p[x]]                           Case 1

 9                if color[left[w]] = BLACK and color[right[w]] = BLACK

10                   then color[w] RED                            Case 2

11                        x ← p[x]                                           Case 2

12                   else if color[right[w]] = BLACK

13                           then color[left[w]] BLACK            Case 3

14                                color[w] RED                    Case 3

15                                RIGHT-ROTATE(T, w)                Case 3

16                                w right[p[x]]                   Case 3

17                         color[w] color[p[x]]                   Case 4

18                         color[p[x]] BLACK                      Case 4

19                         color[right[w]] BLACK                  Case 4

20                         LEFT-ROTATE(T, p[x])                     Case 4

21                         x root[T]                              Case 4

22        else                 same as then clause with "right" and "left" exchange

     w left[p[x]]

                  if color[w] = RED

                     then color[w] BLACK                          Case 5

                          color[p[x]] RED                         Case 5

                          RIGHT-ROTATE(T, p[x])                      Case 5

                          w left[p[x]]                           Case 5

                  if color[right[w]] = BLACK and color[left[w]] = BLACK

                   then color[w] RED                            Case 6

                        x p[x]                                           Case 6

                   else if color[left[w]] = BLACK

                           then color[right[w]] BLACK            Case 7

                                color[w] RED                    Case 7

                                LEFT-ROTATE(T, w)                Case 7

                                w left[p[x]]                   Case 7

                         color[w] color[p[x]]                   Case 8

                         color[p[x]] BLACK                      Case 8

                         color[left[w]] BLACK                  Case 8

                         RIGHT-ROTATE(T, p[x])                     Case 8

                         x root[T]                              Case 8

23 color[x] BLACK

 

你可能感兴趣的:(Exchange)