Professional Documents
Culture Documents
L22 RedBlackTrees II
L22 RedBlackTrees II
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
• Delete 17 y 17 41
30 47
26
w
41 41
x Nil 26
30 47 x w 47
Nil
30
41 41
x
26 26
x 47 w 47
w
17 17
30 30 48 49
48 49
29 38 29 38
41
Converted case 3 to 26
case 4; w.right is red x w 47
17
27 48 49
30
29 38
27 47
x 26 30 48 49
17 29 38
T.nil