SlideShare une entreprise Scribd logo
1  sur  81
Red-Black Trees by Thomas A. Anastasio
A Red-Black Tree with NULLs shown Black-Height of the tree = 4
Red-Black Trees ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
A valid Red-Black Tree Black-Height = 2
[object Object],[object Object]
[object Object],[object Object]
[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Bottom –Up Insertion ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Bottom Up Insertion ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
X P G U P G U Case 1 – U is Red Just Recolor and move up X
X P G U S X P G S U Case 2 – Zig-Zag Double Rotate   X around P; X around G Recolor G and X
X P G U S P X G S U Case 3 – Zig-Zig Single Rotate P around G Recolor P and G
11 14 15 2 1 7 5 8 Black node Red node Insert 4 into this R-B Tree
Insertion Practice ,[object Object]
Asymptotic Cost of Insertion ,[object Object],[object Object],[object Object],[object Object]
Red-Black Trees Bottom-Up Deletion
Recall “ordinary” BST Delete ,[object Object],[object Object],[object Object]
Bottom-Up Deletion ,[object Object],[object Object]
Which RB Property may be violated after deletion? ,[object Object],[object Object]
Fixing the problem ,[object Object],[object Object]
Terminology ,[object Object],[object Object],[object Object],[object Object],Black Node Red Node Red or Black and don’t care
Bottom-Up Deletion Case 1 ,[object Object],[object Object],[object Object],[object Object]
Case 1 Diagram P S V+ P S V+ Rotate P V+ S Recolor
Bottom-Up Deletion Case 2 ,[object Object],[object Object],[object Object],[object Object],[object Object]
Case 2 diagram P S V+ P+ S V Recolor and absorb Either extra black absorbed by P or P now has extra blackness
Bottom-Up Deletion Case 3 ,[object Object],[object Object],[object Object],[object Object],[object Object]
Case 3 diagrams P S V+ P S V Rotate P S V Recolor
Bottom-Up Deletion Case 4 ,[object Object],[object Object],[object Object],[object Object]
Case 4 Diagrams P S V+ P S V+ Rotate P S V+ Recolor
65 50 80 10 60 70 90 62 Perform the following deletions, in the order specified Delete 90, Delete 80, Delete 70
Red Black Trees Top-Down Insertion
Review of Bottom-Up Insertion ,[object Object],[object Object],[object Object],[object Object]
Top-Down Insertion Strategy ,[object Object],[object Object],[object Object]
Goal of T-D Insertion ,[object Object],[object Object]
Goal (2) ,[object Object],[object Object]
Possible insertion configurations X (Red or Black) Y Z If a new node is inserted as a child of Y or Z, there is no problem since the new node’s parent is black
Possible insertion configurations X Y Z If new node is child of Z, no problem since Z is black. If new node is child of Y, no problem since the new node’s uncle (Z) is black – do a few rotations and recolor…. done
Possible insertion configurations X Y Z If new node is inserted as child of Y or Z, it’s uncle will be red and we will have to go back up the tree.  This is the only case we need to avoid.
Top-Down Traversal X Y Z As we traverse down the tree and encounter this case, we recolor and possible do some rotations. There are 3 cases. Remember the goal – to create an insertion point at which the parent of the new node is Black, or the uncle of the new node is black.
Case 1 – X’s Parent is Black X Z Y P X Z P Just recolor and continue down the tree Y
Case 2 ,[object Object],[object Object],[object Object],[object Object],[object Object]
Case 2 diagrams X Z Y P G U S X Z Y P G U S Rotate  P around G.  Recolor X, Y, Z, P and G
Case 3 ,[object Object],[object Object],[object Object],[object Object],[object Object]
Case 3 Diagrams (1 of 2) X Z Y P G U S X Y S P G U Z Step 1 – recolor X, Y and Z. Rotate X around P.
Case 3 Diagrams (2 of 2) X Y S P G U Z P Y S X G U Z Step 2 – Rotate X around G.  Recolor X and G
An exercise – insert F D T W Z V L J P E K
Top-Down Insert Summary P X Y Z Case 1 P is Black Just Recolor P X Y Z Case 2 P is Red X & P both left/right P X Y Z G P X Y Z G Recolor X,Y,Z P X Y Z G Rotate P around G Recolor P,G Case 3 P is Red X and P are opposite children P X Y Z G Recolor X,Y,Z Rotate X around P X P Y Z G Rotate X around G Recolor X, G Recolor  X,Y,Z X P Y Z G
Red Black Trees Top-Down Deletion
Recall the rules for BST deletion ,[object Object],[object Object],[object Object]
What can go wrong? ,[object Object],[object Object]
The goal of T-D Deletion ,[object Object],[object Object],[object Object],[object Object]
Bottom-Up vs. Top-Down ,[object Object],[object Object],[object Object],[object Object],[object Object]
Terminology ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Basic Strategy ,[object Object],[object Object],[object Object],[object Object]
Step 1 – Examine the root ,[object Object],[object Object],[object Object],[object Object],[object Object]
Step 2 – the main case ,[object Object],[object Object],[object Object],[object Object],[object Object]
Case 2A X has two Black Children P T X 2A1. T has 2 Black Children 2A2. T’s left child is Red 2A3. T’s right child is Red ** if both of T’s children are Red, we can do either 2A2 or 2A3
Case 2A1 X and T have 2 Black Children P T X P T X Just recolor X, P and T and move down the tree
Case 2A2 P T X L X has 2 Black Children and T’s Left Child is Red Rotate L around T, then L around P Recolor X and P then continue down the tree L1 L2 P T X L L1 L2
Case 2A3 P T X X has 2 Black Children and T’s Right Child is Red Rotate T around P Recolor X, P, T and R then continue down the tree R1 R2 P R X T R2 R1 R L L
Case 2B X has at least one Red child  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Case 2B Diagram P X T Move down the tree.  P X T P T X If move to Black child (2B2) Rotate T around P; Recolor P and T Back to step 2, the main case If move to the Red child (2B1) Move down again
Step 3 Eventually, find the node to be deleted – a leaf or a node with one non-null child that is a leaf.  Delete the appropriate node as a Red leaf Step 4 Color the Root Black
Example 1 Delete 10 from this RB Tree 15 17 16 20 23 18 13 10 7 12 6 3 Step 1 – Root has 2 Black children.  Color Root Red Descend the tree, moving X to 6
Example 1 (cont’d) 15 17 16 20 23 18 13 10 7 12 6 3 One of X’s children is Red (case 2B).  Descend down the tree, arriving at 12. Since the new X (12) is also Red (2B1), continue down the tree, arriving at 10.  X
Example 1 (cont’d) 15 17 16 20 23 18 13 10 7 12 6 3 Step 3 -Since 10 is the node to be deleted, replace it’s value with the value of it’s only child (7) and delete 7’s red node X
Example 1 (cont’d) 15 17 16 20 23 18 13 7 12 6 3 The final tree after 7 has replaced 10 and 7’s red node deleted and (step 4) the root has been colored Black.
Example 2 Delete 10 from this RB Tree 15 17 16 20 13 10 12 6 3 4 2 Step 1 – the root does not have 2 Black children. Color the root red, Set X = root and proceed to step 2
Example 2 (cont’d) 15 17 16 20 13 10 12 6 3 4 2 X X has at least one Red child (case 2B).  Proceed down the tree, arriving at 6.  Since 6 is also Red (case 2B1), continue down the tree, arriving at 12.
Example 2 (cont’d) 15 17 16 20 13 10 12 6 3 4 2 X X has 2 Black children. X’s sibling (3) also has 2 black children. Case 2A1– recolor X, P, and T and continue down the tree, arriving at 10. P T
Example 2 (cont’d) 15 17 16 20 13 10 12 6 3 4 2 P X T X is now the leaf to be deleted, but it’s Black, so back to step 2. X has 2 Black children and T has 2 Black children – case 2A1 Recolor X, P and T.  Step 3 -- Now delete 10 as a red leaf. Step 4 -- Recolor the root black
Example 2 Solution 15 17 16 20 13 12 6 3 4 2
Example 3 Delete 11 from this RB Tree 15 13 11 12 10 5 7 3 6 9 2 4 Valid and unaffected Right subtree Step 1 – root has 2 Black children.  Color Root red. Set X to appropriate child of root (10)
Example 3 (cont’d) 15 13 11 12 10 5 7 3 6 9 2 4 X X has one Red child (case 2B) Traverse down the tree, arriving at 12.
Example 3 (cont’d) 15 13 11 12 10 5 7 3 6 9 4 X Since we arrived at a black node (case 2B2) assuring T is red and P is black), rotate T around P, recolor T and P Back to step 2 P T 2
Example 3 (cont’d) 15 13 11 12 10 5 7 3 6 9 4 X P T 2 Now X is Black with Red parent and Black sibling. X and T both have 2 Black children (case 2A1) Just recolor X, P and T and continue traversal
Example 3 (cont’d) 15 13 11 12 10 5 7 3 6 9 4 X P T 2 Having traversed down the tree, we arrive at 11, the leaf to be deleted, but it’s Black, so back to step 2. X and T both have two Black children.  Recolor X, P and T. Step 3 -- delete 11 as a red leaf.  Step 4 -- Recolor the root black
Example 3 Solution 13 12 10 5 7 3 6 9 4 2 15

Contenu connexe

Tendances (20)

Red black tree
Red black treeRed black tree
Red black tree
 
Red black tree
Red black treeRed black tree
Red black tree
 
Database design & Normalization (1NF, 2NF, 3NF)
Database design & Normalization (1NF, 2NF, 3NF)Database design & Normalization (1NF, 2NF, 3NF)
Database design & Normalization (1NF, 2NF, 3NF)
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Stack using Linked List
Stack using Linked ListStack using Linked List
Stack using Linked List
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Expression trees
Expression treesExpression trees
Expression trees
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
single linked list
single linked listsingle linked list
single linked list
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
stack
stackstack
stack
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Relational Algebra,Types of join
Relational Algebra,Types of joinRelational Algebra,Types of join
Relational Algebra,Types of join
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Stacks
StacksStacks
Stacks
 
Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
 

Similaire à Red Black Trees

Similaire à Red Black Trees (20)

anastasio-red-black-trees-1-1-091222204455-phpapp02.pdf
anastasio-red-black-trees-1-1-091222204455-phpapp02.pdfanastasio-red-black-trees-1-1-091222204455-phpapp02.pdf
anastasio-red-black-trees-1-1-091222204455-phpapp02.pdf
 
lecture 14
lecture 14lecture 14
lecture 14
 
Advanced data structures and implementation
Advanced data structures and implementationAdvanced data structures and implementation
Advanced data structures and implementation
 
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.pptUnit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
 
Lec14
Lec14Lec14
Lec14
 
16 rbtrees
16 rbtrees16 rbtrees
16 rbtrees
 
Red black trees
Red black treesRed black trees
Red black trees
 
Data structure
Data structureData structure
Data structure
 
Red blacktrees
Red blacktreesRed blacktrees
Red blacktrees
 
Red black 1
Red black 1Red black 1
Red black 1
 
rbtrees.ppt
rbtrees.pptrbtrees.ppt
rbtrees.ppt
 
Red black-trees-4
Red black-trees-4Red black-trees-4
Red black-trees-4
 
RED BLACK TREE ADSA UNIT 1 ROTAITON RED B
RED BLACK TREE ADSA UNIT 1 ROTAITON RED BRED BLACK TREE ADSA UNIT 1 ROTAITON RED B
RED BLACK TREE ADSA UNIT 1 ROTAITON RED B
 
Red black tree
Red black treeRed black tree
Red black tree
 
Balanced Tree(AVL Tree,Red Black Tree)
Balanced Tree(AVL Tree,Red Black Tree)Balanced Tree(AVL Tree,Red Black Tree)
Balanced Tree(AVL Tree,Red Black Tree)
 
Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
 
Red Black Tree
Red Black TreeRed Black Tree
Red Black Tree
 
void insertFixUp (Node node) This method recolors and rotates the tree.pdf
void insertFixUp (Node node) This method recolors and rotates the tree.pdfvoid insertFixUp (Node node) This method recolors and rotates the tree.pdf
void insertFixUp (Node node) This method recolors and rotates the tree.pdf
 
Red black trees
Red black treesRed black trees
Red black trees
 
Cse 225 rbt_red_black_tree
Cse 225 rbt_red_black_treeCse 225 rbt_red_black_tree
Cse 225 rbt_red_black_tree
 

Dernier

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 

Dernier (20)

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 

Red Black Trees

  • 1. Red-Black Trees by Thomas A. Anastasio
  • 2. A Red-Black Tree with NULLs shown Black-Height of the tree = 4
  • 3.
  • 4. A valid Red-Black Tree Black-Height = 2
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13. X P G U P G U Case 1 – U is Red Just Recolor and move up X
  • 14. X P G U S X P G S U Case 2 – Zig-Zag Double Rotate X around P; X around G Recolor G and X
  • 15. X P G U S P X G S U Case 3 – Zig-Zig Single Rotate P around G Recolor P and G
  • 16. 11 14 15 2 1 7 5 8 Black node Red node Insert 4 into this R-B Tree
  • 17.
  • 18.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26. Case 1 Diagram P S V+ P S V+ Rotate P V+ S Recolor
  • 27.
  • 28. Case 2 diagram P S V+ P+ S V Recolor and absorb Either extra black absorbed by P or P now has extra blackness
  • 29.
  • 30. Case 3 diagrams P S V+ P S V Rotate P S V Recolor
  • 31.
  • 32. Case 4 Diagrams P S V+ P S V+ Rotate P S V+ Recolor
  • 33. 65 50 80 10 60 70 90 62 Perform the following deletions, in the order specified Delete 90, Delete 80, Delete 70
  • 34. Red Black Trees Top-Down Insertion
  • 35.
  • 36.
  • 37.
  • 38.
  • 39. Possible insertion configurations X (Red or Black) Y Z If a new node is inserted as a child of Y or Z, there is no problem since the new node’s parent is black
  • 40. Possible insertion configurations X Y Z If new node is child of Z, no problem since Z is black. If new node is child of Y, no problem since the new node’s uncle (Z) is black – do a few rotations and recolor…. done
  • 41. Possible insertion configurations X Y Z If new node is inserted as child of Y or Z, it’s uncle will be red and we will have to go back up the tree. This is the only case we need to avoid.
  • 42. Top-Down Traversal X Y Z As we traverse down the tree and encounter this case, we recolor and possible do some rotations. There are 3 cases. Remember the goal – to create an insertion point at which the parent of the new node is Black, or the uncle of the new node is black.
  • 43. Case 1 – X’s Parent is Black X Z Y P X Z P Just recolor and continue down the tree Y
  • 44.
  • 45. Case 2 diagrams X Z Y P G U S X Z Y P G U S Rotate P around G. Recolor X, Y, Z, P and G
  • 46.
  • 47. Case 3 Diagrams (1 of 2) X Z Y P G U S X Y S P G U Z Step 1 – recolor X, Y and Z. Rotate X around P.
  • 48. Case 3 Diagrams (2 of 2) X Y S P G U Z P Y S X G U Z Step 2 – Rotate X around G. Recolor X and G
  • 49. An exercise – insert F D T W Z V L J P E K
  • 50. Top-Down Insert Summary P X Y Z Case 1 P is Black Just Recolor P X Y Z Case 2 P is Red X & P both left/right P X Y Z G P X Y Z G Recolor X,Y,Z P X Y Z G Rotate P around G Recolor P,G Case 3 P is Red X and P are opposite children P X Y Z G Recolor X,Y,Z Rotate X around P X P Y Z G Rotate X around G Recolor X, G Recolor X,Y,Z X P Y Z G
  • 51. Red Black Trees Top-Down Deletion
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60. Case 2A X has two Black Children P T X 2A1. T has 2 Black Children 2A2. T’s left child is Red 2A3. T’s right child is Red ** if both of T’s children are Red, we can do either 2A2 or 2A3
  • 61. Case 2A1 X and T have 2 Black Children P T X P T X Just recolor X, P and T and move down the tree
  • 62. Case 2A2 P T X L X has 2 Black Children and T’s Left Child is Red Rotate L around T, then L around P Recolor X and P then continue down the tree L1 L2 P T X L L1 L2
  • 63. Case 2A3 P T X X has 2 Black Children and T’s Right Child is Red Rotate T around P Recolor X, P, T and R then continue down the tree R1 R2 P R X T R2 R1 R L L
  • 64.
  • 65. Case 2B Diagram P X T Move down the tree. P X T P T X If move to Black child (2B2) Rotate T around P; Recolor P and T Back to step 2, the main case If move to the Red child (2B1) Move down again
  • 66. Step 3 Eventually, find the node to be deleted – a leaf or a node with one non-null child that is a leaf. Delete the appropriate node as a Red leaf Step 4 Color the Root Black
  • 67. Example 1 Delete 10 from this RB Tree 15 17 16 20 23 18 13 10 7 12 6 3 Step 1 – Root has 2 Black children. Color Root Red Descend the tree, moving X to 6
  • 68. Example 1 (cont’d) 15 17 16 20 23 18 13 10 7 12 6 3 One of X’s children is Red (case 2B). Descend down the tree, arriving at 12. Since the new X (12) is also Red (2B1), continue down the tree, arriving at 10. X
  • 69. Example 1 (cont’d) 15 17 16 20 23 18 13 10 7 12 6 3 Step 3 -Since 10 is the node to be deleted, replace it’s value with the value of it’s only child (7) and delete 7’s red node X
  • 70. Example 1 (cont’d) 15 17 16 20 23 18 13 7 12 6 3 The final tree after 7 has replaced 10 and 7’s red node deleted and (step 4) the root has been colored Black.
  • 71. Example 2 Delete 10 from this RB Tree 15 17 16 20 13 10 12 6 3 4 2 Step 1 – the root does not have 2 Black children. Color the root red, Set X = root and proceed to step 2
  • 72. Example 2 (cont’d) 15 17 16 20 13 10 12 6 3 4 2 X X has at least one Red child (case 2B). Proceed down the tree, arriving at 6. Since 6 is also Red (case 2B1), continue down the tree, arriving at 12.
  • 73. Example 2 (cont’d) 15 17 16 20 13 10 12 6 3 4 2 X X has 2 Black children. X’s sibling (3) also has 2 black children. Case 2A1– recolor X, P, and T and continue down the tree, arriving at 10. P T
  • 74. Example 2 (cont’d) 15 17 16 20 13 10 12 6 3 4 2 P X T X is now the leaf to be deleted, but it’s Black, so back to step 2. X has 2 Black children and T has 2 Black children – case 2A1 Recolor X, P and T. Step 3 -- Now delete 10 as a red leaf. Step 4 -- Recolor the root black
  • 75. Example 2 Solution 15 17 16 20 13 12 6 3 4 2
  • 76. Example 3 Delete 11 from this RB Tree 15 13 11 12 10 5 7 3 6 9 2 4 Valid and unaffected Right subtree Step 1 – root has 2 Black children. Color Root red. Set X to appropriate child of root (10)
  • 77. Example 3 (cont’d) 15 13 11 12 10 5 7 3 6 9 2 4 X X has one Red child (case 2B) Traverse down the tree, arriving at 12.
  • 78. Example 3 (cont’d) 15 13 11 12 10 5 7 3 6 9 4 X Since we arrived at a black node (case 2B2) assuring T is red and P is black), rotate T around P, recolor T and P Back to step 2 P T 2
  • 79. Example 3 (cont’d) 15 13 11 12 10 5 7 3 6 9 4 X P T 2 Now X is Black with Red parent and Black sibling. X and T both have 2 Black children (case 2A1) Just recolor X, P and T and continue traversal
  • 80. Example 3 (cont’d) 15 13 11 12 10 5 7 3 6 9 4 X P T 2 Having traversed down the tree, we arrive at 11, the leaf to be deleted, but it’s Black, so back to step 2. X and T both have two Black children. Recolor X, P and T. Step 3 -- delete 11 as a red leaf. Step 4 -- Recolor the root black
  • 81. Example 3 Solution 13 12 10 5 7 3 6 9 4 2 15