Red Black Deletion

download Red Black Deletion

of 20

Transcript of Red Black Deletion

  • 8/7/2019 Red Black Deletion

    1/20

    CS 21: Red Black Tree Deletion February 25, 1998

    erm 12.235

    E

    R

    S X

    C

    DB

    Deletion from Red-Black

    Trees

    R

    UO

  • 8/7/2019 Red Black Deletion

    2/20

    CS 21: Red Black Tree Deletion February 25, 1998

    erm 12.236

    Setting Up Deletion

    As with binary search trees, we can alwaysdelete a node that has at least one external child

    If the key to be deleted is stored at a node thathas no external children, we move there the keyof its inorder predecessor (or successor), anddelete that node instead

    Example: to delete key 7, we move key 5 tonode u, and delete node v

    7

    4 8

    2 5 9

    5

    4 8

    2 9

    u

    v

    u

  • 8/7/2019 Red Black Deletion

    3/20

    CS 21: Red Black Tree Deletion February 25, 1998

    erm 12.237

    1. Remove v with a removeAboveExternal op-eration

    2. If v was red, color u black. Else, color udouble black.

    3. While a double black edge exists, performone of the following actions ...

    v

    v

    uu

    u u

    Deletion Algorithm

  • 8/7/2019 Red Black Deletion

    4/20

    CS 21: Red Black Tree Deletion February 25, 1998

    erm 12.238

    How to Eliminate the

    Double Black Edge

    The intuitive idea is to perform a colorcompensation

    Find a red edge nearby, and change thepair ( red, double black ) into

    ( black , black )

    As for insertion, we have two cases:

    restructuring, and

    recoloring (demotion, inverse of

    promotion)

    Restructuring resolves the problem lo-cally, while recoloring may propagate ittwo levels up

    Slightly more complicated than inser-

    tion, since two restructurings may occur(instead of just one)

  • 8/7/2019 Red Black Deletion

    5/20

    CS 21: Red Black Tree Deletion February 25, 1998

    erm 12.239

    If sibling is black and one of its children isred, perform a restructuring

    Case 1: black sibling with a red

    child

    vp

    s

    v

    p

    s

    v

    p

    s

    zv

    z

    p s

    z

    z

  • 8/7/2019 Red Black Deletion

    6/20

    CS 21: Red Black Tree Deletion February 25, 1998

    erm 12.240

    (2,4) Tree Interpretation

    30

    20

    x

    y

    10

    40

    r

    z2010

    40

    ... 30 ...

    ... ...

    30

    20

    c

    b

    10

    40

    r

    a

    40

    ... 20 ...

    ... ...

    3010

  • 8/7/2019 Red Black Deletion

    7/20

    CS 21: Red Black Tree Deletion February 25, 1998

    erm 12.241

    Case 2: black sibling with

    black childern

    If sibling and its children are black, per-form a recoloring

    If parent becomes double black, continueupward

    v s

    p

    v

    p

    s

    v

    p

    s v

    p

    s

  • 8/7/2019 Red Black Deletion

    8/20

    CS 21: Red Black Tree Deletion February 25, 1998

    erm 12.242

    (2,4) Tree Interpretation

    30

    20

    x

    y40

    r

    40

    10 30 ...

    20

    10

    ...

    30

    20

    x

    y40

    r

    40

    10 ...

    20

    10

    ...

    30

  • 8/7/2019 Red Black Deletion

    9/20

    CS 21: Red Black Tree Deletion February 25, 1998

    erm 12.243

    Case 3: red sibling

    If sibling is red, perform an adjustment

    Now the sibling is black and one the of pre-vious cases applies

    If the next case is recoloring, there is nopropagation upward (parent is now red)

    v

    p

    s

    v

    p

    s

  • 8/7/2019 Red Black Deletion

    10/20

    CS 21: Red Black Tree Deletion February 25, 1998

    erm 12.244

    How About an Example?

    6

    4 8

    2 5 9

    Remove 9

    6

    4 8

    2 5 7

    7

  • 8/7/2019 Red Black Deletion

    11/20

    CS 21: Red Black Tree Deletion February 25, 1998

    erm 12.245

    6

    4 8

    2 5

    What do we know?

    Sibling is black with blackchildren

    What do we do? Recoloring

    7

    6

    4 8

    2 5 7

    Example

  • 8/7/2019 Red Black Deletion

    12/20

    CS 21: Red Black Tree Deletion February 25, 1998

    erm 12.246

    Delete 8

    no double black

    6

    4 8

    2 5 7

    Example

    6

    4 7

    2 5

  • 8/7/2019 Red Black Deletion

    13/20

    CS 21: Red Black Tree Deletion February 25, 1998

    erm 12.247

    Delete 7

    Restructuring

    Example

    4

    2 5

    6

    4 7

    2 5

    6

    4

    62

    5

  • 8/7/2019 Red Black Deletion

    14/20

    CS 21: Red Black Tree Deletion February 25, 1998

    erm 12.248

    Example

    4

    7

    5

    14

    12

    16

    15 18

    17

    4

    7

    5

    14

    16

    15 18

    17

  • 8/7/2019 Red Black Deletion

    15/20

    CS 21: Red Black Tree Deletion February 25, 1998

    erm 12.249

    Example

    4

    7

    5

    14

    16

    15 18

    17

    5

    14

    16

    15 18

    17

    74

  • 8/7/2019 Red Black Deletion

    16/20

    CS 21: Red Black Tree Deletion February 25, 1998

    erm 12.250

    Time Complexity of Deletion

    Take a guess at the time complexity of deletionin a red-black tree . . .

  • 8/7/2019 Red Black Deletion

    17/20

    CS 21: Red Black Tree Deletion February 25, 1998

    erm 12.251

    What else could it be?!

    O(logN)

  • 8/7/2019 Red Black Deletion

    18/20

    CS 21: Red Black Tree Deletion February 25, 1998

    erm 12.252

    Colors and Weights

    Color Weight

    red

    black

    double black

    0

    1

    2

    Every root-to-leaf path has the same weight

    There are no two consecutive red edges

    Therefore, the length of any root-to-leafpath is at most twice the weight

  • 8/7/2019 Red Black Deletion

    19/20

    CS 21: Red Black Tree Deletion February 25, 1998

    erm 12.253

    Bottom-Up Rebalancing

    ofRed-Black Trees An insertion or deletion may cause a local

    perturbation (twoconsecutive red edges, ora double-black edge)

    The perturbation is either resolved locally (restructuring), or

    propagatedto a higher level in the treeby recoloring (promotion or demotion)

    O(1) time for a restructuring or recoloring

    At most one restructuring per insertion, andat most two restructurings per deletion

    O(log N) recolorings

    Total time: O(log N)

  • 8/7/2019 Red Black Deletion

    20/20

    CS 21: Red Black Tree Deletion February 25, 1998

    Red-Black Trees

    Search O(log N)

    Insert O(log N)Delete O(log N)

    Operation Time