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

PDF
Fast algorithms for large scale genome alignment and comparison
PDF
Red black trees
PPT
16 rbtrees
PPT
Red black tree
PPT
rbtrees.ppt
PDF
anastasio-red-black-trees-1-1-091222204455-phpapp02.pdf
PPTX
Data structure
PDF
Red black tree
Fast algorithms for large scale genome alignment and comparison
Red black trees
16 rbtrees
Red black tree
rbtrees.ppt
anastasio-red-black-trees-1-1-091222204455-phpapp02.pdf
Data structure
Red black tree

Similar to Red blacktrees (20)

PPT
PPT
lecture 14
PPT
Red Black Trees
PDF
Red black tree
PPT
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
PPT
RedBlackTrees_2.pptNNNNNNNNNNNNNNNNNNNNNN
PPT
RedBlackTrees_2.pptmmmmmmmmmmmmmmmmmmmmmmmmmm
PPT
lecture 13
PPTX
Red black trees
PPTX
Red black trees
PPT
Red-black_trees.ppt by shivam sharma ofo
PPT
operations insertion Red-black_treesver1.ppt
PDF
Red-Black Tree Presentation By Mobin Nesari.pdf
PDF
docsity-red-black-trees-data-structures-lecture-slides_2.pdf
PPTX
Red black trees1109
PPTX
Red-black-tree presentation in Algorithm
PPTX
Red black trees
PDF
Cse 225 rbt_red_black_tree
PPTX
red black tree.pptxMMMMMMMMMMMMMMMMMMMMMMMMMM
PPT
Advanced data structures and implementation
lecture 14
Red Black Trees
Red black tree
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
RedBlackTrees_2.pptNNNNNNNNNNNNNNNNNNNNNN
RedBlackTrees_2.pptmmmmmmmmmmmmmmmmmmmmmmmmmm
lecture 13
Red black trees
Red black trees
Red-black_trees.ppt by shivam sharma ofo
operations insertion Red-black_treesver1.ppt
Red-Black Tree Presentation By Mobin Nesari.pdf
docsity-red-black-trees-data-structures-lecture-slides_2.pdf
Red black trees1109
Red-black-tree presentation in Algorithm
Red black trees
Cse 225 rbt_red_black_tree
red black tree.pptxMMMMMMMMMMMMMMMMMMMMMMMMMM
Advanced data structures and implementation
Ad

More from Core Condor (6)

PPT
Weighted graphs
PPT
Red black 2
PPT
Red black 1
PPT
Graph isomorphism
PPT
Disjoint sets
PPT
2 3 tree
Weighted graphs
Red black 2
Red black 1
Graph isomorphism
Disjoint sets
2 3 tree
Ad

Recently uploaded (20)

PDF
Complications of Minimal Access Surgery at WLH
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Institutional Correction lecture only . . .
PDF
Classroom Observation Tools for Teachers
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Pre independence Education in Inndia.pdf
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Complications of Minimal Access Surgery at WLH
STATICS OF THE RIGID BODIES Hibbelers.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Insiders guide to clinical Medicine.pdf
01-Introduction-to-Information-Management.pdf
Institutional Correction lecture only . . .
Classroom Observation Tools for Teachers
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
human mycosis Human fungal infections are called human mycosis..pptx
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Pre independence Education in Inndia.pdf
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
VCE English Exam - Section C Student Revision Booklet
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
FourierSeries-QuestionsWithAnswers(Part-A).pdf

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.