SLIDE 1 Red Black Tree 5 Rules
1
SLIDE 2 Red Black Tree Properties - A
- 1. Every Node Is Either RED or BLACK
- 2. Every NILL Node Is BLACK sometimes referred to as a "Null Leaf"
SLIDE 3 Red Black Tree Properties - B
- 3. Every RED Node Has Two Black Child Nodes (Or Is Childless) There
Can't Be Two RED Nodes In A Row
- 4. Every Distance Path From Node X Down To A Leaf Node Has The Same
Number Of BLACK Nodes
- 5. The Root Node Is Always BLACK
SLIDE 4 Rotation Generalization Right Rotation
4
SLIDE 5 Right Rotation - 1/2
- There Exists Some Square Node
(somewhere in the tree)
- There Exists Some Circle Node
(Where Value In Circle Is Less Than The Value In Square)
- There Exists Some Subtrees:
A, B, & C A B C
- Putting Values In Nodes Is Not
Necessary, But Since I Did It To Demonstrate The AVL Rotations, I Will Do It For The Red Black.
SLIDE 6 Right Rotation - 2/2
- Simple Way To Rotate The Valid Binary
Search Tree Without Altering The Search Capabilities.
- Single Rotate Right AVL
A B C 100 90 90 A C 100 B
SLIDE 7 Rotation Generalization Left Rotation
7
SLIDE 8 Left Rotation - 1/2
- There Exists Some Square Node
(somewhere in the tree)
- There Exists Some Circle Node
(Where Value In Circle Is Greater Than The Value In Square)
- There Exists Some Subtrees:
A, B, & C A B C
- Putting Values In Nodes Is Not
Necessary, But Since I Did It To Demonstrate The AVL Rotations, I Will Do It For The Red Black.
SLIDE 9 Left Rotation - 2/2
- Simple Way To Rotate The Valid Binary Search
Tree Without Altering The Search Capabilities.
A 90 100 C B A B C 90 100
SLIDE 10 Violation 1L RED UNCLE Case
(On Left)
10
SLIDE 11 Easy Case 1L - RED UNCLE Case - 1 (P On Left)
- The Color Distance Will Not
Change The Number of Black Down Any Branch
- If We Make The New Node RED, It
Minimizes The Number Of Bad Things That Can Happen:
- Only Thing That Can Go Wrong Is
Two Red Nodes In A Row
50 150 125 100
P Pf Pg Pu
Occurring As We Walk Our Way Up The Tree.
c d e f b
a
SLIDE 12 Easy Case 1L - RED UNCLE Case - 2 (P On Left)
- Easy Case 1L: UNCLE is RED No Rotation Required!
50 150 125 100
c
d
e
f
b
a
- Record The Relative Distances Down
The Various Paths In The Tree:
SLIDE 13 Easy Case 1L - RED UNCLE Case - 3 (P On Left)
- Easy Case 1L: UNCLE is RED No Rotation Required!
50 150 125 100
c d e f b
a
P Pf Pg Pu
- ab = 1
- ac = 1
- ad = 1
- ae = 1
- af = 1
- Pf Color = BLACK Pu Color = BLACK Pg Color = RED
50 150 125 100
c d e f b
a
P Pf Pg Pu
- Double Check The Distances ab=1, ac=1, ad=1, ae=1, af=1 YEA!
SLIDE 14
Case 1L Rule Summary
50 150 125 100
c d e f b
a
P Pf Pg Pu
50 150 125 100
c d e f b
a
P Pf Pg Pu
SLIDE 15 Violation 1R RED UNCLE Case
(On Right)
15
SLIDE 16 Easy Case 1R - RED UNCLE Case - 1 (P On Right)
- The Color Distance Will Not
Change The Number of Black Down Any Branch
- If We Make The New Node RED, It
Minimizes The Number Of Bad Things That Can Happen:
- Only Thing That Can Go Wrong Is
Two Red Nodes In A Row
50 200 150 125
P Pf Pg Pu
Occurring As We Walk Our Way Up The Tree.
c d e f b
a
SLIDE 17 Easy Case 1R - RED UNCLE Case - 2 (P On Right)
- Easy Case 1R: UNCLE is RED No Rotation Required!
- ac = 1
- ad = 1
- ae = 1
- af = 1
- ab = 1
- Record The Relative Distances Down
The Various Paths In The Tree:
50 200 150 125
P Pf Pg Pu
c d e f b
a
SLIDE 18 Easy Case 1R - RED UNCLE Case - 3 (P On Right)
- Easy Case 1L: UNCLE is RED No Rotation Required!
- How Fix?
- ab = 1
- ac = 1
- ad = 1
- ae = 1
- af = 1
- Pf Color = BLACK Pu Color = BLACK Pg Color = RED
- Double Check The Distances ab=1, ac=1, ad=1, ae=1, af=1 YEA!
50 200 150 125
c d e f b
a
P Pf Pg Pu
50 200 150 125
c d e f b
a
P Pf Pg Pu
SLIDE 19
Case 1R Rule Summary
50 200 150 125
c d e f b
a
P Pf Pg Pu
50 200 150 125
c d e f b
a
P Pf Pg Pu
SLIDE 20 RED Uncle Can Occur With Insertion RED UNCLE Case
(On Left)
20
SLIDE 21
- Distance Property = 2 For All - If Count Null
Easy Case 1L - RED UNCLE Case - 1 (P On Left)
- Easy Case 1L: UNCLE is RED No Rotation Required!
200 250
- Add 50 to Tree - Make it Red
- Will Not Change The Number of
Black Down Any Branch
- Minimize The Number Of Bad
Things That Can Happen:
- Only Thing That Can Go Wrong Is
Two Red Nodes In A Row
50 150 125 100
P Pf Pg Pu
SLIDE 22 Easy Case 1L - RED UNCLE Case - 2 (P On Left)
- Easy Case 1L: UNCLE is RED No Rotation Required!
200 250 50 150 125 100
- How Fix?
- THIS IS EASY CASE - Pu Is Red
(125)
- Might Become Easier If We Don't
Try To Fix Everything At Once Start With This Portion
Make Pf BLACK Make Pu BLACK
- This Shifts The Problem Up Two
Generations! P Pg Pf Pu
SLIDE 23 Easy Case 1L - RED UNCLE Case - 3 (P On Left)
200 250 50 150 125 100
- How Fix?
- How Fix?
- Change Pf To BLACK
- 2 BLACK UNCLE Cases: One Requires A Rotation
One Requies A Preliminary Rotation And A Second Rotation
- Distance Property = 3 For All
P Pf
SLIDE 24 RED Uncle Can Occur With Insertion Add To Tree Violation 1R RED UNCLE Case
(On Right)
24
SLIDE 25
- Distance Property = 2 For All If Count Null
Easy Case 1R - RED UNCLE Case - 1 (P On Right)
- Easy Case 1R: UNCLE is RED No Rotation Required!
100 50 150 200
- Add 225 to Tree - Make it Red
- Will Not Change The Number of
Black Down Any Branch
- Minimize The Number Of Bad
Things That Can Happen:
- Only Thing That Can Go Wrong Is
Two Red Nodes In A Row
125 225
P Pf Pg Pu
SLIDE 26 Easy Case 1R - RED UNCLE Case - 2 (P On Right)
100 50 150 200
- How Fix?
- THIS IS EASY CASE - Uncle Of P
Is Red (125)
- Might Become Easier If We Don't
Try To Fix Everything At Once Start With This Portion
Make Pf BLACK Make Pu BLACK
125 225
- This Shifts The Problem Up Two
Generations! Pg Pf P Pu
SLIDE 27 Easy Case 1R - RED UNCLE Case - 3 (P On Right)
100 50 150 200
- How Fix?
- Change Pf To BLACK
125 225
- 2 BLACK UNCLE Cases: One Requires A Rotation
One Requies A Preliminary Rotation And A Second Rotation
- Distance Property = 3 For All
Pf P
SLIDE 28 Violation 3L BLACK UNCLE Case
With One Rotation
(On Left)
28
SLIDE 29 Case 3L - BLACK UNCLE One Rotation Class- 1 (P On Left)
200 250
Occurring As We Walk Our Way Up The Tree.
- We Have Already Established That
The Distance Property Is OK
125 100
a b c
d
e
f
- af = 2
- GOAL: Fix The RED CHILD With RED
Father Problem Without Messing Up The Distance Properties.
SLIDE 30 Case 3L - BLACK UNCLE One Rotation Class- 2 (P On Left)
200 250
125 100
a b c d e f
- ab = 1
- ac = 1
- ad = 1
- ae = 2
- af = 2
- Label P Pf Pg Pu
P Pf Pg Pu b c P Pf 125
100
a
200 250
e f Pg Pu
d
- Hook up 'd'
- Swap Colors Pg Pf
- Double Check The Distances ab=1, ac=1, ad=1, ae=2, af=2 YEA!
SLIDE 31
Case 3L Rule Summary f
200 250 125 100
a b c d e P Pf Pg Pu P Pu b c Pf 125
100
a
250
e f Pg d
200
SLIDE 32 Violation 3R BLACK UNCLE Case
With One Rotation
(On Right)
32
SLIDE 33 Case 3R - BLACK UNCLE One Rotation Class- 1 (P On Right)
200 250
Occurring As We Walk Our Way Up The Tree.
- We Have Already Established That
The Distance Property Is OK
125 300
a b c
d
e
f
- af = 1
- GOAL: Fix The RED CHILD With RED
Father Problem Without Messing Up The Distance Properties.
SLIDE 34
200 125
b c Case 3R - BLACK UNCLE One Rotation Class- 2 (P On Right)
200
125
a b c
250 300
d e f
- ab = 2
- ac = 2
- ad = 1
- ae = 1
- af = 1
- Label P Pf Pg Pu
P Pf Pg Pu a
- Double Check The Distances ab=2, ac=2, ad=1, ae=1, af=1 YEA!
- Promote Pf Up
250 300
e f P Pf
d
SLIDE 35
Case 3R Rule Summary
200 125
a b c
250 300
d e f P Pf Pg Pu
200 125
b c a
250 300
e f P Pf
SLIDE 36 Violation 2L BLACK UNCLE Case
With Two Rotations
(On Left)
36
SLIDE 37 Case 2L - BLACK UNCLE Two Rotations Class- 1 (P On Left)
200 250 125 150
P
Pf Pg Pu
Occurring As We Walk Our Way Up The Tree.
- We Have Already Established That
The Distance Property Is OK c
d
e
f
a b
- ab = 1
- Case 2L is going to turn into Case 3L.
- This Is The Worst Possible Case
SLIDE 38 Case 2L - BLACK UNCLE Two Rotations Class- 2 (P On Left)
200 250 100 125
- ab = 1
- ac = 1
- ad = 1
- ae = 2
- af = 2
P
Pf Pg Pu c d e f a b
- Might Become Easier If We Don't
Try To Fix Everything At Once Start With This Portion
125 P
d
200 250
Pg Pu e f a
1
100
Pf b
c
- Hook up 'c'
- This Is Case 3L
SLIDE 39 Case 2L - BLACK UNCLE Two Rotations Class- 3 (P On Left)
- ab = 1
- ac = 1
- ad = 1
- ae = 2
- af = 2
- Swap Pointers Pf P
1
125 Pf
d
200 250
Pg Pu e f a
100
P b c
SLIDE 40 Case 2L - BLACK UNCLE Two Rotations Class- 4 (P On Left)
- ab = 1
- ac = 1
- ad = 1
- ae = 2
- af = 2
- Swap Pointers Pf & P
2
125 Pf
d
200 250
Pg Pu e f a
100
P b c b c P Pf 125
100
200 250
e f Pg Pu d a
- Double Check The Distances ab=1, ac=1, ad=1, ae=2, af=2 YEA!
SLIDE 41
Case 2L Rule Summary
200 250 125 150 P
Pf Pg Pu c d e f a b b c P Pf 125
100 200 250
e f Pg Pu d a
SLIDE 42 Violation 2R BLACK UNCLE Case
With Two Rotations
(On Right)
42
SLIDE 43 Case 2R- BLACK UNCLE Two Rotations Class- 1 (P On Right)
Occurring As We Walk Our Way Up The Tree.
- We Have Already Established That
The Distance Property Is OK c
d
e
f
a b
- ab = 2
- Case 2R is going to turn into Case 3R.
- This Is The Worst Possible Case
200 125
a
300 250 P
Pf Pg Pu
SLIDE 44 Case 2R - BLACK UNCLE Two Rotations Class- 2 (P On Right)
- ab = 2
- ac = 2
- ad = 1
- ae = 1
- af = 1
- Might Become Easier If We Don't
Try To Fix Everything At Once Start With This Portion
1
c d e f a b
200 125
a
300 250 P
Pf Pg Pu
- Hook P To The Right Of Pg
d
250 P
c a b
200 125
Pg Pu
e
- Hook Pf To The Right Of P
f
300
Pf
SLIDE 45 Case 2R - BLACK UNCLE Two Rotations Class- 3 (P On Right) d
250 P
c a b
200 125
Pg Pu e f
300
Pf
- Swap Labels Pf P
- This Is Case 3L
- ab = 2
- ac = 2
- ad = 1
- ae = 1
- af = 1
SLIDE 46 Case 2R - BLACK UNCLE Two Rotations Class- 4 (P On Right)
2
- Double Check The Distances ab=2, ac=2, ad=1, ae=1, af=1 YEA!
- ab = 2
- ac = 2
- ad = 1
- ae = 1
- af = 1
d
250 P
c a b
200 125
Pg Pu e f
300
Pf
200 125
b c
250 300
e f P Pf d a
SLIDE 47
Case 2R Rule Summary c d e f a b
200 125
a
300 250 P
Pf Pg Pu
200 125
b c
250 300
e f P Pf d a
SLIDE 48 Rotation Generalization Right Rotation
48
SLIDE 49
SLIDE 50 Practice Problem #1
50
SLIDE 51 #1 Practice Red Black Insertion Add 10 to Empty Tree Some folks draw in the NULL Leaves - I do not! Some folks code this with an empty node to represent the Leaves
Label P Pf Pg Pu
P Pf = / Pg = / Pu = /
SLIDE 52 Practice Problem #2
52
SLIDE 53
#2 Practice Red Black Insertion Add 5 Tee Is There A Need To Do Anything To Preserve The Red Black Tree? No Label P Pf Pg Pu
P Pf Pg = / Pu = /
SLIDE 54 Practice Problem #3
54
SLIDE 55
#3 Practice Red Black Insertion Add 15 Tee Is There A Need To Do Anything To Preserve The Red Black Tree? No Label P Pf Pg Pu
P Pf Pg = / Pu = /
SLIDE 56 Practice Problem #4
56
SLIDE 57
#4 Practice Red Black Insertion Add 15 Tree
Is There A Need To Do Anything To Preserve The Red Black Tree?
No
Label P Pf Pg Pu
P Pf Pg = / Pu = /
SLIDE 58 Practice Problem #5
58
SLIDE 59
#5 Practice Red Black Insertion Add 5 Tree
Is There A Need To Do Anything To Preserve The Red Black Tree?
No
Label P Pf Pg Pu
P Pf Pg = / Pu = /
SLIDE 60 Practice Problem #6
60
SLIDE 61
#6 Practice Red Black Insertion Add 3 - 1
Is There A Need To Do Anything To Preserve The Red Black Tree?
Yes
Label P Pf Pg Pu
P Pf Pg Pu = /
Simple Rotate Right [ Pg Around Pf ]
P Pf Pg
What Do Now?
SLIDE 62 #6 Practice Red Black Insertion Add 3 - 2
P Pf Pg
Color Change (Pf & Pg)
Pg Pf P
- AVL Simple Rotate Right
What Do Now?
SLIDE 63 Practice Problem #7
63
SLIDE 64
#7 Practice Red Black Insertion Add 20 - 1
Is There A Need To Do Anything To Preserve The Red Black Tree?
Yes
Label P Pf Pg Pu
P Pf Pg Pu = /
Simple Rotate Left [ Pg Around Pf ]
P Pf Pg
What Do Now?
SLIDE 65 #7 Practice Red Black Insertion Add 20 - 2
P Pf Pg P Pf Pg
Color Change (Pf & Pg)
What Do Now?
SLIDE 66 Practice Problem #8
66
SLIDE 67
#8 Practice Red Black Insertion Add 8 - 1
Is There A Need To Do Anything To Preserve The Red Black Tree? Yes
Label P Pf Pg Pu
P Pf Pg Pu = /
Rotate Pf & P To The Left Around Pf
Pg Pf P
What Do Now?
SLIDE 68 #8 Practice Red Black Insertion Add 8 - 2 Color Change (Pf & Pg)
Pg Pf P
Simple Rotate Right [ Pg Around Pf ]
Pg Pf P Pg Pf P
What Do Now? What Do Now?
- AVL Simple Double Rotate Right
Pg P Pf
Swap Pointers Pf & P What Do Now?
SLIDE 69 Practice Problem #9
69
SLIDE 70
#9 Practice Red Black Insertion Add 8 - 1
Is There A Need To Do Anything To Preserve The Red Black Tree? Yes
Label P Pf Pg Pu
P Pf Pg Pu = /
Rotate Pf & P To The Right Around Pf
Pg P Pf
What Do Now?
SLIDE 71 #9 Practice Red Black Insertion Add 8 - 2 Color Change (Pf & Pg) Simple Rotate Left[ Pg Around Pf ]
P Pf Pg P Pf Pg
What Do Now? What Do Now?
- AVL Simple Double Rotate Left
Pg P Pf Pg Pf P
What Do Now? Swap Pointers Pf & P
SLIDE 72 Practice Problem #10
72
SLIDE 73
#10 Practice Red Black Insertion Add 5 - 1
Is There A Need To Do Anything To Preserve The Red Black Tree?
Yes
Label P Pf Pg Pu
P Pf Pg Pu
What Do Now? Violation 1L
SLIDE 74
#10 Practice Red Black Insertion Add 5 - 2
P Pg Pf Pu
Fix Violation 1L
P Pg Pf Pu
Make Tree Root BLACK What Do Now?
P Pg Pf Pu
SLIDE 75 Practice Problem #11
75
SLIDE 76
#11 Practice Red Black Insertion Add 15 - 1
Is There A Need To Do Anything To Preserve The Red Black Tree?
Yes
Label P Pf Pg Pu
P Pf Pg Pu
What Do Now? Violation 1R
SLIDE 77
Fix Violation 1R #11 Practice Red Black Insertion Add 15 - 2
P Pf Pg Pu P Pf Pg Pu
Make Tree Root BLACK
P Pf Pg Pu
What Do Now?
SLIDE 78 Practice Problem #12
78
SLIDE 79
#12 Practice Red Black Insertion Add 25
Is There A Need To Do Anything To Preserve The Red Black Tree?
No
P Pg Pf Pu
Label P Pf Pg Pu
SLIDE 80 Practice Problem #13
80
SLIDE 81
#13 Practice Red Black Insertion Add 8 - 1
Is There A Need To Do Anything To Preserve The Red Black Tree?
Yes
P Pg Pf Pu = /
Label P Pf Pg Pu
Pf Pg P
Simple Rotate Right Pg Around Pf What Do Now?
SLIDE 82
#13 Practice Red Black Insertion Add 8 - 2
Pf Pg P Pf Pg P
Swap Pointers Pf & P What Do Now?
SLIDE 83
#13 Practice Red Black Insertion Add 8 - 3 Simple Rotate Right PB Around Pf
Pf Pg P
What Do Now?
Pf Pg P
SLIDE 84
#13 Practice Red Black Insertion Add 8 - 4
Pf Pg P
What Do Now? Color Change (Pf & Pg)
Pf Pg P
SLIDE 85 Practice Problem #14
85
SLIDE 86
#14 Practice Red Black Insertion Add 8 - 1
Is There A Need To Do Anything To Preserve The Red Black Tree? Yes P Pg Pf Pu = /
Label P Pf Pg Pu
Pf Pg P
Rotate P & Pf To The Left Around Pf What Do Now?
SLIDE 87
#14 Practice Red Black Insertion Add 8 - 2
Pf Pg P P Pg Pf
Swap Pointers Pf & P What Do Now?
SLIDE 88
#14 Practice Red Black Insertion Add 8 - 3 Simple Rotate Right Pg Around Pf Color Change (Pf & Pg)
P Pg Pf
What Do Now?
P Pg Pf
What Do Now?
P Pg Pf
SLIDE 89 Practice Problem #15
89
SLIDE 90
#15 Practice Red Black Insertion Add 9 - 1
Is There A Need To Do Anything To Preserve The Red Black Tree?
Yes
P Pg Pf Pu
Label P Pf Pg Pu
SLIDE 91
#15 Practice Red Black Insertion Add 9 - 2
Is There A Need To Do Anything To Preserve The Red Black Tree?
Yes
P Pg Pf Pu
Label P Pf Pg Pu Violation 1R
SLIDE 92 Practice Problem #16
92
SLIDE 93
#16 Practice Red Black Insertion Add 13 - 1
P Pg Pf Pu
Label P Pf Pg Pu Violation 1R
Is There A Need To Do Anything To Preserve The Red Black Tree?
Yes
What Do Now?
SLIDE 94
#16 Practice Red Black Insertion Add 13 - 2
P Pg Pf Pu
Apply Violation 1R
SLIDE 95
#16 Practice Red Black Insertion Add 13 - 3 What Do Now?
P Pg Pf Pu P Pg Pf Pu
Move Potential Problem (focus) Up Two Levels
SLIDE 96
#16 Practice Red Black Insertion Add 13 - 4 What Do Now?
P Pg Pf Pu
Rotate P & Pf To The Left Around Pf
P Pg Pf Pu
SLIDE 97
#16 Practice Red Black Insertion Add 13 - 5 What Do Now? Swap Pointers P & Pf
P Pg Pf Pu P Pg Pf Pu
SLIDE 98
#16 Practice Red Black Insertion Add 13 -6 What Do Now? Fix Violation 3L
P Pg Pf Pu P Pg Pf Pu
Note That Pf-R Goes To Pg-L
SLIDE 99
SLIDE 100 Rotation Generalization Right Rotation
100