Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Red Black Trees - II

Instructor: Ashok Singh Sairam


Lecture Plan
• Review of Red-Black Trees
• Deletion

MA512: Data Structures and Algorithms


2
Exercise

MA512: Data Structures and Algorithms


3
RB Properties Affected by Delete
DELETE: what color was the node that was removed? Black?
1. Every node is either red or black OK!
2. The root is black Not OK! If removing the root and
the child that replaces it is red
3. Every leaf (NIL) is black OK!
4. If a node is red, then both its children are black
Not OK! Could change the Not OK! Could create
black heights of some nodes two red nodes in a row
5. For each node, all paths from the node to descendant
leaves contain the same number of black nodes

MA512: Data Structures and Algorithms


4
Deletion
• Deleting node from a red-black tree is a bit more
complicated than inserting a node
• Deleted node y is red
 Not a problem – no RB properties violated
• Deleted node y is black
 deleting it will change the black-height along some path
 When we delete y, we push y’s blackness to x. Then x is
either doubly black or red-black (property 1 violated)
 RB-DELETE-FIXUP() used to remove the extra blackness of x

MA512: Data Structures and Algorithms


5
Algorithm: Deletion
• Delete as in a general BST
• To fix the RB Tree, pass the
node x to RB-DELETE-FIXUP
• z(or y) is the node deleted
• What is node x?
 If z has single child, x

points to that child


 If z has two child, y
replaces z, x is either the
left/right child or T.nil

MA512: Data Structures and Algorithms 6


Example: Deletion
• Delete 50 26
17 41

12 30 47 y
38 50
Deleted node y is red 26
Not a problem!
17 41

12 30 47
38
MA512: Data Structures and Algorithms
7
Deleting Black Node
• Node x: Color of deleted node y is transferred to x
• Examine w, sibling of x
• Case 1: w is red
 Convert to case 2/3/4
• Case 2: w is black and both of w’s children is black
 Move the violation up
• Case 3: w is black, w.left is red and w.right is black
 Convert to case 4
• Case 4: w is black, w.left is black and w.right is red
MA512: Data Structures and Algorithms
8
Deletion: Case 1
• Color of deleted node y transferred to x;Node w sibling of x
• Case 1: w is red (w must have black children)
 Switch colors of w and x.p
 Perform left rotation on x.p
 New sibling of x is black => converted to case 2/3/4

MA512: Data Structures and Algorithms


9
Ex: Deletion Case 1 26

• Delete 17 y 17 41

30 47

26
w
41 41
x Nil 26
30 47 x w 47
Nil
30

The new sibling of x, which is


one of w’s children is black
[Case2/3/4]
MA512: Data Structures and Algorithms
10
Deletion: Case 2
• Color of deleted node y transferred to x;Node w sibling of x
• Case 2: w is black; both of w’s children is black
 Remove one black color from x and w. [w will become
red]
 Add an extra black to x.p; becomes new x

MA512: Data Structures and Algorithms


11
Ex: Deletion Case2
• w is black, both children black

41 41
x
26 26
x 47 w 47
w
17 17
30 30 48 49
48 49

29 38 29 38

MA512: Data Structures and Algorithms


12
Deletion: Case 3
• Color of deleted node y transferred to x;Node w sibling of x
• Case 3: w is black; w.left is red and w.right is black
 Switch the colors of w and w.left
 Perform right rotation on w
 Transformed into case 4

MA512: Data Structures and Algorithms


13
41
26
Ex: Deletetion Case 3 x w 47
17
• w.left is red 30 48 49
 w.left ↔w; RR(w) 27 38
29

41
Converted case 3 to 26
case 4; w.right is red x w 47
17
27 48 49

30
29 38

MA512: Data Structures and Algorithms


14
Deletion: Case 4
• Color of deleted node y transferred to x;Node w sibling of x
• Case 4: w is black; w.left is black and w.right is red
 Make some color changes
 Perform a left rotation on x.p

MA512: Data Structures and Algorithms


15
41
Ex: Deletetion Case 4x 26
47
w
• w.right is red 17
27 48 49
 Perform some recoloring
• w.color =x.p.color 30
• x.p.color=black 29 38
• w.right.color=black
 LR(x.p) 41

27 47

x 26 30 48 49
17 29 38

MA512: Data Structures and Algorithms


16
Example: Deletion
• Now show the red-black trees that result from the successive
deletion of the keys in the order 8,12,19,31,38,41
• DELETE 8

MA512: Data Structures and Algorithms


17
Example: Deletion
• DELETE 12

T.nil

MA512: Data Structures and Algorithms


18
• Delete 19

MA512: Data Structures and Algorithms


19
• Delete 31

MA512: Data Structures and Algorithms


20
Exercise
• Show the red-black trees that result from the
successive deletion of the keys in the order 25,
50, 75, 99.

MA512: Data Structures and Algorithms


21

You might also like