SlideShare a Scribd company logo
Ensuring tree height h = O(log n)

          Red Black Trees
Red Black Properties
• A binary search tree where
   – Every node is either “red” or “black”
      • For consistency we count “null” pointers as if they were
        “black” nodes. This means that an empty tree (one where the
        root is null) satisfies the definition (it’s black).
   – If a node is red, then both children are black
      • Another reason for “null” pointers being considered black
   – For any node, x, in the tree. All paths from x to a leaf
     have exactly the same number of black nodes.
Red/Black Tree has height O(log n)
• Let the “black-height” of a node x be the number of black nodes that
  are descendants of x.
• Then, for any node x, the subtree rooted at x has at least 2bh(x) -1 nodes.
    – Proof, by induction on the height of node x.
         • If the height of x is 0, then the bh is zero, and 20 -1 is 0. Of course, if the
           height of x is 0 then there are no nodes in the tree (x is an empty subtree).
         • If the height of x is k, and the bh of x is b (b < k), then consider each of the
           two children of x (either of which may be null). The black height of the child
           must be at least b – 1 (which happens if the child is black). By the inductive
           hypothesis, the subtree rooted at each child has 2b-1 -1 nodes. If we add up the
           number of nodes in both children, we have 2 * (2b-1 – 1), or 2b – 2. When we
           add in the node x we get 2b – 1. So the number of nodes in the subtree rooted
           at x is at least 2b-1
• Note that bh > h/2. So a tree with height h must have at least 2h/2-1
  nodes in it. i.e., 2h/2 -1 ≤ n.
• Therefore (taking the log base 2 of both sides) h < 2log2(n+1)
Huh? Doesn’t that work for any
              tree?
• That proof kinda stinks of the “let’s prove zero
  equals one” sort of proofs… in particular, it seems
  that the technique could be used to prove that all
  trees are balanced.
• The inductive proof relies on the black height
  being “well defined” for any node.
   – The height is defined as the longest path to a leaf
   – The black height is the same for all paths to a leaf.
• That’s why you cannot prove that any tree of
  height h has at least Ω(2h) nodes in it.
Making Red/Black Trees
• The basic idea of a Red/Black tree is to use
  the insert and remove operations of an
  ordinary binary search tree.
  – But this may result in violating the red/black
    properties
• So, we’ll “fixup” the tree after each
  insert/remove
Rotations
• A “right rotate” will interchange a node with its left child.
  The child will become the parent, and the parent will
  become a child.
   – The parent becomes the right child of the child.
       • The old “right grandchild” becomes the left child of the parent
• A “left rotate” will interchange a node with its right child
   – The parent becomes the left child of the child
       • The old “left grandchild” becomes the right child of the parent
• Note that these operations are exact opposites (inverses) of
  each other.
• Note also that Rotations do not affect the BST properties
  (although they will almost certainly affect the red/black
  properties).
Insert
• Insert the value normally, and make the new node
  “red”
   – We have not changed the black height of any node in
     the tree.
   – However, we may have created a red node with a red
     parent (and this is bad).
• As we “fixup” we’ll always have a pointer to a red
  node. We’ll always know that the black height is
  OK, and the only problem we need to worry about
  is that the parent of this red node is also red.
Fixup
• Let c (child) be the red node we inserted
• Let p (parent) be the parent of c
• Let gp (grandparent) be the parent of p
• Let u (uncle) be the child of gp that is not equal to
  p
• If p->color == black, we’re done. So, assume p-
  >color == red.
    – We know, therefore, that gp->color == black.
• Two interesting cases
    – Uncle is red (easy), or uncle is black (harder)
Uncle is red
• If the grandparent is black, the parent is red and
  the uncle is red, then
   – we would not change the black height by making the
     grandparent red and the parent (and uncle) black.
   – We may, however, have introduced a new problem
     where the grandparent is now red, and its parent is also
     red (the great-grandparent).
• So, if the uncle is red, make it and the parent
  black. Make the grandparent red, and then repeat
  fixup where we treat the grandparent as the next
  “child”.
Uncle is Black
• Make the parent black
   – But this increases the number of black nodes along the path to the
     child.
• Make the grandparent red
   – This fixes the problem with the path from the root to the child, but
     it decreases (breaks) the number of black nodes on the path from
     the root to the uncle
• Rotate around the grandparent and parent
   – So that the path from the root to the uncle now passes through both
     the parent and the grandparent
   – (and the path from the root to the child no longer passes through
     the grandparent).
Case Analysis
• Coding this up requires six cases. Three cases are for
  when the parent is the left child of the grandparent, and
  three (perfectly symmetric) cases for when the parent is the
  right child of the grandparent.
• Of the remaining three cases, “uncle is red” is one case.
• Two cases are required for “uncle is black” depending on
  whether the path from grandparent to child is “straight” or
  “crooked”
   – If the path is “crooked” then we’ll need to rotate first around the
     parent and child, and then perform the rotation around the parent
     and grandparent.
“Root is Black” Sentinel
• Once we reach the root of the tree, we’re done.
• If we can ensure that the root is always black, then
  we don’t need to worry about the special case of
  reaching the root in fixup.
   – Fixup stops whenever the next “child” is black, or when
     the parent is black.
• It’s easy (and always correct) to simply make the
  root black as the last step in any insert/remove
  operation.
Time Complexity
• Fixup runs in a loop. Each iteration of the loop we do
   – O(1) work in case analysis
   – O(1) work recoloring nodes (“uncle is red” case)
   – O(1) work performing rotations (at worst 2 rotations)
• Each iteration of the loop we either terminate (always the
  case after a rotation), or we set “child” equal to
  grandparent
   – i.e., each iteration of the loop uses a node with height less than the
     previous iteration.
• Since height must decrease each iteration, we can do at
  most h iterations. Since h = O(log n), we do O(log n)
  iterations with O(1) work each iteration.

More Related Content

Similar to Red blacktrees

Red black trees
Red black treesRed black trees
Red black trees
Amit Kumar Rathi
 
Red black tree
Red black treeRed black tree
Red black tree
vinitha96
 
4a searching-more
4a searching-more4a searching-more
4a searching-moreShahzad Ali
 
bst trees AVL trees-red black trees spay trees
bst trees AVL trees-red black trees spay treesbst trees AVL trees-red black trees spay trees
bst trees AVL trees-red black trees spay trees
somashekar60
 
Red black trees
Red black treesRed black trees
Red black trees
Maira Shahnawaz
 
Red black tree
Red black treeRed black tree
Red black tree
uos lahore
 
Red black trees
Red black treesRed black trees
Red black trees
TCHAYE Jude
 
Red black trees
Red black treesRed black trees
Red black trees
mumairsadiq
 
Data structure
Data structureData structure
Data structure
janani thirupathi
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
SeethaDinesh
 
Red black tree
Red black treeRed black tree
Red black tree
Dr Sandeep Kumar Poonia
 
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
Salini P
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
shameen khan
 
redblacktreeindatastructure-200409083949.pptx
redblacktreeindatastructure-200409083949.pptxredblacktreeindatastructure-200409083949.pptx
redblacktreeindatastructure-200409083949.pptx
RAtna29
 
Red black tree in data structure
Red black tree in data structureRed black tree in data structure
Red black tree in data structure
Vrushali Dhanokar
 
Heapsort
HeapsortHeapsort
Heapsort
Gopi Saiteja
 
BST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptx
BST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptxBST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptx
BST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptx
ssuser7b7f4e
 

Similar to Red blacktrees (20)

Lec14
Lec14Lec14
Lec14
 
Red black trees
Red black treesRed black trees
Red black trees
 
Red black tree
Red black treeRed black tree
Red black tree
 
4a searching-more
4a searching-more4a searching-more
4a searching-more
 
bst trees AVL trees-red black trees spay trees
bst trees AVL trees-red black trees spay treesbst trees AVL trees-red black trees spay trees
bst trees AVL trees-red black trees spay trees
 
Red black trees
Red black treesRed black trees
Red black trees
 
Red black tree
Red black treeRed black tree
Red black tree
 
Red black trees
Red black treesRed black trees
Red black trees
 
Red black trees
Red black treesRed black trees
Red black trees
 
Data structure
Data structureData structure
Data structure
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
Red black tree
Red black treeRed black tree
Red black tree
 
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
 
Binary tree
Binary treeBinary tree
Binary tree
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
redblacktreeindatastructure-200409083949.pptx
redblacktreeindatastructure-200409083949.pptxredblacktreeindatastructure-200409083949.pptx
redblacktreeindatastructure-200409083949.pptx
 
Red black tree in data structure
Red black tree in data structureRed black tree in data structure
Red black tree in data structure
 
Heapsort
HeapsortHeapsort
Heapsort
 
Heapsort
HeapsortHeapsort
Heapsort
 
BST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptx
BST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptxBST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptx
BST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptx
 

More from Core Condor

Graph isomorphism
Graph isomorphismGraph isomorphism
Graph isomorphismCore Condor
 

More from Core Condor (6)

Weighted graphs
Weighted graphsWeighted graphs
Weighted graphs
 
Red black 2
Red black 2Red black 2
Red black 2
 
Red black 1
Red black 1Red black 1
Red black 1
 
Graph isomorphism
Graph isomorphismGraph isomorphism
Graph isomorphism
 
Disjoint sets
Disjoint setsDisjoint sets
Disjoint sets
 
2 3 tree
2 3 tree2 3 tree
2 3 tree
 

Recently uploaded

The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
Kartik Tiwari
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 

Recently uploaded (20)

The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 

Red blacktrees

  • 1. Ensuring tree height h = O(log n) Red Black Trees
  • 2. Red Black Properties • A binary search tree where – Every node is either “red” or “black” • For consistency we count “null” pointers as if they were “black” nodes. This means that an empty tree (one where the root is null) satisfies the definition (it’s black). – If a node is red, then both children are black • Another reason for “null” pointers being considered black – For any node, x, in the tree. All paths from x to a leaf have exactly the same number of black nodes.
  • 3. Red/Black Tree has height O(log n) • Let the “black-height” of a node x be the number of black nodes that are descendants of x. • Then, for any node x, the subtree rooted at x has at least 2bh(x) -1 nodes. – Proof, by induction on the height of node x. • If the height of x is 0, then the bh is zero, and 20 -1 is 0. Of course, if the height of x is 0 then there are no nodes in the tree (x is an empty subtree). • If the height of x is k, and the bh of x is b (b < k), then consider each of the two children of x (either of which may be null). The black height of the child must be at least b – 1 (which happens if the child is black). By the inductive hypothesis, the subtree rooted at each child has 2b-1 -1 nodes. If we add up the number of nodes in both children, we have 2 * (2b-1 – 1), or 2b – 2. When we add in the node x we get 2b – 1. So the number of nodes in the subtree rooted at x is at least 2b-1 • Note that bh > h/2. So a tree with height h must have at least 2h/2-1 nodes in it. i.e., 2h/2 -1 ≤ n. • Therefore (taking the log base 2 of both sides) h < 2log2(n+1)
  • 4. Huh? Doesn’t that work for any tree? • That proof kinda stinks of the “let’s prove zero equals one” sort of proofs… in particular, it seems that the technique could be used to prove that all trees are balanced. • The inductive proof relies on the black height being “well defined” for any node. – The height is defined as the longest path to a leaf – The black height is the same for all paths to a leaf. • That’s why you cannot prove that any tree of height h has at least Ω(2h) nodes in it.
  • 5. Making Red/Black Trees • The basic idea of a Red/Black tree is to use the insert and remove operations of an ordinary binary search tree. – But this may result in violating the red/black properties • So, we’ll “fixup” the tree after each insert/remove
  • 6. Rotations • A “right rotate” will interchange a node with its left child. The child will become the parent, and the parent will become a child. – The parent becomes the right child of the child. • The old “right grandchild” becomes the left child of the parent • A “left rotate” will interchange a node with its right child – The parent becomes the left child of the child • The old “left grandchild” becomes the right child of the parent • Note that these operations are exact opposites (inverses) of each other. • Note also that Rotations do not affect the BST properties (although they will almost certainly affect the red/black properties).
  • 7. Insert • Insert the value normally, and make the new node “red” – We have not changed the black height of any node in the tree. – However, we may have created a red node with a red parent (and this is bad). • As we “fixup” we’ll always have a pointer to a red node. We’ll always know that the black height is OK, and the only problem we need to worry about is that the parent of this red node is also red.
  • 8. Fixup • Let c (child) be the red node we inserted • Let p (parent) be the parent of c • Let gp (grandparent) be the parent of p • Let u (uncle) be the child of gp that is not equal to p • If p->color == black, we’re done. So, assume p- >color == red. – We know, therefore, that gp->color == black. • Two interesting cases – Uncle is red (easy), or uncle is black (harder)
  • 9. Uncle is red • If the grandparent is black, the parent is red and the uncle is red, then – we would not change the black height by making the grandparent red and the parent (and uncle) black. – We may, however, have introduced a new problem where the grandparent is now red, and its parent is also red (the great-grandparent). • So, if the uncle is red, make it and the parent black. Make the grandparent red, and then repeat fixup where we treat the grandparent as the next “child”.
  • 10. Uncle is Black • Make the parent black – But this increases the number of black nodes along the path to the child. • Make the grandparent red – This fixes the problem with the path from the root to the child, but it decreases (breaks) the number of black nodes on the path from the root to the uncle • Rotate around the grandparent and parent – So that the path from the root to the uncle now passes through both the parent and the grandparent – (and the path from the root to the child no longer passes through the grandparent).
  • 11. Case Analysis • Coding this up requires six cases. Three cases are for when the parent is the left child of the grandparent, and three (perfectly symmetric) cases for when the parent is the right child of the grandparent. • Of the remaining three cases, “uncle is red” is one case. • Two cases are required for “uncle is black” depending on whether the path from grandparent to child is “straight” or “crooked” – If the path is “crooked” then we’ll need to rotate first around the parent and child, and then perform the rotation around the parent and grandparent.
  • 12. “Root is Black” Sentinel • Once we reach the root of the tree, we’re done. • If we can ensure that the root is always black, then we don’t need to worry about the special case of reaching the root in fixup. – Fixup stops whenever the next “child” is black, or when the parent is black. • It’s easy (and always correct) to simply make the root black as the last step in any insert/remove operation.
  • 13. Time Complexity • Fixup runs in a loop. Each iteration of the loop we do – O(1) work in case analysis – O(1) work recoloring nodes (“uncle is red” case) – O(1) work performing rotations (at worst 2 rotations) • Each iteration of the loop we either terminate (always the case after a rotation), or we set “child” equal to grandparent – i.e., each iteration of the loop uses a node with height less than the previous iteration. • Since height must decrease each iteration, we can do at most h iterations. Since h = O(log n), we do O(log n) iterations with O(1) work each iteration.