Transcript PPT
Red-Black Trees
Bottom-Up Deletion
Recall “ordinary” BST Delete
1. If node to be deleted is a leaf, just delete it.
2. If node to be deleted has just one child,
replace it with that child (splice)
3. If node to be deleted has two children,
replace the value in the node by its in-order
predecessor/successor’s value then delete the
in-order predecessor/successor
(a recursive step)
3/20/2006
2
Bottom-Up Deletion
1. Do ordinary BST deletion. Eventually a
“case 1” or “case 2” deletion will be done
(leaf or just one child).
-- If deleted node, U, is a leaf, think of
deletion as replacing U with the NULL
pointer, V.
-- If U had one child, V, think of deletion
as replacing U with V.
2. What can go wrong??
3/20/2006
3
Which RB Property may be
violated after deletion?
1. If U is Red?
Not a problem – no RB properties violated
2. If U is Black?
If U is not the root, deleting it will change
the black-height along some path
3/20/2006
4
Fixing the problem
• Think of V as having an “extra” unit of
blackness. This extra blackness must be
absorbed into the tree (by a red node), or
propagated up to the root and out of the
tree.
• There are four cases – our examples and
“rules” assume that V is a left child. There
are symmetric cases for V as a right child.
3/20/2006
5
Terminology
• The node just deleted was U
• The node that replaces it is V, which has an
extra unit of blackness
• The parent of V is P
• The sibling of V is S
Black Node
Red or Black and don’t care
Red Node
3/20/2006
6
Bottom-Up Deletion
Case 1
• V’s sibling, S, is Red
– Rotate S around P and recolor S & P
• NOT a terminal case – One of the other
cases will now apply
• All other cases apply when S is Black
3/20/2006
7
Case 1 Diagram
P
V+
S
Rotate S around P
P
S
V+
S
Recolor S & P
P
3/20/2006
V+
8
Bottom-Up Deletion
Case 2
• V’s sibling, S, is Black and has two Black
children.
– Recolor S to be Red
– P absorbs V’s extra blackness
• If P is Red, we’re done (it absorbed the blackness)
• If P is Black, it now has extra blackness and
problem has been propagated up the tree
3/20/2006
9
Case 2 diagram
Recolor S
P absorbs blackness
P
S
V+
V
P+
S
Either extra Black absorbed by P
or
P now has extra blackness
3/20/2006
10
Bottom-Up Deletion
Case 3
• S is Black
• S’s right child is RED (Left child either color)
– Rotate S around P
– Swap colors of S and P,
and color S’s right child Black
• This is the terminal case – we’re done
3/20/2006
11
Case 3 diagrams
P
Rotate S around P
P
S
V+
S
V+
S
P
3/20/2006
V
Swap colors of S & P
Color S’s right child Black
12
Bottom-Up Deletion
Case 4
• S is Black, S’s right child is Black and S’s
left child is Red
– Rotate S’s left child around S
– Swap color of S and S’s left child
– Now in case 3
3/20/2006
13
Case 4 Diagrams
P
V+
P
S
V+
P
Rotate S’s
left around S
S
V+
S
3/20/2006
Swap colors of S
and S’s original
left child
14
Top-Down Deletion
An alternative to the recursive “bottom-up”
deletion is “top-down” deletion.
This method is iterative. It moves down the
tree only, “fixing” things as it goes.
What is the goal of top-down deletion?
3/20/2006
15
65
50
10
80
70
60
90
62
Perform the following deletions, in the order specified
Delete 90, Delete 80, Delete 70
3/20/2006
16