0% found this document useful (0 votes)
8 views52 pages

Lec 09 RB

Uploaded by

Saksham Rathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views52 pages

Lec 09 RB

Uploaded by

Saksham Rathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

CS213/293 Data Structure and Algorithms 2023

Lecture 9: Red-Black Trees

Instructor: Ashutosh Gupta

IITB India

Compile date: 2023-09-20

cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 1
Topic 9.1

Balance and rotation

cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 2
Maintain balance

BST may have a large height.

Height is directly related to branching. More branching implies a shorter height.

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?

Rotation is a local operation, which must be guided by non-local measure height.

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

Definition 9.1 Example 9.3


A red-black tree T is a binary search tree such An example of a red-black tree.
that the following holds.
▶ All nodes are colored either red or black 8
▶ Null leaves have no color
5 17
▶ Root is colored black
▶ All red nodes have black children 21
▶ All paths from the root to null leaves
have the same number of black nodes.

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

Let h be the black height of a red-black tree containing n nodes.

▶ 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.

log4 n < h < 1 + log2 n

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.

Their running time will be O(log n) because h < 1 + log2 n.

The question is how do we do insertion and deletion on a red-black tree?

cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 14
Topic 9.3

Insertion in red-black tree

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.

▶ Good news: No change in the black height of the tree.


▶ Bad news: n may have a red parent.

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+1 g If n has a red parent, we correct the error either by rotation


or re-coloring.
u p
We have three cases.
n h
T4 ▶ Case 1: u is red
h h
T2 T3 ▶ Case 2: u is not red and g to n path is not straight

▶ Case 3: u is not red and g to n path is straight


Exercise 9.3
Why g must exist and be black? No transformation should change the black height of g .
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 18
Case 1: The uncle is red
h+1 g h+1 g
re-color

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+1 g (p,n) rotate h+1 g

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

The transformation removes the red-red violation.

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)

We apply case three as follows.

8 (17,20)-rotate 8

and color swap


5 17 5 20

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

Deletion red-black tree

cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 26
BST deletion in red-black tree

▶ Delete a node as if it is a binary search 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

x can be either a null or non-null node.

cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 27
What can go wrong with a red-black tree?

Since a child x of n takes the role of n, we need to check if x can replace n.

▶ If n was red, no violations occur. (Why?)


▶ If n was black, bh(x) = bh(n) − 1, or it is possible that both x and p are red.
| {z } | {z }
black height violation red-red violation

The leaves of the subtree rooted at


x will have one less black depth.

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

▶ To remove violation at the subtree of x, we may recolor and rotate around x.

▶ We may fix the violation at x or move the violation to the parent of x.

Therefore, we present the violation resolution cases in a generalized description.

In the following, x may be some internal node.

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 We correct the violation either by rotation or


re-coloring.
h to h − 1 x h s
There are six cases
T1 1. x is red
c1 c2
2. x is not red and root.
T4
3. x is not red and s is red
T2 T3 4. x is not red and s is black
4.1 c2 is red
Exercise 9.6 4.2 c2 is not red and c1 is red
Show: If x is not root and not red, 4.3 c2 is not red and c1 is not red
s must exist. The goal is to restore the black height of p.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 30
Case 1: x is red

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

The above transformation solves the black height violation.

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

▶ Case 1, 2, and 4.1 solve the violation at the node.


▶ Case 3 turns the violation into 4.1 or 4.2.
▶ Case 4.2 turns the violation into 4.1.
▶ Case 4.3 moves the violation from x to its parent p.

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

Extra slides: AVL trees

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

Height of AVL tree

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?)

Therefore, n(h) = 1 + n(h − 1) + n(h − 2). ...


Commentary: We need to show that n(h) > n(h − 1) is monotonous. Ideally, n(h) = 1 + n(h − 1) + min(n(h − 2), n(h − 1)). This proves that n(h) > n(h − 1).

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).

Therefore, the height of an AVL tree is O(log n). (Why?)


cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 46
Closest leaf

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?)

▶ Therefore, the maximum height of C is 3. 5 B


▶ Therefore, the maximum height of right(B) is 4.
▶ Therefore, the maximum height of B is 5. 3 C
2k − 2 4
▶ Continuing the argument, the maximum height of
root r is 2k − 1. 1 D
2

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?)

Let us assume that a node n at level k − 2 − i has a single child n′ .

The height of n′ cannot be more than 1. (Why?)

Therefore, n′ is a leaf. Contradiction.

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

Let T have n nodes and the height of T be h.

We know the following from the previous theorems.


▶ n ≥ 2k−1 , and
▶ 2k − 1 ≥ h.

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

Insertion and deletion

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

You might also like