Data Structures and Algorithms: (CS210/ESO207/ESO211)
Data Structures and Algorithms: (CS210/ESO207/ESO211)
(CS210/ESO207/ESO211)
Lecture 17
Red-Black trees
Handling deletion of nodes
1
Red Black Tree
Red-Black tree is a full binary search tree with each leaf as a null node and
satisfying the following properties at each stage.
Each node is colored red or black.
Each leaf is colored black and so is the root.
Every red node will have both its children black.
The number of black nodes on a path from root to a leaf node is same for
every leaf. This parameter is called black height of the tree.
2
A red-black tree
3
root
2
28
46
67
25
5
31 41
35
49
Handling Insertion in a Red Black Tree
4
A quick recap from the previous
lecture
Rotation around a node
An important tool for balancing trees
5
v
x
Right rotation
Left rotation
v
u
p
Note that the tree T continues to
remain a BST even after rotation
around any node.
Insertion in a red-black tree
6
T
2
28
46
67
25
5
31 41
35
49
83
54
Insert(T,44) :
Inserting 44 into T.
44
Color imbalance
Key points
about Insertion in a Red-Black tree
Black Height of the tree is kept intact always.
An insertion can potentially lead to color imbalance.
A color imbalance is handled using one or more of the following
tools:
1. Swapping of colors
2. Shifting the imbalance upward
3. Rotation around a node
7
Handling Deletion in a Red Black Tree
8
Notations to be used
9
a black node
a red node
a node whose color is either red or black
a BST
Could potentially be
How to maintain a BST under deletion of
nodes ?
10
Deletion in a BST is slightly 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 ?
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.
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
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
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
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-leaf non-leaf
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 ?
Swap 28 and 25
and then delete 28
How to delete 46 ?
Swap 46 and 44
and then delete 46
Can you figure
out the strategy
now ?
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-leaf non-leaf
We need to handle deletion only for the
following case
19
p
q
How to maintain a red-black tree under
deletion ?
20
We shall first perform deletion like in a BST and
then restore all properties of red-black tree.
Easy cases and difficult case
21
p
q
Easy case Difficult case
p
q
p
q
Easy case:
Change color of q to
black
Handling the difficult case
22
p
q
s
Handling the difficult case
23
q
s p
Handling the difficult case
24
q
s
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 one less than any
other leaf node. We need
an algorithm to remove this
black-height imbalance.
Handling the difficult case: An overview
26
s is red s is black reduction
one child of s is red Both children of s are black
right(s) is red
left(s) is red and
right(s) is black
reduction
Eventually we need
to handle these two
cases only
Color of s ?
Reduction of
case s is red to case s is black
27
Reduction of case s is red to case s is black
28
q s
2
1
r
Reduction of case s is red to case s is black
29
q s
2
1
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 ?
Reduction of case s is red to case s is black
30
q s
2
1
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.
We just need to handle the case
s is black
31
Handling the case when s is black
Case 1: both children of s are black
Case 2: at least one child of s is red
32
q s
r
Handling the case:
s is black and both children of s are black
33
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
2
1
r
When r is red
q s
2
1
r
When r is black
Handling the case:
s is black and both children of s are black
35
q s
2
1
r
q s
2
1
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.
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 s to red has 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
has propagated to 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.
Handling the case:
s is black and one of its children is red
37
There are two cases
38
q s
r
When right(s) is red
reduction
q s
r
When left(s) is red and right(s) is black
Handling the case: right(s) is red
39
Handling the case: right(s) is red
40
q s
2
1
r
Let color(r) be c
Handling the case: right(s) is red
41
q s
2
1
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 ?
Handling the case: right(s) is red
42
q s
2
1
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.
Handling the case: right(s) is red
43
q s
2
1
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 of
the subtrees shown.
Handling the case
left(s) is red and right(s) is black
44
Handling the case:
left(s) is red and right(s) is black
45
q s
r
Handling the case:
left(s) is red and right(s) is black
46
q s
3
1
r
2
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 ?
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 q has its right
child red. So we have effectively reduced the
current case to the case which we know how to
handle.
Theorem: We can maintain red-black trees under deletion of
nodes in O(log n) time per insert/search operation where n is the
number of the nodes in the tree.
49
Key points
about Deletion in a Red-Black tree
3
rd
property of the red-black tree (no two consecutive red
nodes) is kept intact always.
An insertion can potential lead to an imbalance of black
height (some leaf nodes having less number of black nodes on the path
from the root).
An imbalance of black height is handled using one or more of
the following tools:
1. Swapping of colors
2. Shifting the imbalance upward
3. Rotation around a node
50
Points to ponder
As the nodes are being deleted, the black nodes will reduce in numbers
and 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 single
deletion in a red black tree ?
What is the worst case number of rotation operations during a single
insertion in a red black tree ?
51