8/11/2019 Lecture-17-CS210-2012 (1).pptx
1/51
Data Structures and Algorithms
(CS210/ESO207/ESO211)
Lecture 17
Red-Black trees Handling deletion of nodes
1
8/11/2019 Lecture-17-CS210-2012 (1).pptx
2/51
RedBlackTree
Red-Black tree is a fullbinary search tree with each leaf as a nullnode and
satisfying the following properties at each stage.
Each node is colored redor black.
Each leaf is colored black and so is the root.
Every red node will have both its children black.
The number of blacknodes on a path from root to a leaf node is same for
every leaf. This parameter is called blackheight of the tree.
2
8/11/2019 Lecture-17-CS210-2012 (1).pptx
3/51
A red-blacktree
3
root
2
28
46
67
25
5
31 41
35 49
8/11/2019 Lecture-17-CS210-2012 (1).pptx
4/51
Handling Insertion in a Red BlackTree
4
Aquick recap from the previous
lecture
8/11/2019 Lecture-17-CS210-2012 (1).pptx
5/51
Rotation around a nodeAn important tool for balancing trees
5
v
u
xRight rotation Left rotation
v
u
x
p
Note that the tree T continues to
remain a BST even after rotation
around any node.
8/11/2019 Lecture-17-CS210-2012 (1).pptx
6/51
Insertion in a red-black tree
6
T
2
28
46
67
25
5
31 41
35 49 83
54
Insert(T,44) :Inserting 44into T.
44
Color imbalance
8/11/2019 Lecture-17-CS210-2012 (1).pptx
7/51
Key pointsabout Insertion in a Red-Blacktree
Black Height of the tree is kept intactalways.
An insertion can potentially lead to color imbalance.
A color imbalance is handled using one or more of the following
tools:
1. Swappingof colors
2. Shiftingthe imbalance upward3. Rotationaround a node
7
8/11/2019 Lecture-17-CS210-2012 (1).pptx
8/51
Handling Deletion in a Red BlackTree
8
8/11/2019 Lecture-17-CS210-2012 (1).pptx
9/51
Notations to be used
9
a blacknode
a rednode
a node whose color is either redor black
a BSTCould potentially be
8/11/2019 Lecture-17-CS210-2012 (1).pptx
10/51
How to maintain a BST under deletion of
nodes ?
10
8/11/2019 Lecture-17-CS210-2012 (1).pptx
11/51
Deletion in a BST isslightly harder than Insertion
(even ignoring the height balance factor)
11
T
2
28
46
67
25
5
31 41
35 49 83
54
44
How to delete 28 ?
8/11/2019 Lecture-17-CS210-2012 (1).pptx
12/51
Is deletion of a node easier for some cases ?
12
T
2
28
46
67
25
5
31 41
35 49 83
54
44
Deletion of 31 is easy.
Can you see ?Deletion of 49 is also
easy.
8/11/2019 Lecture-17-CS210-2012 (1).pptx
13/51
An insight
It is easier to maintain a BST under deletion if the node to be deleted has at
most one child which is non-leaf.
13
p
q
8/11/2019 Lecture-17-CS210-2012 (1).pptx
14/51
An insight
It is easier to maintain a BST under deletion if the node to be deleted has at
most one child which is non-leaf.
14
q
p
8/11/2019 Lecture-17-CS210-2012 (1).pptx
15/51
An insight
It is easier to maintain a BST under deletion if the node to be deleted has at
most one child which is non-leaf.
15
q
8/11/2019 Lecture-17-CS210-2012 (1).pptx
16/51
An important question
It is easier to maintain a BST under deletion if the node to be deleted has at
most one child which is non-leaf.
Question: Can we transform every other case to the above case ?
Answer: ??
16
p
non-leafnon-leaf
8/11/2019 Lecture-17-CS210-2012 (1).pptx
17/51
How to delete a node whose both children are non-leaves?
17
T
2
28
46
67
25
5
31 41
35 49 83
54
44
How to delete 28 ?
Swap28 and25
and then delete 28
How to delete 46 ?
Swap46 and44
and then delete 46
Can you figure
out the strategy
now ?
8/11/2019 Lecture-17-CS210-2012 (1).pptx
18/51
An important observation
It is easier to maintain a BST under deletion if the node to be deleted has at
most one child which is non-leaf.
Question: Can we transform every other case to the above case ?
Answer: by swapping value(p) with its predecessor.
18
p
non-leafnon-leaf
8/11/2019 Lecture-17-CS210-2012 (1).pptx
19/51
We need to handle deletion only for the
following case
19
p
q
8/11/2019 Lecture-17-CS210-2012 (1).pptx
20/51
How to maintain a red-blacktree under
deletion ?
20
We shall first perform deletion like in a BST and
then restore all properties of red-black tree.
8/11/2019 Lecture-17-CS210-2012 (1).pptx
21/51
Easy cases and difficult case
21
p
q
Easy case Difficult case
p
q
p
q
Easy case:Change color of qto
black
8/11/2019 Lecture-17-CS210-2012 (1).pptx
22/51
Handling the difficult case
22
p
q
s
8/11/2019 Lecture-17-CS210-2012 (1).pptx
23/51
Handling the difficult case
23
q
sp
8/11/2019 Lecture-17-CS210-2012 (1).pptx
24/51
Handling the difficult case
24
q
s
8/11/2019 Lecture-17-CS210-2012 (1).pptx
25/51
Handling the difficult case
25
q s
r
As some students had noticed after the class that the subtree rooted at q
will actually be just a leaf node in the beginning. But we are not showing
it explicitly here. This is because we are depicting the most general case.
During the algorithm, we might shift the height imbalance upwards and
in that case the subtree rooted at q might not be a leaf node.
Notice that the number of
black nodes to each leaf
node in subtree(q) has
become oneless than any
other leaf node. We need
an algorithm to remove this
black-height imbalance.
8/11/2019 Lecture-17-CS210-2012 (1).pptx
26/51
Handling the difficult case: An overview
26
sisred sisblackreduction
one child of sis redBoth children of sare black
right(s)isredleft(s)isred and
right(s) isblackreduction
Eventually we need
to handle these two
cases only
Color ofs ?
8/11/2019 Lecture-17-CS210-2012 (1).pptx
27/51
Reduction of
case s is red to case s is black
27
8/11/2019 Lecture-17-CS210-2012 (1).pptx
28/51
Reduction of case s is red to case s is black
28
q s
21
r
8/11/2019 Lecture-17-CS210-2012 (1).pptx
29/51
Reduction of case s is red to case s is black
29
q s
21
r
Left rotation
r
q
2
1
s
The new sibling of q is now a black
node. But the number of black nodes
to leaves of tree 2 have reduced by
one. What to do ?
8/11/2019 Lecture-17-CS210-2012 (1).pptx
30/51
Reduction of case s is red to case s is black
30
q s
21
r
Left rotation
r
q
2
1
s
Convince yourself that the number of
black nodes to any leaf of subtree(q)
or subtrees 1 and 2 is now the same
as before the rotation. And now the
sibling of q is black. So we are done.
8/11/2019 Lecture-17-CS210-2012 (1).pptx
31/51
We just need to handle the case
s is black
31
8/11/2019 Lecture-17-CS210-2012 (1).pptx
32/51
Handling the case when sis black
Case 1:both children of s are black
Case 2:at least one child of s is red
32
q s
r
8/11/2019 Lecture-17-CS210-2012 (1).pptx
33/51
Handling the case:
s is black and both children of s are black
33
8/11/2019 Lecture-17-CS210-2012 (1).pptx
34/51
Handling the case:
s is black and both children of s are black
34
What if we swap colors of
s and r
q s
21
r
When r is red
q s
21
r
When r is black
8/11/2019 Lecture-17-CS210-2012 (1).pptx
35/51
Handling the case:
s is black and both children of s are black
35
q s
21
r
q s
21
r
What if we change color
of s to red
When r is red When r is black
Swapping the colors leaves the number
of black nodes to the leaves of trees 1
and 2 unchanged. Interestingly, the
deficiency of one black node on the
path to the leaves of subtree rooted at
q is also compensated. So we may stop.
8/11/2019 Lecture-17-CS210-2012 (1).pptx
36/51
Handling the case:
s is black and both children of s are black
36
q s
r
q s
r
When r is red When r is black
Changing color of sto redhas reduced
the number of black nodes on the path
to the root of subtree(q) by one. As a
result the imbalance of black height
haspropagatedto r. So we process
vertex r in a similar fashion.
Swapping the colors leaves the number
of black nodes to the leaves of trees 1
and 2 unchanged. Interestingly, the
deficiency of one black node on the
path to the leaves of subtree rooted at
q is also compensated. So we may stop.
8/11/2019 Lecture-17-CS210-2012 (1).pptx
37/51
Handling the case:
s is black and one of its children is red
37
8/11/2019 Lecture-17-CS210-2012 (1).pptx
38/51
There are two cases
38
q s
r
When right(s) is red reduction
q s
r
When left(s) is red andright(s) isblack
8/11/2019 Lecture-17-CS210-2012 (1).pptx
39/51
Handling the case: right(s) is red
39
8/11/2019 Lecture-17-CS210-2012 (1).pptx
40/51
Handling the case: right(s) is red
40
q s
21
r
Let color(r) be c
8/11/2019 Lecture-17-CS210-2012 (1).pptx
41/51
Handling the case: right(s) is red
41
q s
21
r
Left rotation
q
r
2
1
s
The number of black nodes on the path from root to
any leaf node of subtree(q) has increased by one
(this is good!), has remained unchanged for leaves of
tree 1, and is uncertain for leaves of tree 2(depends
upon c). How to get rid of this uncertainty ?
8/11/2019 Lecture-17-CS210-2012 (1).pptx
42/51
Handling the case: right(s) is red
42
q s
21
r
Left rotation
q
r
2
1
s
The number of black nodes on the path from
root to any leaf node 2 is now less by one
node. What to do ? (Hint: root of tree 2 is red)
Change color of root
of tree 2 to black and
we are done.
8/11/2019 Lecture-17-CS210-2012 (1).pptx
43/51
Handling the case: right(s) is red
43
q s
21
r
Left rotation
q
r
2
1
s
Convince yourself that left rotation at r,
followed by color swap of s and r, followed by
change of color of root of tree r removes the
imbalance of black height for all leaf nodes ofthe subtrees shown.
8/11/2019 Lecture-17-CS210-2012 (1).pptx
44/51
Handling the caseleft(s) is redandright(s) is black
44
8/11/2019 Lecture-17-CS210-2012 (1).pptx
45/51
Handling the case:
left(s) is red and right(s) is black
45
q s
r
8/11/2019 Lecture-17-CS210-2012 (1).pptx
46/51
Handling the case:
left(s) is red and right(s) is black
46
q s
3
1
r
2
8/11/2019 Lecture-17-CS210-2012 (1).pptx
47/51
Handling the case:
left(s) is red and right(s) is black
47
q s
3
1
r
2
Right rotation
q
s
3
1
r
2
The number of black nodes on the path from
root to any leaf node in tree 1 has now reduced
by one athough it is the same for trees 2 and 3.
What should we do ?
8/11/2019 Lecture-17-CS210-2012 (1).pptx
48/51
Handling the case:
left(s) is red and right(s) is black
48
q s
3
1
r
2
Right rotation
q
s
3
1
r
2
Notice that now the new sibling of qhas its right
child red. So we have effectively reduced the
current case to the case which we know how to
handle.
8/11/2019 Lecture-17-CS210-2012 (1).pptx
49/51
Theorem: We can maintain red-black trees under deletion of
nodes in O(log n) time per insert/search operation where nis the
number of the nodes in the tree.
49
8/11/2019 Lecture-17-CS210-2012 (1).pptx
50/51
Key pointsabout Deletion in a Red-Blacktree
3rdproperty of the red-black tree (no two consecutive red
nodes) is kept intactalways.
An insertion can potential lead to an imbalance of black
height(some leaf nodes having less number of black nodes on the pathfrom the root).
An imbalance of black height is handled using one or more of
the following tools:1. Swappingof colors
2. Shiftingthe imbalance upward
3. Rotationaround a node
50
8/11/2019 Lecture-17-CS210-2012 (1).pptx
51/51
Points to ponder
As the nodes are being deleted, the black nodes will reduce in numbersand so will the black height. Among various cases we discussed for
handling deletion of nodes, figure out the case(s) which can potentially
lead to a reduction in the black height of the tree.
What is the worst case number of rotation operations during a singledeletion in a red black tree ?
What is the worst case number of rotation operations during a single
insertion in a red black tree ?