Algorithm Analysis (CSC 301 2015F) : EBoards

CSC301.01 2015F, Class 14: Red-Black Trees, Concluded


Overview

Preliminaries

Admin

Upcoming Work

Extra Credit

Academic

Peer

Questions

Deletion is confusing

We'll work through some of it today.

Problem 13.3-6

The RB-INSERT and RB-INSERT-FIXUP procedures often refer to x.p or x.p.p (or something equivalent). That suggests that the underlying implementation stores a parent pointer in each node. p means parent, at least in this context.

Real data structures try to conserve space. Do I really need the parent pointer? Can I write fixup without it? Yes, you can do the stupid "search the whole tree for the parent". But that's not efficient. So find an efficient approach. * Note: You may need to gather information during insert to use during fixup.

Key ideas from insertion

Deletion in binary search trees

Some simple cases (removing z)

        |
        z      =>  NIL
       / \    
     NIL NIL

        |         |
        z      => r
       / \       / \
     NIL  r     ?   ?
         / \
        ?   ?


        |         |
        z      => l
       / \       / \
      l  NIL    ?   ?
     / \
    ?   ?

        z     =>  y
       / \       / \
      l   y     l   x
         / \
       NIL  x

        z     =>           z          =>     y
       / \                / \               / \
      l   r              l   y             l   r
         / \                / \               / \
        .   ?             NIL  r             .
       .                      / \           .
      .                      .   ?         .
     y                      .             x
    / \                    .             
  NIL  x                  x

Deletion in red-black trees

        |
        z      =>  NIL
       / \    
     NIL NIL
        |         |
        z      => RED
       / \       / \
     NIL RED   NIL NIL
         / \
       NIL NIL
        z     =>  y
       / \       / \
      l   y     l   x
         / \
       NIL  x
        z     =>           z          =>     y
       / \                / \               / \
      l   r              l   y             l   r
         / \                / \               / \
        .   ?             NIL  r             .
       .                      / \           .
      .                      .   ?         .
     y                      .             x
    / \                    .             
  NIL  x                  x

The main thing to fix: We've broken the black height property by 1. We need to fix it. Identify cases and do clever rotations and recolorings.