SlideShare a Scribd company logo
1 of 23
Red-black Trees



            Author:
            Varun Mahajan
            <varunmahajan06@gmail.com>
Contents
 ●
     Dynamic sets
       ●
           Operations on Dynamic sets

 ●
     Binary trees: Basic terminology

 ●
     Binary search trees
       ●
           TREE_SEARCH
       ●
           TREE_INSERT
       ●
           Operations' running time: O(h)

 ●
     Red-black trees
       ●
           Red-black properties
       ●
           Height: O(lg n)
       ●
           Queries: O(lg n)
       ●
           Rotations: O(1)
       ●
           RB-INSERT: O(lg n)
Dynamic sets
 Set: A collection of objects of a particular kind

 The Sets manipulated by algorithms can grow, shrink, or otherwise change
 over time. Such sets are called Dynamic Sets

 In a typical implementation of a dynamic set, each element is represented by
 an object whose fields can be examined and manipulated if we have a pointer
 to the object

 Object fields:
    ●
      Identifying Key field
    ●
      Satellite data
Operations on Dynamic sets
 Two categories:
 ●
   Queries
 ●
   Modifying Operations

 SEARCH(S,k): A query that, given a set S and a key value k, returns a
 pointer x to an element in S such that key[x] = k, or NIL if no such element
 belongs to S

 INSERT(S,x): A modifying operation that augments the set S with the
 element pointed to by x. We usually assume that any fields in element x
 needed by the set implementation have already been initialized

 DELETE(S,x): A modifying operation that, given a pointer x to an element in
 the set S, removes x from S. (Note that this operation uses a pointer to an
 element x, not a key value.)

 MINIMUM(S): A query on a totally ordered set S that returns a pointer to
 the element of S with the smallest key
Operations on Dynamic sets
 MAXIMUM(S): A query on a totally ordered set S that returns a pointer to
 the element of S with the largest key

 SUCCESSOR(S,x): A query that, given an element x whose key is from a
 totally ordered set S, returns a pointer to the next larger element in S, or NIL
 if x is the maximum element

 PREDESESSOR(S,x): A query that, given an element x whose key is from a
 totally ordered set S, returns a pointer to the next smaller element in S, or
 NIL if x is the minimum element




 Totally ordered set: For any two elements a and b in the set, exactly one of the
 following must hold: a < b, a = b, or a > b
Binary trees
 Binary trees are defined recursively. A binary tree T is a structure defined on
 a finite set of nodes that either

     ●
         contains no nodes, or
     ●
         is composed of three disjoint sets of nodes: a root node, a binary tree
         called its left subtree, and a binary tree called its right subtree

 The binary tree that contains no nodes is called the empty tree or null tree,
 sometimes denoted NIL

 If the left subtree is nonempty, its root is called the left child of the root of
 the entire tree

 Likewise, the root of a nonnull right subtree is the right child of the root of
 the entire tree

 A node with no children is an external node or leaf

 A nonleaf node is an internal node
Binary trees
 If n1, n2, . . . , nk is a sequence of nodes in a tree such that ni is the parent
 of ni+1 for 1 ≀ i < k, then this sequence is called a path from node n1 to
 node nk.

 The length of a path is one less than the number of nodes in the path

 The height of a node in a tree is the length of a longest path from the node
 to a leaf

 The height of a tree is the height of the root

 The depth of a node is the length of the unique path from the root to that
 node
Binary Search trees




 The keys in a binary search tree are always stored in such a way as to
 satisfy the binary-search-tree property:

    ●
        Let x be a node in a binary search tree. If y is a node in the left subtree
        of x, then key[y] ≀ key[x]. If y is a node in the right subtree of x, then
        key[x] ≀ key[y]
TREE-SEARCH
 T R E E -S E A R C H (T, k )

  if T = N I L of k = key[T ]
       return T
  els e if k < key[T ]
       return T R E E -S E A R C H (left[T ], k )
  els e
       return T R E E -S E A R C H (rig ht[T ], k )

 TREE-SEARCH runs in O(h) time on a tree of height h




                                                      (a) A binary search tree on 6 nodes
                                                      with height 2. (b) A less efficient
                                                      binary search tree with height 4 that
                                                      contains the same keys
TREE-INSERT
 T R E E -I N S E R T (T, x )

  if T = N I L
       T = x
  return
  els e if key[x ] < key[T ]
       return T R E E -I N S E R T (left[T ], x )
  els e
       return T R E E -I N S E R T (rig ht[T ], x )

 TREE-INSERT runs in O(h) time on a tree of height h




                                                      Inserting an item with key 13 into a binary
                                                      search tree. Lightly shaded nodes indicate the
                                                      path from the root down to the position where
                                                      the item is inserted. The dashed line indicates
                                                      the link in the tree that is added to insert the
                                                      item
Binary Search trees Operations: Running time
  The worst-case running time for most search-tree operations is
  proportional to the height of the tree

  Queries:
     ●
       TREE-SEARCH(S,k):        O(h)
     ●
       TREE-MINIMUM(S):         O(h)
     ●
       TREE-MAXIMUM(S):         O(h)
     ●
       TREE-SUCCESSOR(S,x):     O(h)
     ●
       TREE-PREDECESSOR(S,x):   O(h)

  Modifying operations:
     ●
       TREE-INSERT(S,x):        O(h)
     ●
       TREE-DELETE(S,x):        O(h)
Red-black trees
 Red-black trees are binary search trees that are "balanced" in order to
 guarantee that basic dynamic-set operations take O(lg n) time in the
 worst case (height of the tree: O(lg n) where n is the no of nodes)

 A red-black tree is a binary search tree with one extra bit of storage per node:
 its color, which can be either RED or BLACK. By constraining the way
 nodes can be colored on any path from the root to a leaf, red-black
 trees ensure that no such path is more than twice as long as any
 other, so that the tree is approximately balanced
Red-black properties
 Binary search tree is a red-black tree if it satisfies the following red-black
 properties:
     ●
       Every node is either red or black
     ●
       The root is black
     ●
       Every leaf (NIL) is black
     ●
       If a node is red, then both its children are black
     ●
       For each node, all paths from the node to descendant leaves contain the same
       number of black nodes

 The number of black nodes on any path from, but not including, a node x down to a leaf
 is called the black-height of the node, denoted as bh(x)

 black-height of a red-black tree is the black-height of its root
Height O(lg n)
 A red-black tree with n internal nodes has height at most 2 lg(n + 1)
 i.e. O(lg n)

     ●
         A subtree rooted at x contains internal nodes >= 2^bh(x) -1

          ●
              For leaf: bh(leaf) = 0 => internal nodes = 2^0 - 1 = 0 (TRUE)
          ●
              For internal node x with two children:
                ●
                    Each child has black height bh(x) or bh(x) -1, depending on whether its color is red or
                    black, respectively
                ●
                    Subtree rooted at x: internal nodes >= (2^ (bh(x) – 1) -1) + (2^ (bh(x) – 1) -1) + 1 =
                    2^bh(x) -1 (TRUE)
          ●
              Claim is proved by induction

     ●
         The black-height of the root must be at least h/2 by property 4 (h is
         height of tree)

          ●
              n >= 2^bh(root) - 1
          ●
              n >= 2^(h/2) – 1
          ●
              lg(n +1) >= h/2
          ●
              h <= 2 lg(n+1)

 An immediate consequence of this lemma is that the dynamic-set operations
 SEARCH, MINIMUM, MAXIMUM, SUCCESSOR, and PREDECESSOR can be
 implemented in O(lg n) time on red-black trees, since they can be made to run
 in O(h) time on a search tree of height h and any red-black tree on n nodes is a
 search tree with height O(lg n)
Rotations
β€’ A local operation in a
search tree which changes
the pointer structure and
preserves    the     binary-
search-tree property

β€’ Both LEFT-ROTATE and
RIGHT-ROTATE run in O(1)
time. Only pointers are
changed by a rotation; all
other fields in a node
remain the same
RB-INSERT
R B -I N S E R T (T, z)

    T R E E -I N S E R T (T, z)
    c o lo r[z] = R E D
    R B -I N S E R T-FI X U P (T, z)
RB-INSERT-FIXUP: Case 1
R B -I N S E R T-FI X U P (T, z)
RB-INSERT-FIXUP: Case 2,3
R B -I N S E R T-FI X U P (T, z)
RB-INSERT-FIXUP: Example
RB-INSERT: Running time
R B -I N S E R T (T, z): O (lg n)

       T R E E -I N S E R T (T, z)              O (lg n)
       c o lo r[z] = R E D                      O (1)
       R B -I N S E R T-FI X U P (T, z)         O (lg n)

 R B -I N S E R T-FI X U P (T, z): O (lg n)
   ●
        Case 1: The pointer z moves two levels up the tree. Maximum times this can happen
        (when case 1 is repeated) is O(lg n)
   ●
        Case 2,3: At the maximum two rotations are done




The running time of RB-DELETE(T, z) is also O(lg n)
Exercise
 Study:

   ●
       TREE-DELETE, TREE-SUCCESSOR, TREE_PREDECESSOR
   ●
       RB-DELETE
   ●
       Linux kernel implementation of Red-black tree:
         ●
           ../include/linux/rbtree.h
         ●
           ../lib/rbtree.c
References
 ●
  Introduction to Algorithms, Second Edition, Thomas H. Cormen, Charles E. Leiserson, Ronald
 L. Rivest, Clifford Stein; The MIT Press

 ●
     http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-046j-introduction-to-alg

 ●
     http://lwn.net/Articles/184495/
END...

More Related Content

What's hot

Red black trees
Red black treesRed black trees
Red black treesmumairsadiq
Β 
Red black tree
Red black treeRed black tree
Red black treevinitha96
Β 
Fibonacci search
Fibonacci searchFibonacci search
Fibonacci searchneilluiz94
Β 
02 order of growth
02 order of growth02 order of growth
02 order of growthHira Gul
Β 
Binary search tree in data structures
Binary search tree in  data structuresBinary search tree in  data structures
Binary search tree in data structureschauhankapil
Β 
Page Replacement
Page ReplacementPage Replacement
Page Replacementchandinisanz
Β 
Tree data structure
Tree data structureTree data structure
Tree data structureDana dia
Β 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihmSajid Marwat
Β 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notationsNikhil Sharma
Β 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sortDistrict Administration
Β 
Bucket sort
Bucket sortBucket sort
Bucket sortToto MZiri
Β 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursionAbdullah Al-hazmy
Β 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programmingGopi Saiteja
Β 
Binary tree
Binary tree Binary tree
Binary tree Rajendran
Β 
First Order Logic resolution
First Order Logic resolutionFirst Order Logic resolution
First Order Logic resolutionAmar Jukuntla
Β 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data StructureAnuj Modi
Β 

What's hot (20)

AVL Tree
AVL TreeAVL Tree
AVL Tree
Β 
Red black trees
Red black treesRed black trees
Red black trees
Β 
Red black tree
Red black treeRed black tree
Red black tree
Β 
Fibonacci search
Fibonacci searchFibonacci search
Fibonacci search
Β 
Dfs
DfsDfs
Dfs
Β 
02 order of growth
02 order of growth02 order of growth
02 order of growth
Β 
Threaded Binary Tree.pptx
Threaded Binary Tree.pptxThreaded Binary Tree.pptx
Threaded Binary Tree.pptx
Β 
Binary search tree in data structures
Binary search tree in  data structuresBinary search tree in  data structures
Binary search tree in data structures
Β 
Apriori Algorithm
Apriori AlgorithmApriori Algorithm
Apriori Algorithm
Β 
Page Replacement
Page ReplacementPage Replacement
Page Replacement
Β 
Tree data structure
Tree data structureTree data structure
Tree data structure
Β 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
Β 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
Β 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
Β 
Bucket sort
Bucket sortBucket sort
Bucket sort
Β 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
Β 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Β 
Binary tree
Binary tree Binary tree
Binary tree
Β 
First Order Logic resolution
First Order Logic resolutionFirst Order Logic resolution
First Order Logic resolution
Β 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data Structure
Β 

Viewers also liked

Viewers also liked (8)

Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
Β 
Red black trees1109
Red black trees1109Red black trees1109
Red black trees1109
Β 
10 Red-Black Trees
10 Red-Black Trees10 Red-Black Trees
10 Red-Black Trees
Β 
Red black trees presentation
Red black trees presentationRed black trees presentation
Red black trees presentation
Β 
Red Black Tree
Red Black TreeRed Black Tree
Red Black Tree
Β 
Insertion in RED BLACK TREE
Insertion in RED BLACK TREEInsertion in RED BLACK TREE
Insertion in RED BLACK TREE
Β 
Intro to Graphing Data Powerpoint-7th and 8th Grade
Intro to Graphing Data Powerpoint-7th and 8th GradeIntro to Graphing Data Powerpoint-7th and 8th Grade
Intro to Graphing Data Powerpoint-7th and 8th Grade
Β 
Introduction to graph class 8
Introduction to graph class 8Introduction to graph class 8
Introduction to graph class 8
Β 

Similar to Red Black Trees

lecture 13
lecture 13lecture 13
lecture 13sajinsc
Β 
Skiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure treesSkiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure treeszukun
Β 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeAdityaK92
Β 
15-btrees.ppt
15-btrees.ppt15-btrees.ppt
15-btrees.pptplagcheck
Β 
Review session2
Review session2Review session2
Review session2NEEDY12345
Β 
UNIT II.ppt
UNIT II.pptUNIT II.ppt
UNIT II.pptSwarndeviKm
Β 
Data structures final lecture 1
Data structures final  lecture 1Data structures final  lecture 1
Data structures final lecture 1Nazir Ahmed
Β 
part4-trees.ppt
part4-trees.pptpart4-trees.ppt
part4-trees.pptSuneel61
Β 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeZafar Ayub
Β 
CS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfCS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfssuser034ce1
Β 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures treemaamir farooq
Β 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfCS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfssuser034ce1
Β 
Balance tree. Short overview
Balance tree. Short overviewBalance tree. Short overview
Balance tree. Short overviewElifTech
Β 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structuresNiraj Agarwal
Β 

Similar to Red Black Trees (20)

lecture 13
lecture 13lecture 13
lecture 13
Β 
Binary trees1
Binary trees1Binary trees1
Binary trees1
Β 
Skiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure treesSkiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure trees
Β 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
Β 
15-btrees.ppt
15-btrees.ppt15-btrees.ppt
15-btrees.ppt
Β 
Review session2
Review session2Review session2
Review session2
Β 
UNIT II.ppt
UNIT II.pptUNIT II.ppt
UNIT II.ppt
Β 
7.tree
7.tree7.tree
7.tree
Β 
Data structures final lecture 1
Data structures final  lecture 1Data structures final  lecture 1
Data structures final lecture 1
Β 
16 rbtrees
16 rbtrees16 rbtrees
16 rbtrees
Β 
part4-trees.ppt
part4-trees.pptpart4-trees.ppt
part4-trees.ppt
Β 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
Β 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
Β 
CS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfCS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdf
Β 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
Β 
Trees
TreesTrees
Trees
Β 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfCS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdf
Β 
tree.ppt
tree.ppttree.ppt
tree.ppt
Β 
Balance tree. Short overview
Balance tree. Short overviewBalance tree. Short overview
Balance tree. Short overview
Β 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
Β 

More from Varun Mahajan

I2C Bus (Inter-Integrated Circuit)
I2C Bus (Inter-Integrated Circuit)I2C Bus (Inter-Integrated Circuit)
I2C Bus (Inter-Integrated Circuit)Varun Mahajan
Β 
I2C Subsystem In Linux-2.6.24
I2C Subsystem In Linux-2.6.24I2C Subsystem In Linux-2.6.24
I2C Subsystem In Linux-2.6.24Varun Mahajan
Β 
Hibernation in Linux 2.6.29
Hibernation in Linux 2.6.29Hibernation in Linux 2.6.29
Hibernation in Linux 2.6.29Varun Mahajan
Β 
Process' Virtual Address Space in GNU/Linux
Process' Virtual Address Space in GNU/LinuxProcess' Virtual Address Space in GNU/Linux
Process' Virtual Address Space in GNU/LinuxVarun Mahajan
Β 
Program Structure in GNU/Linux (ELF Format)
Program Structure in GNU/Linux (ELF Format)Program Structure in GNU/Linux (ELF Format)
Program Structure in GNU/Linux (ELF Format)Varun Mahajan
Β 
Introduction to GNU/Linux, Free Software, Open Source Software, FSF, FSM, OSI
Introduction to GNU/Linux, Free Software, Open Source Software, FSF, FSM, OSIIntroduction to GNU/Linux, Free Software, Open Source Software, FSF, FSM, OSI
Introduction to GNU/Linux, Free Software, Open Source Software, FSF, FSM, OSIVarun Mahajan
Β 

More from Varun Mahajan (6)

I2C Bus (Inter-Integrated Circuit)
I2C Bus (Inter-Integrated Circuit)I2C Bus (Inter-Integrated Circuit)
I2C Bus (Inter-Integrated Circuit)
Β 
I2C Subsystem In Linux-2.6.24
I2C Subsystem In Linux-2.6.24I2C Subsystem In Linux-2.6.24
I2C Subsystem In Linux-2.6.24
Β 
Hibernation in Linux 2.6.29
Hibernation in Linux 2.6.29Hibernation in Linux 2.6.29
Hibernation in Linux 2.6.29
Β 
Process' Virtual Address Space in GNU/Linux
Process' Virtual Address Space in GNU/LinuxProcess' Virtual Address Space in GNU/Linux
Process' Virtual Address Space in GNU/Linux
Β 
Program Structure in GNU/Linux (ELF Format)
Program Structure in GNU/Linux (ELF Format)Program Structure in GNU/Linux (ELF Format)
Program Structure in GNU/Linux (ELF Format)
Β 
Introduction to GNU/Linux, Free Software, Open Source Software, FSF, FSM, OSI
Introduction to GNU/Linux, Free Software, Open Source Software, FSF, FSM, OSIIntroduction to GNU/Linux, Free Software, Open Source Software, FSF, FSM, OSI
Introduction to GNU/Linux, Free Software, Open Source Software, FSF, FSM, OSI
Β 

Recently uploaded

Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
Β 
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
Β 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
Β 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
Β 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
Β 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
Β 
call girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈ
call girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈcall girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈ
call girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈ9953056974 Low Rate Call Girls In Saket, Delhi NCR
Β 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
Β 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
Β 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
Β 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
Β 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
Β 
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
Β 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
Β 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
Β 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
Β 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
Β 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
Β 

Recently uploaded (20)

Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Β 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.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
Β 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
Β 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
Β 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
Β 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
Β 
call girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈ
call girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈcall girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈ
call girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈ
Β 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
Β 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
Β 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
Β 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
Β 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
Β 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Β 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
Β 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
Β 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
Β 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Β 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
Β 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
Β 

Red Black Trees

  • 1. Red-black Trees Author: Varun Mahajan <varunmahajan06@gmail.com>
  • 2. Contents ● Dynamic sets ● Operations on Dynamic sets ● Binary trees: Basic terminology ● Binary search trees ● TREE_SEARCH ● TREE_INSERT ● Operations' running time: O(h) ● Red-black trees ● Red-black properties ● Height: O(lg n) ● Queries: O(lg n) ● Rotations: O(1) ● RB-INSERT: O(lg n)
  • 3. Dynamic sets Set: A collection of objects of a particular kind The Sets manipulated by algorithms can grow, shrink, or otherwise change over time. Such sets are called Dynamic Sets In a typical implementation of a dynamic set, each element is represented by an object whose fields can be examined and manipulated if we have a pointer to the object Object fields: ● Identifying Key field ● Satellite data
  • 4. Operations on Dynamic sets Two categories: ● Queries ● Modifying Operations SEARCH(S,k): A query that, given a set S and a key value k, returns a pointer x to an element in S such that key[x] = k, or NIL if no such element belongs to S INSERT(S,x): A modifying operation that augments the set S with the element pointed to by x. We usually assume that any fields in element x needed by the set implementation have already been initialized DELETE(S,x): A modifying operation that, given a pointer x to an element in the set S, removes x from S. (Note that this operation uses a pointer to an element x, not a key value.) MINIMUM(S): A query on a totally ordered set S that returns a pointer to the element of S with the smallest key
  • 5. Operations on Dynamic sets MAXIMUM(S): A query on a totally ordered set S that returns a pointer to the element of S with the largest key SUCCESSOR(S,x): A query that, given an element x whose key is from a totally ordered set S, returns a pointer to the next larger element in S, or NIL if x is the maximum element PREDESESSOR(S,x): A query that, given an element x whose key is from a totally ordered set S, returns a pointer to the next smaller element in S, or NIL if x is the minimum element Totally ordered set: For any two elements a and b in the set, exactly one of the following must hold: a < b, a = b, or a > b
  • 6. Binary trees Binary trees are defined recursively. A binary tree T is a structure defined on a finite set of nodes that either ● contains no nodes, or ● is composed of three disjoint sets of nodes: a root node, a binary tree called its left subtree, and a binary tree called its right subtree The binary tree that contains no nodes is called the empty tree or null tree, sometimes denoted NIL If the left subtree is nonempty, its root is called the left child of the root of the entire tree Likewise, the root of a nonnull right subtree is the right child of the root of the entire tree A node with no children is an external node or leaf A nonleaf node is an internal node
  • 7. Binary trees If n1, n2, . . . , nk is a sequence of nodes in a tree such that ni is the parent of ni+1 for 1 ≀ i < k, then this sequence is called a path from node n1 to node nk. The length of a path is one less than the number of nodes in the path The height of a node in a tree is the length of a longest path from the node to a leaf The height of a tree is the height of the root The depth of a node is the length of the unique path from the root to that node
  • 8. Binary Search trees The keys in a binary search tree are always stored in such a way as to satisfy the binary-search-tree property: ● Let x be a node in a binary search tree. If y is a node in the left subtree of x, then key[y] ≀ key[x]. If y is a node in the right subtree of x, then key[x] ≀ key[y]
  • 9. TREE-SEARCH T R E E -S E A R C H (T, k ) if T = N I L of k = key[T ] return T els e if k < key[T ] return T R E E -S E A R C H (left[T ], k ) els e return T R E E -S E A R C H (rig ht[T ], k ) TREE-SEARCH runs in O(h) time on a tree of height h (a) A binary search tree on 6 nodes with height 2. (b) A less efficient binary search tree with height 4 that contains the same keys
  • 10. TREE-INSERT T R E E -I N S E R T (T, x ) if T = N I L T = x return els e if key[x ] < key[T ] return T R E E -I N S E R T (left[T ], x ) els e return T R E E -I N S E R T (rig ht[T ], x ) TREE-INSERT runs in O(h) time on a tree of height h Inserting an item with key 13 into a binary search tree. Lightly shaded nodes indicate the path from the root down to the position where the item is inserted. The dashed line indicates the link in the tree that is added to insert the item
  • 11. Binary Search trees Operations: Running time The worst-case running time for most search-tree operations is proportional to the height of the tree Queries: ● TREE-SEARCH(S,k): O(h) ● TREE-MINIMUM(S): O(h) ● TREE-MAXIMUM(S): O(h) ● TREE-SUCCESSOR(S,x): O(h) ● TREE-PREDECESSOR(S,x): O(h) Modifying operations: ● TREE-INSERT(S,x): O(h) ● TREE-DELETE(S,x): O(h)
  • 12. Red-black trees Red-black trees are binary search trees that are "balanced" in order to guarantee that basic dynamic-set operations take O(lg n) time in the worst case (height of the tree: O(lg n) where n is the no of nodes) A red-black tree is a binary search tree with one extra bit of storage per node: its color, which can be either RED or BLACK. By constraining the way nodes can be colored on any path from the root to a leaf, red-black trees ensure that no such path is more than twice as long as any other, so that the tree is approximately balanced
  • 13. Red-black properties Binary search tree is a red-black tree if it satisfies the following red-black properties: ● Every node is either red or black ● The root is black ● Every leaf (NIL) is black ● If a node is red, then both its children are black ● For each node, all paths from the node to descendant leaves contain the same number of black nodes The number of black nodes on any path from, but not including, a node x down to a leaf is called the black-height of the node, denoted as bh(x) black-height of a red-black tree is the black-height of its root
  • 14. Height O(lg n) A red-black tree with n internal nodes has height at most 2 lg(n + 1) i.e. O(lg n) ● A subtree rooted at x contains internal nodes >= 2^bh(x) -1 ● For leaf: bh(leaf) = 0 => internal nodes = 2^0 - 1 = 0 (TRUE) ● For internal node x with two children: ● Each child has black height bh(x) or bh(x) -1, depending on whether its color is red or black, respectively ● Subtree rooted at x: internal nodes >= (2^ (bh(x) – 1) -1) + (2^ (bh(x) – 1) -1) + 1 = 2^bh(x) -1 (TRUE) ● Claim is proved by induction ● The black-height of the root must be at least h/2 by property 4 (h is height of tree) ● n >= 2^bh(root) - 1 ● n >= 2^(h/2) – 1 ● lg(n +1) >= h/2 ● h <= 2 lg(n+1) An immediate consequence of this lemma is that the dynamic-set operations SEARCH, MINIMUM, MAXIMUM, SUCCESSOR, and PREDECESSOR can be implemented in O(lg n) time on red-black trees, since they can be made to run in O(h) time on a search tree of height h and any red-black tree on n nodes is a search tree with height O(lg n)
  • 15. Rotations β€’ A local operation in a search tree which changes the pointer structure and preserves the binary- search-tree property β€’ Both LEFT-ROTATE and RIGHT-ROTATE run in O(1) time. Only pointers are changed by a rotation; all other fields in a node remain the same
  • 16. RB-INSERT R B -I N S E R T (T, z) T R E E -I N S E R T (T, z) c o lo r[z] = R E D R B -I N S E R T-FI X U P (T, z)
  • 17. RB-INSERT-FIXUP: Case 1 R B -I N S E R T-FI X U P (T, z)
  • 18. RB-INSERT-FIXUP: Case 2,3 R B -I N S E R T-FI X U P (T, z)
  • 20. RB-INSERT: Running time R B -I N S E R T (T, z): O (lg n) T R E E -I N S E R T (T, z) O (lg n) c o lo r[z] = R E D O (1) R B -I N S E R T-FI X U P (T, z) O (lg n) R B -I N S E R T-FI X U P (T, z): O (lg n) ● Case 1: The pointer z moves two levels up the tree. Maximum times this can happen (when case 1 is repeated) is O(lg n) ● Case 2,3: At the maximum two rotations are done The running time of RB-DELETE(T, z) is also O(lg n)
  • 21. Exercise Study: ● TREE-DELETE, TREE-SUCCESSOR, TREE_PREDECESSOR ● RB-DELETE ● Linux kernel implementation of Red-black tree: ● ../include/linux/rbtree.h ● ../lib/rbtree.c
  • 22. References ● Introduction to Algorithms, Second Edition, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein; The MIT Press ● http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-046j-introduction-to-alg ● http://lwn.net/Articles/184495/