Red Black Tree 5 Rules 1
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"
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
Rotation Generalization Right Rotation 4
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) C There Exists Some Subtrees: A, B, & C A B 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.
Right Rotation - 2/2 100 90 C 90 A 100 A Simple Way To Rotate The Valid Binary B C B Search Tree Without Altering The Search Capabilities. Single Rotate Right AVL
Rotation Generalization Left Rotation 7
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) A There Exists Some Subtrees: A, B, & C C B 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.
Left Rotation - 2/2 90 100 A 100 C 90 C B A B Simple Way To Rotate The Valid Binary Search Tree Without Altering The Search Capabilities. Single Rotate Left AVL
Violation 1L RED UNCLE Case (On Left) 10
Easy Case 1L - RED UNCLE Case - 1 (P On Left) This RED UNCLE Case Is a Occurring As We Walk Our Way Up Pg 125 The Tree. If We Make The New Node RED, It Minimizes The Number Of Bad Pf 100 150 Pu Things That Can Happen: e d f The Color Distance Will Not 50 P Change The Number of Black Down Any Branch b c Only Thing That Can Go Wrong Is Two Red Nodes In A Row Label P Pf Pg Pu Label The Distance Paths
Easy Case 1L - RED UNCLE Case - 2 (P On Left) Record The Relative Distances Down a The Various Paths In The Tree: 125 ab = 1 ac = 1 100 150 ad = 1 ae = 1 d e f 50 af = 1 b c Easy Case 1L: UNCLE is RED No Rotation Required!
Easy Case 1L - RED UNCLE Case - 3 (P On Left) ab = 1 a ac = 1 a ad = 1 Pg How Fix? 125 Pg 125 ae = 1 af = 1 Pf 100 150 Pu Pf 100 150 Pu d e f 50 d e P f 50 P b c b c Pf Color = BLACK Pu Color = BLACK Pg Color = RED Easy Case 1L: UNCLE is RED No Rotation Required! Double Check The Distances ab=1, ac=1, ad=1, ae=1, af=1 YEA!
Case 1L Rule Summary a a Pg Pg 125 125 Pf Pf 100 100 150 150 Pu Pu e d e d f f 50 50 P P b b c c
Violation 1R RED UNCLE Case (On Right) 15
Easy Case 1R - RED UNCLE Case - 1 (P On Right) This RED UNCLE Case Is a Occurring As We Walk Our Way Up Pg 150 The Tree. If We Make The New Node RED, It Minimizes The Number Of Bad Pu 125 Pf 200 Things That Can Happen: b c P The Color Distance Will Not d 50 Change The Number of Black Down Any Branch e f Only Thing That Can Go Wrong Is Two Red Nodes In A Row Label P Pf Pg Pu Label The Distance Paths
Easy Case 1R - RED UNCLE Case - 2 (P On Right) Record The Relative Distances Down a The Various Paths In The Tree: Pg 150 ab = 1 ac = 1 Pu 125 Pf 200 ad = 1 ae = 1 b c P d 50 af = 1 e f Label P Pf Pg Pu Label The Distance Paths Easy Case 1R: UNCLE is RED No Rotation Required!
Easy Case 1R - RED UNCLE Case - 3 (P On Right) ab = 1 a ac = 1 a ad = 1 How Fix? Pg 150 Pg 150 ae = 1 af = 1 125 Pu 200 Pf 125 Pu 200 Pf b P c 50 d b c 50 P d e f e f Pf Color = BLACK Pu Color = BLACK Pg Color = RED Easy Case 1L: UNCLE is RED No Rotation Required! Double Check The Distances ab=1, ac=1, ad=1, ae=1, af=1 YEA!
Case 1R Rule Summary a a Pg Pg 150 150 125 125 Pu 200 Pf Pu 200 Pf b b c P 50 P c 50 d d e e f f
RED Uncle Can Occur With Insertion RED UNCLE Case (On Left) 20
Easy Case 1L - RED UNCLE Case - 1 (P On Left) Distance Property = 2 For All - If Count Null Add 50 to Tree - Make it Red Minimize The Number Of Bad 200 Things That Can Happen: Will Not Change The Number of Pg 250 125 Black Down Any Branch Only Thing That Can Go Wrong Is 100 Pf 150 Two Red Nodes In A Row Pu 50 P Label P Pf Pg Pu Easy Case 1L: UNCLE is RED No Rotation Required!
Easy Case 1L - RED UNCLE Case - 2 (P On Left) How Fix? Might Become Easier If We Don't 200 Try To Fix Everything At Once Start With This Portion Pg 125 250 THIS IS EASY CASE - Pu Is Red (125) 100 Pf Pu 150 Make Pg RED Make Pf BLACK Make Pu BLACK 50 P This Shifts The Problem Up Two Generations! Easy Case 1L: UNCLE is RED No Rotation Required!
Easy Case 1L - RED UNCLE Case - 3 (P On Left) How Fix? How Fix? Change Pf To BLACK 200 Pf Distance Property = 3 For All 125 250 P 100 150 50 2 BLACK UNCLE Cases: One Requires A Rotation One Requies A Preliminary Rotation And A Second Rotation
RED Uncle Can Occur With Insertion Add To Tree Violation 1R RED UNCLE Case (On Right) 24
Easy Case 1R - RED UNCLE Case - 1 (P On Right) Distance Property = 2 For All If Count Null Add 225 to Tree - Make it Red 100 Minimize The Number Of Bad Things That Can Happen: 50 150 Pg Will Not Change The Number of Black Down Any Branch Only Thing That Can Go Wrong Is 125 Pu 200 Pf Two Red Nodes In A Row P 225 Label P Pf Pg Pu Easy Case 1R: UNCLE is RED No Rotation Required!
Easy Case 1R - RED UNCLE Case - 2 (P On Right) How Fix? 100 Might Become Easier If We Don't Try To Fix Everything At Once 50 150 Pg Start With This Portion THIS IS EASY CASE - Uncle Of P 125 Is Red (125) Pu 200 Pf Make Pg RED Make Pf BLACK P 225 Make Pu BLACK This Shifts The Problem Up Two Generations!
Easy Case 1R - RED UNCLE Case - 3 (P On Right) How Fix? Pf 100 Change Pf To BLACK 50 P 150 Distance Property = 3 For All 125 200 225 2 BLACK UNCLE Cases: One Requires A Rotation One Requies A Preliminary Rotation And A Second Rotation
Violation 3L BLACK UNCLE Case With One Rotation (On Left) 28
Case 3L - BLACK UNCLE One Rotation Class- 1 (P On Left) This BLACK UNCLE Case Is a Occurring As We Walk Our Way Up 200 The Tree. We Have Already Established That 250 125 The Distance Property Is OK ab = 1 f e d ac = 1 100 ad = 1 ae = 2 b c af = 2 GOAL: Fix The RED CHILD With RED Father Problem Without Messing Up The Distance Properties.
Case 3L - BLACK UNCLE One Rotation Class- 2 (P On Left) ab = 1 a ac = 1 a ad = 1 Pg 200 Pf 125 ae = 2 af = 2 Pu 250 Pf 125 Pg 100 200 P e f d b c 100 d P 250 Pu b c e f Solve RED RED Problem? Promote Pf Up Hook up 'd' Label P Pf Pg Pu Hook Pg Right of Pf Swap Colors Pg Pf Double Check The Distances ab=1, ac=1, ad=1, ae=2, af=2 YEA!
Case 3L Rule Summary a a Pf 125 Pg 200 250 Pu 125 Pf 100 Pg P 200 e d f b c d Pu 250 100 P e f b c
Violation 3R BLACK UNCLE Case With One Rotation (On Right) 32
Case 3R - BLACK UNCLE One Rotation Class- 1 (P On Right) This BLACK UNCLE Case Is a Occurring As We Walk Our Way Up 200 The Tree. We Have Already Established That 250 The Distance Property Is OK 125 ab = 2 d c b 300 ac = 2 ad = 1 ae = 1 e f af = 1 GOAL: Fix The RED CHILD With RED Father Problem Without Messing Up The Distance Properties.
Case 3R - BLACK UNCLE One Rotation Class- 2 (P On Right) ab = 2 a ac = 2 a ad = 1 Pg 200 Pf 250 ae = 1 af = 1 Pu 250 200 Pf 125 P 300 d e b c f 300 P 125 d e f b c Solve RED RED Problem? Promote Pf Up Hook up 'd' Label P Pf Pg Pu Hook Pg Left of Pf Swap Colors Pg Pf Double Check The Distances ab=2, ac=2, ad=1, ae=1, af=1 YEA!
Case 3R Rule Summary a a Pg Pf 200 250 200 P Pu 300 250 Pf 125 e d f b c 125 300 P e b f c
Violation 2L BLACK UNCLE Case With Two Rotations (On Left) 36
Case 2L - BLACK UNCLE Two Rotations Class- 1 (P On Left) This BLACK UNCLE Case Is a Occurring As We Walk Our Way Up Pg 200 The Tree. We Have Already Established That Pu 250 The Distance Property Is OK Pf 125 ab = 1 ac = 1 b e f P 150 ad = 1 ae = 2 c d af = 2 Label P Pf Pg Pu Case 2L is going to turn into Case 3L. This Is The Worst Possible Case
Recommend
More recommend