1 / 100

Red Black Tree 5 Rules

Red Black Tree 5 Rules. Red Black Tree Properties - A. Every NILL Node Is BLACK  sometimes referred to as a "Null Leaf". Every Node Is Either RED or BLACK. Red Black Tree Properties - B. Every RED Node Has Two Black Child Nodes ( Or Is Childless)  There Can't Be Two RED Nodes In A Row.

leroyc
Download Presentation

Red Black Tree 5 Rules

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Red Black Tree 5 Rules

  2. Red Black Tree Properties - A • Every NILL Node Is BLACK  sometimes referred to as a "Null Leaf" • Every Node Is Either RED or BLACK

  3. Red Black Tree Properties - B • Every RED Node Has Two Black Child Nodes (Or Is Childless) There Can't Be Two RED Nodes In A Row • Every Distance Path From Node X Down To A Leaf Node Has The Same Number Of BLACK Nodes • The Root Node Is Always BLACK

  4. Rotation GeneralizationRight Rotation

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

  6. Right Rotation - 2/2 100 90 C 90 100 A • Simple Way To Rotate The Valid Binary Search Tree Without Altering The Search Capabilities. A B C B • Single Rotate Right  AVL

  7. Rotation GeneralizationLeft Rotation

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

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

  10. Violation 1LRED UNCLE Case(On Left)

  11. Pg Pf Pu P Easy Case 1L - RED UNCLE Case - 1 (P On Left) a • This RED UNCLE Case Is Occurring As We Walk Our Way Up The Tree. 125 • Label P Pf  Pg  Pu • If We Make The New Node RED, It Minimizes The Number Of Bad Things That Can Happen: 100 e d f 150 50 • The Color Distance Will Not Change The Number of Black Down Any Branch b c • Only Thing That Can Go Wrong Is Two Red Nodes In A Row • Label The Distance Paths

  12. Easy Case 1L - RED UNCLE Case - 2 (P On Left) • Record The Relative Distances Down The Various Paths In The Tree: a 125 • Easy Case 1L: UNCLE is RED  No Rotation Required! • ab = 1 • ac = 1 100 150 • ad = 1 • ae = 1 e d f 50 • af = 1 b c

  13. Easy Case 1L - RED UNCLE Case - 3 (P On Left) a a Pg 125 • How Fix? Pg 125 • Easy Case 1L: UNCLE is RED  No Rotation Required! Pf 100 150 Pu Pf 100 150 Pu • ab = 1 e d • ac = 1 f 50 e d P f 50 • ad = 1 P • ae = 1 • af = 1 b c b c • Pf  Color = BLACK Pu  Color = BLACK Pg Color = RED • Double Check The Distances ab=1, ac=1, ad=1, ae=1, af=1  YEA!

  14. Case 1L Rule Summary a a Pg 125 Pg 125 Pf 100 150 Pu Pf 100 150 Pu e d f 50 e d P f 50 P b c b c

  15. Violation 1RRED UNCLE Case(On Right)

  16. Pg Pu Pf P Easy Case 1R - RED UNCLE Case - 1 (P On Right) a • This RED UNCLE Case Is Occurring As We Walk Our Way Up The Tree. 150 • Label P Pf  Pg  Pu • If We Make The New Node RED, It Minimizes The Number Of Bad Things That Can Happen: 125 b c 200 d 50 • The Color Distance Will Not Change The Number of Black Down Any Branch e f • Only Thing That Can Go Wrong Is Two Red Nodes In A Row • Label The Distance Paths

  17. Pg Pu Pf P Easy Case 1R - RED UNCLE Case - 2 (P On Right) a • Record The Relative Distances Down The Various Paths In The Tree: 150 • Label P Pf  Pg  Pu • Easy Case 1R: UNCLE is RED  No Rotation Required! • ab = 1 • ac = 1 125 • ad = 1 • ae = 1 b c 200 d 50 • af = 1 e f • Label The Distance Paths

  18. Easy Case 1R - RED UNCLE Case - 3 (P On Right) a a Pg 150 • How Fix? Pg 150 • Easy Case 1L: UNCLE is RED  No Rotation Required! 125 Pu Pf 125 Pu Pf • ab = 1 • ac = 1 b 200 200 P 50 c d b c 50 P d • ad = 1 • ae = 1 • af = 1 e f e f • Pf  Color = BLACK Pu  Color = BLACK Pg Color = RED • Double Check The Distances ab=1, ac=1, ad=1, ae=1, af=1  YEA!

  19. Case 1R Rule Summary a a Pg 150 Pg 150 125 200 Pu Pf 125 200 Pu Pf b P 50 c d b c 50 P d e f e f

  20. RED Uncle Can Occur With InsertionRED UNCLE Case(On Left)

  21. 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 Things That Can Happen: 200 • Easy Case 1L: UNCLE is RED  No Rotation Required! • Will Not Change The Number of Black Down Any Branch 250 Pg 125 • Only Thing That Can Go Wrong Is Two Red Nodes In A Row 50 150 100 Pf Pu P • Label P Pf  Pg  Pu

  22. Easy Case 1L - RED UNCLE Case - 2 (P On Left) • How Fix? • Might Become Easier If We Don't Try To Fix Everything At OnceStart With This Portion 200 • Easy Case 1L: UNCLE is RED  No Rotation Required! Pg 250 125 • THIS IS EASY CASE - Pu Is Red (125) 50 150 100 Pf Pu • Make Pg REDMake Pf BLACKMake Pu BLACK P • This Shifts The Problem Up Two Generations!

  23. Easy Case 1L - RED UNCLE Case - 3 (P On Left) • How Fix? • How Fix? • Change Pf To BLACK Pf 200 250 125 P • Distance Property = 3 For All 50 150 100 • 2 BLACK UNCLE Cases: One Requires A RotationOne Requies A Preliminary Rotation And A Second Rotation

  24. RED Uncle Can Occur With InsertionAdd To Tree Violation 1RRED UNCLE Case(On Right)

  25. 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: • Easy Case 1R: UNCLE is RED  No Rotation Required! 50 150 Pg • Will Not Change The Number of Black Down Any Branch • Only Thing That Can Go Wrong Is Two Red Nodes In A Row 125 Pu Pf 225 200 P • Label P Pf  Pg  Pu

  26. 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 OnceStart With This Portion 50 150 Pg • THIS IS EASY CASE - Uncle Of P Is Red (125) 125 Pu Pf 200 225 • Make Pg REDMake Pf BLACKMake Pu BLACK P • This Shifts The Problem Up Two Generations!

  27. 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 RotationOne Requies A Preliminary Rotation And A Second Rotation

  28. Violation 3LBLACK UNCLE CaseWith One Rotation (On Left)

  29. Case 3L - BLACK UNCLE One Rotation Class- 1 (P On Left) a • This BLACK UNCLE Case Is Occurring As We Walk Our Way Up The Tree. 200 • We Have Already Established That The Distance Property Is OK 250 125 • ab = 1 e f 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.

  30. Case 3L - BLACK UNCLE One Rotation Class- 2 (P On Left) a a Pg 200 125 250 Pu 125 Pf 100 Pf • ab = 1 e f d • ac = 1 Pg 100 d 200 P • ad = 1 • ae = 2 P • af = 2 b c 250 Pu b c • Solve RED RED Problem? • Promote Pf Up • Hook up 'd' e f • 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!

  31. Case 3L Rule Summary a a Pg 200 125 Pf 250 Pu 125 Pf P Pg 100 200 e f d Pu b c 100 d 250 P b e c f

  32. Violation 3RBLACK UNCLE CaseWith One Rotation (On Right)

  33. Case 3R - BLACK UNCLE One Rotation Class- 1 (P On Right) a • This BLACK UNCLE Case Is Occurring As We Walk Our Way Up The Tree. 200 • We Have Already Established That The Distance Property Is OK 250 125 • ab = 2 d c b • ac = 2 300 • ad = 1 f e • ae = 1 • af = 1 • GOAL: Fix The RED CHILD With RED Father Problem Without Messing Up The Distance Properties.

  34. Case 3R - BLACK UNCLE One Rotation Class- 2 (P On Right) a a Pg 200 Pf 250 Pu 250 Pg 125 Pf P 300 200 • ab = 2 d e b c f 300 Pu P d • ac = 2 125 • ad = 1 • ae = 1 e f • af = 1 b c • Solve RED RED Problem? • Promote Pf Up • Hook up 'd' • Label P Pf  Pg  Pu • Swap Colors Pg Pf • Hook Pg Left of Pf • Double Check The Distances ab=2, ac=2, ad=1, ae=1, af=1  YEA!

  35. Case 3R Rule Summary a a Pg 200 Pf 250 Pg Pu 250 125 Pf P 300 200 d Pu d e b c f 300 P 125 e f b c

  36. Violation 2LBLACK UNCLE CaseWith Two Rotations (On Left)

  37. Case 2L - BLACK UNCLE Two Rotations Class- 1 (P On Left) a • This BLACK UNCLE Case Is Occurring As We Walk Our Way Up The Tree. Pg 200 • We Have Already Established That The Distance Property Is OK 250 Pu 125 Pf • ab = 1 b • ac = 1 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

  38. Case 2L - BLACK UNCLE Two Rotations Class- 2 (P On Left) 1 a a Pg 200 Pg 200 250 Pu 100 Pf 250 Pu 100 Pf • ab = 1 b e f P b 125 e • ac = 1 f c P 125 • ad = 1 • ae = 2 c • af = 2 d d • Hook P To The Left Of Pg • Might Become Easier If We Don't Try To Fix Everything At OnceStart With This Portion • Hook Pf To The Left Of P • Hook up 'c' • This Is Case 3L

  39. Case 2L - BLACK UNCLE Two Rotations Class- 3 (P On Left) 1 a Pg 200 250 Pf Pu 125 P 100 • ab = 1 b e • ac = 1 f c d • ad = 1 • ae = 2 • af = 2 • Swap Pointers Pf  P

  40. Case 2L - BLACK UNCLE Two Rotations Class- 4 (P On Left) 2 a a 125 Pg 200 250 Pf Pu 125 P 100 100 Pf • ab = 1 b e • ac = 1 f c d Pg d 200 • ad = 1 • ae = 2 P • af = 2 250 Pu • Swap Pointers Pf & P b c e f • Do Case 3L Rotation • Double Check The Distances ab=1, ac=1, ad=1, ae=2, af=2  YEA!

  41. Case 2L Rule Summary a a Pg 200 150 P 250 Pu 125 Pf Pg 125 Pf 200 b e f P 150 b c d 250 Pu c d e f

  42. Violation 2RBLACK UNCLE CaseWith Two Rotations (On Right)

  43. Case 2R- BLACK UNCLE Two Rotations Class- 1 (P On Right) a a • This BLACK UNCLE Case Is Occurring As We Walk Our Way Up The Tree. Pg 200 • We Have Already Established That The Distance Property Is OK Pu 300 125 Pf • ab = 2 • ac = 2 b c P 250 f • ad = 1 • ae = 1 d e • af = 1 • Case 2R is going to turn into Case 3R. • This Is The Worst Possible Case

  44. Case 2R - BLACK UNCLE Two Rotations Class- 2 (P On Right) 1 a a a Pg 200 Pg 200 Pu 300 125 125 Pu Pf P 250 • ab = 2 f P b c 250 b c • ac = 2 d 300 Pf • ad = 1 • ae = 1 d e • af = 1 e f • Might Become Easier If We Don't Try To Fix Everything At OnceStart With This Portion • Hook P To The Right Of Pg • Hook Pf To The Right Of P • Hook up 'e' • This Is Case 3L

  45. Case 2R - BLACK UNCLE Two Rotations Class- 3 (P On Right) a Pg 200 250 125 Pu Pf • ab = 2 b c d • ac = 2 P 300 • ad = 1 • ae = 1 • af = 1 e f • Swap Labels Pf  P • This Is Case 3L

  46. Case 2R - BLACK UNCLE Two Rotations Class- 4 (P On Right) 2 a a Pf 250 Pg 200 P 300 200 250 125 Pu Pf • ab = 2 e f 125 d b c d • ac = 2 P 300 • ad = 1 • ae = 1 b c • af = 1 e f • Swap Labels Pf  P • Do Case 3R Rotation • Double Check The Distances ab=2, ac=2, ad=1, ae=1, af=1  YEA!

  47. Case 2R Rule Summary a a a P 250 Pg 200 Pg Pf 300 200 Pu 300 Pf 125 e f 125 Pu d b c P 250 f b c d e

  48. Rotation GeneralizationRight Rotation

  49. Practice Problem#1

More Related