next up previous
Next: Up: Previous:

Pseudocode

   RB-Delete-Fixup(T,x)
      while x <> root(T) and color(x) = Black
         if x = left(parent(x))
         then s = right(parent(x))               ; Get x's sibling
              if color(s) = Red
              then color(s) = Black              ; Case I
                   color(parent(x)) = Red
                   Left-Rotate(T, parent(x))
                   s = right(parent(x))
              if color(left(s)) = Black and color(right(s)) = Black
              then color(s) = Red                ; Case II
                   x = parent(x)
              else if color(right(s)) = Black
                   then color(left(s)) = Black   ; Case III
                        color(s) = Red
                        Right-Rotate(T,s)
                        s = right(parent(x))
                   color(s) = color(parent(x))   ; Case IV
                   color(parent(x)) = Black
                   color(right(s)) = Black
                   Left-Rotate(T, parent(x))
                   x = root(T)
         else
            ...         ; Same as then with right and left swapped
   color(x) = Black

The performance of this algorithm is O(lgn), with $\leq$ 3 rotations
Thus we see that RB trees maintain O(lgn) time for dynamic-set operations.

RBT Example


next up previous
Next: Up: Previous: