Deletion from Red-Black Trees E R R C U D O B S X erm - - PDF document

deletion from red black trees
SMART_READER_LITE
LIVE PREVIEW

Deletion from Red-Black Trees E R R C U D O B S X erm - - PDF document

CS 21: Red Black Tree Deletion February 25, 1998 Deletion from Red-Black Trees E R R C U D O B S X erm 12.235 CS 21: Red Black Tree Deletion February 25, 1998 Setting Up Deletion As with binary search trees, we can always delete


slide-1
SLIDE 1 CS 21: Red Black Tree Deletion February 25, 1998 erm 12.235

E R S X C D B

Deletion from Red-Black Trees

R U O

slide-2
SLIDE 2 CS 21: Red Black Tree Deletion February 25, 1998 erm 12.236

Setting Up Deletion

As with binary search trees, we can always delete a node that has at least one external child If the key to be deleted is stored at a node that has no external children, we move there the key

  • f its inorder predecessor (or successor), and

delete that node instead Example: to delete key 7, we move key 5 to node u, and delete node v 7 4 8 2 5 9 5 4 8 2 9 u v u

slide-3
SLIDE 3 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 u

double black.

  • 3. While a double black edge exists, perform
  • ne of the following actions ...

v v u u u u Deletion Algorithm

slide-4
SLIDE 4 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 “color

compensation’’

  • Find a red edge nearby, and change the

pair ( 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 it two levels up

  • Slightly more complicated than inser-

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

slide-5
SLIDE 5 CS 21: Red Black Tree Deletion February 25, 1998 erm 12.239
  • If sibling is black and one of its children is

red, perform a restructuring

Case 1: black sibling with a red child v p s v p s v p s z v z p s z z

slide-6
SLIDE 6 CS 21: Red Black Tree Deletion February 25, 1998 erm 12.240

(2,4) Tree Interpretation

30 20 x y 10 40 r z 20 10 40 ... 30 ... ... ... 30 20 c b 10 40 r a 40 ... 20 ... ... ... 30 10
slide-7
SLIDE 7 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, continue

upward

v s p v p s v p s v p s

slide-8
SLIDE 8 CS 21: Red Black Tree Deletion February 25, 1998 erm 12.242

(2,4) Tree Interpretation

30 20 x y 40 r 40 10 30 ... 20 10 ... 30 20 x y 40 r 40 10 ... 20 10 ... 30
slide-9
SLIDE 9 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 no

propagation upward (parent is now red)

v p s v p s

slide-10
SLIDE 10 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

slide-11
SLIDE 11 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 black

children What do we do?

  • Recoloring

7 6 4 8 2 5 7

Example

slide-12
SLIDE 12 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

slide-13
SLIDE 13 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 6 2 5

slide-14
SLIDE 14 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
slide-15
SLIDE 15 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 7 4
slide-16
SLIDE 16 CS 21: Red Black Tree Deletion February 25, 1998 erm 12.250

Time Complexity of Deletion

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

slide-17
SLIDE 17 CS 21: Red Black Tree Deletion February 25, 1998 erm 12.251

What else could it be?!

O(logN)

slide-18
SLIDE 18 CS 21: Red Black Tree Deletion February 25, 1998 erm 12.252

Colors and Weights Color Weight red black double black 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-leaf

path is at most twice the weight

slide-19
SLIDE 19 CS 21: Red Black Tree Deletion February 25, 1998 erm 12.253

Bottom-Up Rebalancing

  • f Red-Black Trees
  • An insertion or deletion may cause a local

perturbation (two consecutive red edges, or a double-black edge)

  • The perturbation is either
  • resolved locally (restructuring), or
  • propagated to a higher level in the tree

by recoloring (promotion or demotion)

  • O(1) time for a restructuring or recoloring
  • At most one restructuring per insertion, and

at most two restructurings per deletion

  • O(log N) recolorings
  • Total time: O(log N)
slide-20
SLIDE 20 CS 21: Red Black Tree Deletion February 25, 1998 erm 12.254

Red-Black Trees

Search O(log N) Insert O(log N) Delete O(log N) Operation Time