Lec 09 RB
Lec 09 RB
IITB India
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 1
Topic 9.1
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 2
Maintain balance
We call BST imbalanced when the difference between the left and right subtree height is large.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 3
Balancing height by rotation
max(h1 , h2 + 1, h3 + 1) + 1 max(h1 + 1, h2 + 1, h3 ) + 1
A (A, B)-rotate B
h1 B A h3
T1 T3
h2 h3 h1 h2
T2 T3 T1 T2
If h3 > h2 = h1 , if we rotate the BST, we will get a valid more balanced BST with less height.
Note that B could have been the left child. For this situation, we can define symmetric rotation.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 4
Example: rotation
Example 9.1
In the following BST, we can rotate 8-17 edge.
8 17
(8, 17) rotate
5 17 8 21
11 21 5 11 18 30
18 30
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 5
When to rotate? Can rotation only fix imbalance?
We need a definition of balance such that rotations operations should be able to achieve the
definition.
Design principle:
We minimize the number of rotations while allowing some imbalance.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 6
Topic 9.2
Red-black tree
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 7
Null leaves
To describe a red-black tree, we replace the null pointer with absent children by dummy null nodes.
Example 9.2
The following tiny nodes are the dummy null nodes.
5 17
21
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 8
Red-black tree
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 9
Exercise: Identify red-black trees
Exercise 9.1
Which of the following are red-black trees?
17 17
5 5 17
16 16 5 17
Observations:
▶ Red nodes are not counted in the imbalance. We need them only when there is an imbalance.
▶ There cannot be too many red nodes. (Why?)
▶ Red nodes can be at every level except the root.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 10
Black height
Definition 9.2
The black height (bh) for each node is defined as follows.
0
n is a null leaf
bh(n) = max(bh(right(n)), bh(left(n))) + 1 n is a black node
max(bh(right(n)), bh(left(n))) n is a red node
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 11
Example: black height
Example 9.4
The black height of the following red-black tree is 2.
Exercise 9.2
Can we change the color of some nodes without breaking the conditions of a red-black tree?
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 12
Bound on the height of a red-black tree
▶ n is the smallest when all nodes are black. Therefore, the tree is a complete binary tree.
Therefore, n = 2h − 1.
▶ n is largest when the alternate levels of the tree are red. The height of the tree is 2h.
Therefore, n = 22h − 1.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 13
Search, Maximum, and Successor/Predecessor
We can run search, maximum, and successor/predecessor on the red-black tree as usual.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 14
Topic 9.3
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 15
BST insertion in red-black tree
1. Follow the usual procedure of insertion in the BST, which inserts the new node n as a leaf.
▶ Note that there are dummy nodes in the red-black tree. n is inserted as the parent of a dummy
node.
2. We color n red.
Commentary: We have null nodes in this setting. We need to add nulls as children to the new node.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 16
Example: insert in red-black tree
Example 9.5
Inserting 20 in the following tree.
8 8
Insert 20
5 17 5 17
21 21
20
The insertion results in violation of the conditions of red-black tree that red nodes can only have
black children.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 17
Red-red violation
After insertion, we may have a red-red violation, where a red node has a red child. Orange color
means that we need to consider all possible colors of the nodes.
h u p h+1 u p
T1 T1
n h n h
T4 T4
h h h h
T2 T3 T2 T3
In the subtree of g , we have no change in the black height, and in the subtree, there is no red-red
error.
Now g is red. We have three possibilities: the parent of g is black, the parent of g is red, and g is
the root. Commentary: Possibility 1: Nothing. Possibility 2:
We have a red-red violation a level up and need to
Exercise 9.4 apply the transformations there. Possibility 3: turn g
back to black.
What do we do CS213/293
cbna
in eachData
case?
Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 19
Case 2: The uncle is not red and the path to the grandparent is not straight
straight means left 2 (parent 2 (n)) = n
or right 2 (parent 2 (n)) = n
h p h n
T1 T1
n h h p
T4 T2
h h h h
T2 T3 T3 T4
This transformation does not resolve the violation but converts the violation to case 3.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 20
Case 3: The uncle is not red and the path to the grandparent is straight
h+1 g h+1 p
h p g n
(g , p) rotate
T1
n swap colors
h h h h h
T2 T1 T2 T3 T4
h h
T3 T4
Exercise 9.5
a. Why roots of T2 , T3 , and T4 are black?
b. Show that if the root of T1 is red then the above operation does not work.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 21
Example: red-red correction case 1
Example 9.6
We just inserted 20 in the following tree. We need to apply case 1 to obtain a red-black tree.
8 8
re-color
5 17 5 17
12 21 12 21
20 20
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 22
Example: red-red correction case 2
Example 9.7
Consider the following example. We are attempting to insert 20. We apply case 2 to move towards
a red-black tree.
8 (20,21)-rotate 8
5 17 5 17
21 20
20 21
The above is not a red-black tree. We need to further apply case 3 to finally obtain the red-black
tree.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 23
Example: red-red correction case 3 (continued)
8 (17,20)-rotate 8
20 17 21
21
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 24
Summary of insertion
1. Insert like BST and assign red color to the new node.
2. While we have case 1, re-color nodes and move up the red-red violation.
3. If we find case 2 or 3, we rotate and the violation is finished.
4. If the root becomes red in the process, then turn it back to black.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 25
Topic 9.4
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 26
BST deletion in red-black tree
▶ Recall: In the BST deletion we always delete a node n that has at most one non-null child.
p Remove n p
n x
T3 T3
x
T1 T2
T1 T2
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 27
What can go wrong with a red-black tree?
Commentary: The or in the second bullet point is not xor. Both the violations are possible at the same time.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 28
Violation removal procedure
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 29
Violation pattern
After deletion, we may need to consider the following five nodes around x.
p p
re-color x
h to h − 1 x h s h x h s
T1 T1
c1 c2 c1 c2
T4 T4
T2 T3 T2 T3
Violation solved!
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 31
Case 2: x is not red and root.
Do nothing.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 32
Case 3: x is not red and the sibling of x is red
p s
(p, s) rotate
and swap colors p
h to h − 1 x h s h c2
T1 T4
c1 c2 h−1 x h c1
T4 T1
T2 T3 T2 T3
The transformation does not solve the height violation at parent of x but changes the sibling of x
from red to black.
Exercise 9.7
Why p, c1 , and c2 must be non-null and black?
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 33
Case 4.1: x is not red and sibling of x is black, and the right nephew is
red(Assuming x is left child)
p s
(p, s) rotate
swap the color of p and s
h to h − 1 x h s h p h c2
T1 T4
c1 c2 h−1 x h − 1 c1
T4 T1
T2 T3 T2 T3
Exercise 9.8
Write the above case if x is the right child of p?
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 34
Case 4.2: x is not red and the sibling of x is black, and the left and right
nephews are red and not red respectively
p p
(s, c1 ) rotate
and swap colors
h to h − 1 x h s x h−1 h c1
T1 T1
c1 c2 s
T2
T4
c2
T2 T3 T3
T4
The above transformation does not solve the height violation. It changes the right child of the
sibling from not red to red, which is the case 4.1.
Exercise 9.9
a. Why case 4.1 transformation cannot be applied to case 4.2?
b. Write the above case if x is the right child of p?
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 35
Case 4.3: x is not red and the sibling of x is black, and the nephews of x
are not red
p p
re-color s
h to h − 1 x h s h−1 x h−1 s
T1 T1
c1 c2 c1 c2
T4 T4
T2 T3 T2 T3
The above transformation reduces bh(p) by 1 and there may be potential violation at p, which is
at the lower level.
We run the case analysis again at node p. The only case that kicks the can upstairs!! All cases are
covered.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 36
Structure among cases
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 37
Summary of deletion
1. Delete like BST. There may be black height violation at the child of deleted node.
2. While we have case 4.3, re-color nodes and move up the black height violation.
3. For all the other cases, we rotate or re-color and the violation is finished.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 38
Topic 9.5
Problems
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 39
Insert and delete
Exercise 9.10
Consider the tree below. Can it be colored and turned into a red-black tree? If we wish to store
the set 1, . . . , 22, label each node with the correct number. Now add 23 to the set and then
delete 1. Also do the same in the reverse order. Are the answers the same? When will the answers
be the same?
A
B C
D E F G
H I J K L M N
O P Q R S T U
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 40
Topic 9.6
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 41
AVL (Adelson, Velsky, and Landis) tree
Definition 9.3
An AVL tree is a binary search tree such that for each node n
|height(right(n)) − height(left(n))| ≤ 1.
Example 9.8 8
An example of an AVL tree.
5 17
7 11 21
10 14
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 42
Exercise: Identify the AVL trees
Exercise 9.11
Which of the following are AVL trees?
17
17 21 17 17
5 20 5 21 5 21
3 18 11 5 11
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 43
Topic 9.7
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 44
AVL tree height
Theorem 9.1
The height of an AVL tree T having n nodes is O(log n).
Proof.
Let n(h) be the minimum number of nodes for height h.
Base case:
n(1) = 1 and n(2) = 2.
Induction step:
Consider an AVL tree with height h ≥ 3. In the minimum case, one child will have height n − 1
and the other n − 2. (Why?)
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 45
AVL tree height(2)
Proof(continued.)
Since n(h − 1) > n(h − 2),
n(h) > 2n(h − 2).
Therefore,
n(h) > 2i n(h − 2i).
For i = h/2 − 1(Why?), Commentary: Here is the explanation of the last step.
Consider an AVL tree with m nodes and h height. By
h/2−1 h/2
n(h) > 2 n(2) = 2 . definition, h(n) ≤ m. Since h < 2 log n(h), h <
2 log m. Therefore, h is O(log m).
Therefore,
h < 2 log n(h).
Theorem 9.2
Let T be an AVL tree. Let the level of the closest leaf to the root of T is k.
height(T ) ≤ 2k − 1
Proof.
.
Let D be the closest leaf of the tree.
2k − 1 r
▶ The height of right(C ) cannot be more than 2. (Why?)
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 47
A part of AVL is a complete tree
Theorem 9.3
Let T be an AVL tree. Let the level of the closest leaf to the root of T is k. Upto level k − 2 all
nodes have two children.
Proof.
A node at level k − 2 − i cannot be a leaf. (Why?)
Exercise 9.12
Show T has at least 2k−1 nodes.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 48
Another proof of tree height bound
Therefore,
n ≥ 2k−1 ≥ 2(h−1)/2
Exercise 9.13
What is the maximum number of nodes given height h?
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 49
Problem: A sharper bound for the AVL tree
Exercise 9.14
a. Find largest c such that c k−2 + c k−1 ≥ c k
b. Recall n(h) = 1 + n(h − 1) + n(h − 2). Let c0 be the largest c. Show that n(h) ≥ c h−1 .
c. Prove that the above bound is a sharper bound than our earlier proof.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 50
Topic 9.8
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 51
End of Lecture 9
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 52