Title: CS2420: Lecture 31
1CS2420 Lecture 31
- Vladimir Kulyukin
- Computer Science Department
- Utah State University
2Outline
- Red-Black Tree
- Bottom-up Insertion
- Top-Down Insertion
- Section 12.2
3Red-Black Tree Properties
- Every node is colored black or red.
- The root is black.
- If a node is red, its children must be black.
- Every path from a node to a NULL pointer must
have the same number of black nodes. - The NULL pointers are black.
4Red-Black Tree Example
30
15
70
10
20
85
60
5
50
65
80
90
40
55
5Red-Black Tree Properties
- No path is more than twice as long as any other
path. - If every path from the root to a NULL node
contains B black nodes, the tree contains at
least 2B 1 black nodes (Proof is by induction
on B). - The height of a red-black tree is at most
2log(N1).
6Red-Black Tree Bottom-Up Insertion
- A new node is always inserted as a leaf, because
this is a BST. - There are two choices for coloring the new node
black or red. - If we color the node black, we violate property
4. - If we color the node red, we may violate property
3. - Which should we choose black or red?
7Red-Black Tree Bottom-Up Insertion
- A new node is colored red.
- Why? Because it is easier.
- We may have to do no work.
8Red-Black Tree Three Basic Insertion Operations
- Color Flip (Color Change)
- Two Rotations
- Single rotation
- Double rotation
9Red-Black Tree Rotations
- In terms of pointer manipulation, Red-Black tree
rotations are the same as the AVL search tree
rotations. - Thus, there are two single rotations (right and
left) and two double rotations (left right and
right left). - For implementation purposes, we need only the two
single rotations. - The rotations are symmetric.
- Unlike the AVL search tree rotations, the
Red-Black tree rotations change node colors.
10Single Right at G S is Black
G
S
P
X
C
D
E
X is inserted and colored red Xs parents
sibling, S, is black
A
B
11Single Right at G S is Black
G
P
S
P
X
G
X
S
C
D
E
C
A
B
D
E
A
B
Before Rotation
After Rotation
12Single Left at G S is Black
G
P
S
X
A
B
C
E
D
X is inserted and colored red Xs parents
sibling, S, is black
13Single Left at G S is Black
G
P
P
S
X
G
X
C
S
C
A
B
D
E
D
E
A
B
Before Rotation
After Rotation
14Double Rotation (LR) at G S is Black
X is inserted and colored red Xs parents
sibling, S, is black
G
S
P
X
A
D
E
B
C
15Double Rotation (LR) at G S is Black
X
G
P
S
P
G
S
D
E
A
B
C
X
A
D
E
B
C
Before Rotation
After Rotation
16Double Rotation (RL) at G S is Black
- RL at G is symmetric to LR at G.
- We first rotate P right and then rotate G left.
- The color flips are the same as in LR.
17Question
- When S is black, will the single and
- double rotations re-balance the tree?
18Answer
- Yes, when S is black, both the single and double
rotations re-balance the tree. Why? Because the
number of black nodes on all paths remains the
same.
19Single Right at G S is Red
G
S
P
X
C
D
E
A
B
X is inserted and colored red Xs parents
sibling, S, is red
20Single Right at G S is Red
P
G
P
S
X
G
S
A
B
C
X
C
D
E
D
E
A
B
Before Rotation
After Rotation
21Single Left at G S is Red
- Single left at G when S is Red is symmetric to
single right at G when S is Red. - G goes down, P goes up.
- The color flips are the same.
22Double Rotation (LR) at G S is Red
G
S
P
X
A
D
E
B
C
23Double Rotation (LR) at G S is Red
X
G
S
P
G
P
A
B
X
A
D
E
C
S
B
C
D
E
Before Rotation
After Rotation
24Question
- When S is red, will the single or
- double rotations re-balance the
- tree?
25Answer
- When S is red, neither the single nor the double
rotations re-balance the tree, because the root
of the sub-tree is now red. - Property 3 may be violated higher up.
26Bottom-Up Insertion
- We insert a leaf, color it red, and then climb up
the insertion path until we reach the root or
perform a rotation that rebalances the tree. - We may have to make two passes one down and one
up the tree.
27Red-Black Tree Top-Down Insertion
- Can we insert, change colors, and rotate in one
pass down the tree? - Yes, we can.
- How?
- We need to guarantee that when we insert a node
(leaf), S is never red. - Basically, we have to flip colors as we drill
down the tree.
28Top-Down Color Flip
- When a node X has two red children, X is colored
red and its two children are colored black.
29Top-Down Color Flip
X
X
XL
XR
XL
XR
Before Color Flip
After Color Flip
30Question
- Is there anything that can go wrong
- during the top-down color flip?
31Answer
- Yes, the color flip may introduce two consecutive
red nodes. - Can it introduce more than two consecutive red
nodes? - No! Because that would mean that property 3 was
already violated before the insertion.
32Question
- Can we fix property 3 violations?
33Answer
- Yes!
- If a color flip results in two consecutive red
nodes, we can apply either single or double
rotation, whichever is appropriate. - But, since we have ensured that S is black, the
rotation will rebalance the tree.
34Top-Down Insertion Example
30
45
15
70
10
20
85
60
5
50
65
80
90
40
55
35Top-Down Insertion Example
Flip?
30
45
15
70
10
20
85
60
5
50
65
80
90
40
55
36Top-Down Insertion Example
Flip? No!
30
45
15
70
10
20
85
60
5
50
65
80
90
40
55
37Top-Down Insertion Example
Flip? No!
30
Flip?
15
70
45
10
20
85
60
5
50
65
80
90
40
55
38Top-Down Insertion Example
Flip? No!
30
Flip? No!
15
70
45
10
20
85
60
5
50
65
80
90
40
55
39Top-Down Insertion Example
Flip? No!
30
Flip? No!
15
70
Flip?
10
20
85
60
45
5
50
65
80
90
40
55
40Top-Down Insertion Example
Flip? No!
30
Flip? No!
15
70
Flip? No!
10
20
85
60
45
5
50
65
80
90
40
55
41Top-Down Insertion Example
Flip? No!
30
Flip? No!
15
70
Flip? No!
10
20
85
60
Flip?
5
50
65
80
90
45
40
55
42Top-Down Insertion Example
Flip? No!
30
Flip? No!
15
70
Flip? No!
10
20
85
60
Flip? Yes!
5
50
65
80
90
45
40
55
43Top-Down Insertion Example
30
15
70
2 Reds!!!
10
20
85
60
5
50
65
80
90
45
40
55
44Question
- Which Rotation and Where?
45Single Right at 70 85 is Black
70
60
85
60
50
70
50
85
C
D
E
C
A
B
D
E
A
B
Before Rotation
After Rotation
46Top-Down Insertion After the Rotation
30
15
60
10
70
20
50
5
85
65
40
55
45
80
90
47Top-Down Insertion Example
30
15
60
10
70
20
50
Flip?
5
85
65
40
55
45
80
90
48Top-Down Insertion Example
30
15
60
10
70
20
50
Flip? No!
5
85
65
40
55
45
80
90
49Top-Down Insertion Example
30
15
60
10
70
20
50
5
85
65
40
55
80
90
45
50Red-Black Tree Implementation
- nullNode is a node used in place of a NULL
pointer. - nullNode is always colored black .
- header is a node that is used as a dummy root.
- The value of the header is -9999.
- The real root is the right child of the header.