SlideShare a Scribd company logo
1 of 29
Red-Black Trees—Again


• rank(x) = # black pointers on path from x to an
  external node.
• Same as #black nodes (excluding x) from x to an
  external node.
• rank(external node) = 0.
An Example
                                                  3
                                            10

                                                                             2
                    2   7                                               40

        1   3               1   8                     2 30                       1   45



1                                            1                 1   35                     1 60
    1           1   5                            20
                            0       0                                            0
                                                      1   25
                                                                   0    0             0          0
0       0 0             0               0
                                                 0                 0
Properties Of rank(x)

•   rank(x) = 0 for x an external node.
•   rank(x) = 1 for x parent of external node.
•   p(x) exists => rank(x) <= rank(p(x)) <= rank(x) + 1.
•   g(x) exists => rank(x) < rank(g(x)).
Red-Black Tree

A binary search tree is a red-black tree iff
 integer ranks can be assigned to its nodes so
 as to satisfy the stated 4 properties of rank.
Relationship Between rank() And Color

•   (p(x),x) is a red pointer iff rank(x) = rank(p(x)).
•   (p(x),x) is a black pointer iff rank(x) = rank(p(x)) – 1.
•   Red node iff pointer from parent is red.
•   Root is black.
•   Other nodes are black iff pointer from parent is black.
•   Given rank(root) and node/pointer colors, remaining
    ranks may be computed on way down.
rank(root)

• Height <= 2 * rank(root).
• No external nodes at levels 1, 2, …, rank(root).
   So, #nodes >= 1 <= i <= rank(root) 2i -1 = 2 rank(root) – 1.
   So, rank(root) <= log2(n+1).
• So, height(root) <= 2log2(n+1).
Join(S,m,B)
• Input
     Dictionary S of pairs with small keys.
     Dictionary B of pairs with big keys.
     An additional pair m.
     All keys in S are smaller than m.key.
     All keys in B are bigger than m.key.
• Output
   A dictionary that contains all pairs in S and B
    plus the pair m.
   Dictionaries S and B may be destroyed.
Join Binary Search Trees

                   m


               S       B




• O(1) time.
Join Red-black Trees

• When rank(S) = rank(B), use binary search tree
  method.

                       m


                  S          B

• rank(root) = rank(S) + 1 = rank(B) + 1.
rank(S) > rank(B)
• Follow right child pointers from root of S to first node
  x whose rank equals rank(B).


                                       p(x)
        p(x)

                                                 m
   S               x
                                           x          B
               a       b
                                       a        b
rank(S) > rank(B)
• If there are now 2 consecutive red pointers/nodes,
  perform bottom-up rebalancing beginning at m.
• O(rank(S) – rank(B)).

                                   p(x)
        p(x)
                                            m
   S               x
                                       x        B
               a       b
                                   a       b
rank(S) < rank(B)



• Follow left child pointers from root of B to first
  node x whose rank equals rank(B).
• Similar to case when rank(S) > rank(B).
Split(k)

• Inverse of join.
• Obtain
   S … dictionary of pairs with key < k.
   B … dictionary of pairs with key > k.
   m … pair with key = k (if present).
Split A Binary Search Tree
        A              S                 B

a           B
        C       b
    c       D
        E       d
    e       m
        f       g
Split A Binary Search Tree
                        S            B

        B           A
    C       b   a
c       D
    E       d
e       m
    f       g
Split A Binary Search Tree
                        S              B

                    A          B
    C           a                  b
c       D
    E       d
e       m
    f       g
Split A Binary Search Tree
                        S              B

                    A          B
                a       C          b
        D
                    c
    E       d
e       m
    f       g
Split A Binary Search Tree
                        S                   B

                    A               B
                a       C   D           b

                    c           d
    E
e       m
    f       g
Split A Binary Search Tree
                    S                       B

                A                   B
            a       C       D           b

                c       E       d

                    e
    m
f       g
Split A Binary Search Tree
               S                               B

           A                           B
       a       C               D           b

           c       E       g       d

               e       f
m
Split A Red-Black Tree

• Previous strategy does not split a red-black
  tree into two red-black trees.
• Must do a search for m followed by a
  traceback to the root.
• During the traceback use the join operation
  to construct S and B.
Split A Red-Black Tree
        A             S=f       B=g
a           B
        C       b
    c       D
        E       d
    e       m
        f       g
Split A Red-Black Tree
        A             S=f       B=g
a           B
        C       b
    c       D
        E       d
    e
Split A Red-Black Tree
        A             S=f           B=g
a           B          S = join(e, E, S)
        C       b
    c       D
        E       d
    e
Split A Red-Black Tree
    A                 S=f           B =g
a           B          S = join(e, E, S)
        C       b      B = join(B, D, d)
    c       D
                d
Split A Red-Black Tree
    A                 S=f           B =g
a           B          S = join(e, E, S)
        C       b      B = join(B, D, d)
    c                  S = join(c, C, S)
Split A Red-Black Tree
    A             S=f           B =g
a       B          S = join(e, E, S)
            b      B = join(B, D, d)
                   S = join(c, C, S)
                   B = join(B, B, b)
Split A Red-Black Tree
    A         S=f           B =g
a              S = join(e, E, S)
               B = join(B, D, d)
               S = join(c, C, S)
               B = join(B, B, b)

               S = join(a, A, S)
Complexity Of Split

• O(log n)
• See text.

More Related Content

What's hot (20)

Red black tree
Red black treeRed black tree
Red black tree
 
B+ tree.pptx
B+ tree.pptxB+ tree.pptx
B+ tree.pptx
 
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
 
Merge sort
Merge sortMerge sort
Merge sort
 
Lecture 6 disjoint set
Lecture 6 disjoint setLecture 6 disjoint set
Lecture 6 disjoint set
 
Multi ways trees
Multi ways treesMulti ways trees
Multi ways trees
 
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtracking
 
Covering Indexes Ordersof Magnitude Improvements
Covering  Indexes  Ordersof Magnitude  ImprovementsCovering  Indexes  Ordersof Magnitude  Improvements
Covering Indexes Ordersof Magnitude Improvements
 
Fibonacci Heap
Fibonacci HeapFibonacci Heap
Fibonacci Heap
 
Splay Tree
Splay TreeSplay Tree
Splay Tree
 
joins in database
 joins in database joins in database
joins in database
 
Normalization in databases
Normalization in databasesNormalization in databases
Normalization in databases
 
Big Challenges in Data Modeling: Supertyping and Subtyping
Big Challenges in Data Modeling: Supertyping and SubtypingBig Challenges in Data Modeling: Supertyping and Subtyping
Big Challenges in Data Modeling: Supertyping and Subtyping
 
Application of greedy method
Application  of  greedy methodApplication  of  greedy method
Application of greedy method
 
月刊NDEF 2013年8月号
月刊NDEF 2013年8月号月刊NDEF 2013年8月号
月刊NDEF 2013年8月号
 
B trees
B treesB trees
B trees
 
B tree
B treeB tree
B tree
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
 
5.3 dynamic programming
5.3 dynamic programming5.3 dynamic programming
5.3 dynamic programming
 

Similar to Red black 2

Similar to Red black 2 (9)

DS MCQS.pptx
DS MCQS.pptxDS MCQS.pptx
DS MCQS.pptx
 
2 depth first
2 depth first2 depth first
2 depth first
 
Lec21
Lec21Lec21
Lec21
 
09 binary-trees
09 binary-trees09 binary-trees
09 binary-trees
 
(Binary tree)
(Binary tree)(Binary tree)
(Binary tree)
 
Roots of polynomials
Roots of polynomialsRoots of polynomials
Roots of polynomials
 
Mock 1
Mock 1Mock 1
Mock 1
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 
Modul penggunaan kalkulator sainstifik sebagai ABM dalam Matematik
Modul penggunaan kalkulator sainstifik sebagai ABM dalam MatematikModul penggunaan kalkulator sainstifik sebagai ABM dalam Matematik
Modul penggunaan kalkulator sainstifik sebagai ABM dalam Matematik
 

More from Core Condor

More from Core Condor (6)

Weighted graphs
Weighted graphsWeighted graphs
Weighted graphs
 
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
 
Red blacktrees
Red blacktreesRed blacktrees
Red blacktrees
 
2 3 tree
2 3 tree2 3 tree
2 3 tree
 

Recently uploaded

FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 

Recently uploaded (20)

FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 

Red black 2

  • 1. Red-Black Trees—Again • rank(x) = # black pointers on path from x to an external node. • Same as #black nodes (excluding x) from x to an external node. • rank(external node) = 0.
  • 2. An Example 3 10 2 2 7 40 1 3 1 8 2 30 1 45 1 1 1 35 1 60 1 1 5 20 0 0 0 1 25 0 0 0 0 0 0 0 0 0 0 0
  • 3. Properties Of rank(x) • rank(x) = 0 for x an external node. • rank(x) = 1 for x parent of external node. • p(x) exists => rank(x) <= rank(p(x)) <= rank(x) + 1. • g(x) exists => rank(x) < rank(g(x)).
  • 4. Red-Black Tree A binary search tree is a red-black tree iff integer ranks can be assigned to its nodes so as to satisfy the stated 4 properties of rank.
  • 5. Relationship Between rank() And Color • (p(x),x) is a red pointer iff rank(x) = rank(p(x)). • (p(x),x) is a black pointer iff rank(x) = rank(p(x)) – 1. • Red node iff pointer from parent is red. • Root is black. • Other nodes are black iff pointer from parent is black. • Given rank(root) and node/pointer colors, remaining ranks may be computed on way down.
  • 6. rank(root) • Height <= 2 * rank(root). • No external nodes at levels 1, 2, …, rank(root).  So, #nodes >= 1 <= i <= rank(root) 2i -1 = 2 rank(root) – 1.  So, rank(root) <= log2(n+1). • So, height(root) <= 2log2(n+1).
  • 7. Join(S,m,B) • Input  Dictionary S of pairs with small keys.  Dictionary B of pairs with big keys.  An additional pair m.  All keys in S are smaller than m.key.  All keys in B are bigger than m.key. • Output  A dictionary that contains all pairs in S and B plus the pair m.  Dictionaries S and B may be destroyed.
  • 8. Join Binary Search Trees m S B • O(1) time.
  • 9. Join Red-black Trees • When rank(S) = rank(B), use binary search tree method. m S B • rank(root) = rank(S) + 1 = rank(B) + 1.
  • 10. rank(S) > rank(B) • Follow right child pointers from root of S to first node x whose rank equals rank(B). p(x) p(x) m S x x B a b a b
  • 11. rank(S) > rank(B) • If there are now 2 consecutive red pointers/nodes, perform bottom-up rebalancing beginning at m. • O(rank(S) – rank(B)). p(x) p(x) m S x x B a b a b
  • 12. rank(S) < rank(B) • Follow left child pointers from root of B to first node x whose rank equals rank(B). • Similar to case when rank(S) > rank(B).
  • 13. Split(k) • Inverse of join. • Obtain  S … dictionary of pairs with key < k.  B … dictionary of pairs with key > k.  m … pair with key = k (if present).
  • 14. Split A Binary Search Tree A S B a B C b c D E d e m f g
  • 15. Split A Binary Search Tree S B B A C b a c D E d e m f g
  • 16. Split A Binary Search Tree S B A B C a b c D E d e m f g
  • 17. Split A Binary Search Tree S B A B a C b D c E d e m f g
  • 18. Split A Binary Search Tree S B A B a C D b c d E e m f g
  • 19. Split A Binary Search Tree S B A B a C D b c E d e m f g
  • 20. Split A Binary Search Tree S B A B a C D b c E g d e f m
  • 21. Split A Red-Black Tree • Previous strategy does not split a red-black tree into two red-black trees. • Must do a search for m followed by a traceback to the root. • During the traceback use the join operation to construct S and B.
  • 22. Split A Red-Black Tree A S=f B=g a B C b c D E d e m f g
  • 23. Split A Red-Black Tree A S=f B=g a B C b c D E d e
  • 24. Split A Red-Black Tree A S=f B=g a B S = join(e, E, S) C b c D E d e
  • 25. Split A Red-Black Tree A S=f B =g a B S = join(e, E, S) C b B = join(B, D, d) c D d
  • 26. Split A Red-Black Tree A S=f B =g a B S = join(e, E, S) C b B = join(B, D, d) c S = join(c, C, S)
  • 27. Split A Red-Black Tree A S=f B =g a B S = join(e, E, S) b B = join(B, D, d) S = join(c, C, S) B = join(B, B, b)
  • 28. Split A Red-Black Tree A S=f B =g a S = join(e, E, S) B = join(B, D, d) S = join(c, C, S) B = join(B, B, b) S = join(a, A, S)
  • 29. Complexity Of Split • O(log n) • See text.