SlideShare a Scribd company logo
1 of 37
Download to read offline
Red Black TreeRed Black Tree
1
Group Member
Balawal Hussain 269
Nasir Shahzad 287
Ali Raza 277
Umair Khan 293
Usman Aslam 299
2
Red Black VS BNS Tree
Red black tree are new form of data
structure but inherit similarities from
binary search tree
 for example: red black tree have up to 2 for example: red black tree have up to 2
children per node. They must follow the
binary search tree.
 On left side is smaller than parent.
 On right side is greater than parent.
3
4
Binary search tree
• In BST, all left sub-tree element should be
less than root node and all right sub-tree
should be greater than root node, called
BST.BST.
• Parent node should not have more than
two children.
Parent<Right
Parent>left
5
Binary search tree
30
25 50
Example: 30,25,50,27,40,66,5,3,10
25 50
5 27 40 66
3 10
6
About Red black tree
Invented in Invented byInvented in
1972.
Invented by
Rudolf Bayer
7
Red black tree
Red black tree properties:
1-Every node is either red or black.
2-The root is black.
3-Every leaf (NIL) is black.3-Every leaf (NIL) is black.
4-If a node is red, then both its children are
black.
5-For each node, all simple paths from the node
to descendant leaves contain the same
number of black nodes.
8
Red black tree
19
12 35
2116 5621
30
3
9
Black-Height of a Node
26
17 41
30 47
38 50
NIL NIL
h = 4
bh = 2
h = 3
bh = 2
h = 2
bh = 1
h = 1
h = 1
bh = 1
h = 2
bh = 1 h = 1
bh = 1
38 50NIL
NIL NIL NIL NIL
NIL
h = 1
bh = 1
bh = 1
Height of a node:
the number of edges in the longest path to a
leaf.
Black-height of a node x:
bh(x) is the number of black nodes
(including NIL) on the path from x to a leaf, not
counting x
10
ROTATIONSROTATIONS
11
RB tree Rotations
There are two type of rotations:
1-left rotation1-left rotation
2-right rotation
12
Left Rotations
assumption for a left rotation on a node x:
The right child of x (y) is not NIL
Idea:
rotate around the link from x to y
Makes y the new root of the sub-tree
x becomes y’s left child
y’s left child becomes x’s right child 13
Example: LEFT-ROTATE
14
Right Rotations
Assumptions for a right rotation on a node x:
The left child of y (x) is not NIL
Idea:
 Pivots around the link from y to x
 Makes x the new root of the subtree
 y becomes x’s right child
 x’s right child becomes y’s left child
15
Insertion
16
RB tree Insertion
As case in BST, first we find place where
we are to insert.
No red red child parent relation.
Red node can have only black childrenRed node can have only black children
Since inserted color is red, the black height
of the tree is remain unchanged.
 A new item is always inserted as a leaf in
the tree.
17
Case 1 – uncle y is red
C
A D
B
  
z
y
C
A D
B
  
new z
p[z]
p[p[z]]
• p[p[z]] (z’s grandparent) must be black, since z and p[z] are both red
and there are no other violations of property 4.
• Make p[z] and y black  now z and p[z] are not both red. But
property 5 might now be violated.
• Make p[p[z]] red  restores property 5.
• The next iteration has p[p[z]] as the new z (i.e., z moves up 2 levels).
B
 
B
 
z is a right child here.
Similar steps if z is a left child.
18
Case 2 – y is black, z is a right child
C
A
B


z
y
C
B
A 

z
y
p[z]
p[z]
• Left rotate around p[z], p[z] and z switch roles  now z is
a left child, and both z and p[z] are red.
• Takes us immediately to case 3.
B

 
A
 
z
19
Case 3 – y is black, z is a left child
B
A
   
C
B
A 
 y
p[z]
C
z
• Make p[z] black and p[p[z]] red.
• Then right rotate on p[p[z]]. Ensures property 4 is
maintained.
• No longer have 2 reds in a row.
• p[z] is now black  no more iterations.
A
 
20
RB tree Insertion
• Example:
Insert 1 1
• The root can’t be red.
21
RB tree Insertion
• Example:
Change the color, according to 2 property.
1
22
RB tree Insertion
• Example:
Insert 2
1
2
23
RB tree Insertion
• Example:
Insert 3
1
2
3
24
RB tree Insertion
• Example:
Case 3
1
2
31 3
25
RB tree Insertion
• Example:
• Follow the property 2
1
2
31 3
26
RB tree Insertion
• Example:
• Insert 4
1
2
31 3
4
27
RB tree Insertion
• Example:
• Case 1
1
2
31 3
4
28
RB tree Insertion
• Example:
• Color
1
2
31 3
4
29
RB tree Insertion
• Example:
• Insert 5
1
2
31 3
4
5
30
RB tree Insertion
• Example:
• Case 3
1
2
41 4
5
3
31
RB tree Insertion
• Example:
• Color
1
2
41 4
53
32
RB tree Insertion
• Example:
• Insert 6
1
2
41 4
53
6
33
RB tree Insertion
• Example:
• Case 1/color
1
2
41 4
53
6
34
Algorithm Analysis
O(lg n) time to get through RB-Insert up to
the call of RB-Insert.
Within RB-Insert:
– Each iteration takes O(1) time.
– Each iteration but the last moves z up 2 levels.
– O(lg n) levels  O(lg n) time.
– Thus, insertion in a red-black tree takes O(lg n)
time.
35
Problems?????
• Let a, b, c be arbitrary nodes in sub-trees , ,  in the
tree below. How do the depths of a, b, c change when a
left rotation is performed on node x?
– a: increases by 1
– b: stays the same– b: stays the same
– c: decreases by 1
36
Problems?????
• What is the ratio between the longest path
and the shortest path in a red-black tree?
- The shortest path is at least bh(root)- The shortest path is at least bh(root)
- The longest path is equal to h(root)
- We know that h(root)≤2bh(root)
- Therefore, the ratio is ≤2
37

More Related Content

What's hot

Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeAbhishek L.R
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search treeKrish_ver2
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeZafar Ayub
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )Sazzad Hossain
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Flynce Miguel
 
Binary search tree in data structures
Binary search tree in  data structuresBinary search tree in  data structures
Binary search tree in data structureschauhankapil
 
Dfs presentation
Dfs presentationDfs presentation
Dfs presentationAlizay Khan
 
Red black tree in data structure
Red black tree in data structureRed black tree in data structure
Red black tree in data structureVrushali Dhanokar
 
Red black tree insertion
Red black tree insertionRed black tree insertion
Red black tree insertionKousalya M
 
Algorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms IIAlgorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms IIMohamed Loey
 
Selection sort
Selection sortSelection sort
Selection sortamna izzat
 
Algorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph AlgorithmsAlgorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph AlgorithmsMohamed Loey
 

What's hot (20)

Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
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)
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )
 
B tree
B treeB tree
B tree
 
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
 
Binary search tree in data structures
Binary search tree in  data structuresBinary search tree in  data structures
Binary search tree in data structures
 
Binary tree
Binary tree Binary tree
Binary tree
 
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS
 
Dfs presentation
Dfs presentationDfs presentation
Dfs presentation
 
Red black tree in data structure
Red black tree in data structureRed black tree in data structure
Red black tree in data structure
 
Red black tree insertion
Red black tree insertionRed black tree insertion
Red black tree insertion
 
Algorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms IIAlgorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms II
 
Selection sort
Selection sortSelection sort
Selection sort
 
Algorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph AlgorithmsAlgorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph Algorithms
 

Similar to Red black tree

Similar to Red black tree (20)

Red black trees
Red black treesRed black trees
Red black trees
 
rbtrees.ppt
rbtrees.pptrbtrees.ppt
rbtrees.ppt
 
Advanced data structures and implementation
Advanced data structures and implementationAdvanced data structures and implementation
Advanced data structures and implementation
 
anastasio-red-black-trees-1-1-091222204455-phpapp02.pdf
anastasio-red-black-trees-1-1-091222204455-phpapp02.pdfanastasio-red-black-trees-1-1-091222204455-phpapp02.pdf
anastasio-red-black-trees-1-1-091222204455-phpapp02.pdf
 
Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
 
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.pptUnit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
 
Cse 225 rbt_red_black_tree
Cse 225 rbt_red_black_treeCse 225 rbt_red_black_tree
Cse 225 rbt_red_black_tree
 
16 rbtrees
16 rbtrees16 rbtrees
16 rbtrees
 
Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
 
Balanced Tree(AVL Tree,Red Black Tree)
Balanced Tree(AVL Tree,Red Black Tree)Balanced Tree(AVL Tree,Red Black Tree)
Balanced Tree(AVL Tree,Red Black Tree)
 
lecture 14
lecture 14lecture 14
lecture 14
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
lecture14.ppt
lecture14.pptlecture14.ppt
lecture14.ppt
 
Red black trees1109
Red black trees1109Red black trees1109
Red black trees1109
 
Red black tree
Red black treeRed black tree
Red black tree
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Lec14
Lec14Lec14
Lec14
 
Red black 1
Red black 1Red black 1
Red black 1
 
Red-black trees
Red-black treesRed-black trees
Red-black trees
 
Red blacktrees
Red blacktreesRed blacktrees
Red blacktrees
 

Recently uploaded

4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsPooky Knightsmith
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 

Recently uploaded (20)

Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young minds
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 

Red black tree

  • 1. Red Black TreeRed Black Tree 1
  • 2. Group Member Balawal Hussain 269 Nasir Shahzad 287 Ali Raza 277 Umair Khan 293 Usman Aslam 299 2
  • 3. Red Black VS BNS Tree Red black tree are new form of data structure but inherit similarities from binary search tree  for example: red black tree have up to 2 for example: red black tree have up to 2 children per node. They must follow the binary search tree.  On left side is smaller than parent.  On right side is greater than parent. 3
  • 4. 4
  • 5. Binary search tree • In BST, all left sub-tree element should be less than root node and all right sub-tree should be greater than root node, called BST.BST. • Parent node should not have more than two children. Parent<Right Parent>left 5
  • 6. Binary search tree 30 25 50 Example: 30,25,50,27,40,66,5,3,10 25 50 5 27 40 66 3 10 6
  • 7. About Red black tree Invented in Invented byInvented in 1972. Invented by Rudolf Bayer 7
  • 8. Red black tree Red black tree properties: 1-Every node is either red or black. 2-The root is black. 3-Every leaf (NIL) is black.3-Every leaf (NIL) is black. 4-If a node is red, then both its children are black. 5-For each node, all simple paths from the node to descendant leaves contain the same number of black nodes. 8
  • 9. Red black tree 19 12 35 2116 5621 30 3 9
  • 10. Black-Height of a Node 26 17 41 30 47 38 50 NIL NIL h = 4 bh = 2 h = 3 bh = 2 h = 2 bh = 1 h = 1 h = 1 bh = 1 h = 2 bh = 1 h = 1 bh = 1 38 50NIL NIL NIL NIL NIL NIL h = 1 bh = 1 bh = 1 Height of a node: the number of edges in the longest path to a leaf. Black-height of a node x: bh(x) is the number of black nodes (including NIL) on the path from x to a leaf, not counting x 10
  • 12. RB tree Rotations There are two type of rotations: 1-left rotation1-left rotation 2-right rotation 12
  • 13. Left Rotations assumption for a left rotation on a node x: The right child of x (y) is not NIL Idea: rotate around the link from x to y Makes y the new root of the sub-tree x becomes y’s left child y’s left child becomes x’s right child 13
  • 15. Right Rotations Assumptions for a right rotation on a node x: The left child of y (x) is not NIL Idea:  Pivots around the link from y to x  Makes x the new root of the subtree  y becomes x’s right child  x’s right child becomes y’s left child 15
  • 17. RB tree Insertion As case in BST, first we find place where we are to insert. No red red child parent relation. Red node can have only black childrenRed node can have only black children Since inserted color is red, the black height of the tree is remain unchanged.  A new item is always inserted as a leaf in the tree. 17
  • 18. Case 1 – uncle y is red C A D B    z y C A D B    new z p[z] p[p[z]] • p[p[z]] (z’s grandparent) must be black, since z and p[z] are both red and there are no other violations of property 4. • Make p[z] and y black  now z and p[z] are not both red. But property 5 might now be violated. • Make p[p[z]] red  restores property 5. • The next iteration has p[p[z]] as the new z (i.e., z moves up 2 levels). B   B   z is a right child here. Similar steps if z is a left child. 18
  • 19. Case 2 – y is black, z is a right child C A B   z y C B A   z y p[z] p[z] • Left rotate around p[z], p[z] and z switch roles  now z is a left child, and both z and p[z] are red. • Takes us immediately to case 3. B    A   z 19
  • 20. Case 3 – y is black, z is a left child B A     C B A   y p[z] C z • Make p[z] black and p[p[z]] red. • Then right rotate on p[p[z]]. Ensures property 4 is maintained. • No longer have 2 reds in a row. • p[z] is now black  no more iterations. A   20
  • 21. RB tree Insertion • Example: Insert 1 1 • The root can’t be red. 21
  • 22. RB tree Insertion • Example: Change the color, according to 2 property. 1 22
  • 23. RB tree Insertion • Example: Insert 2 1 2 23
  • 24. RB tree Insertion • Example: Insert 3 1 2 3 24
  • 25. RB tree Insertion • Example: Case 3 1 2 31 3 25
  • 26. RB tree Insertion • Example: • Follow the property 2 1 2 31 3 26
  • 27. RB tree Insertion • Example: • Insert 4 1 2 31 3 4 27
  • 28. RB tree Insertion • Example: • Case 1 1 2 31 3 4 28
  • 29. RB tree Insertion • Example: • Color 1 2 31 3 4 29
  • 30. RB tree Insertion • Example: • Insert 5 1 2 31 3 4 5 30
  • 31. RB tree Insertion • Example: • Case 3 1 2 41 4 5 3 31
  • 32. RB tree Insertion • Example: • Color 1 2 41 4 53 32
  • 33. RB tree Insertion • Example: • Insert 6 1 2 41 4 53 6 33
  • 34. RB tree Insertion • Example: • Case 1/color 1 2 41 4 53 6 34
  • 35. Algorithm Analysis O(lg n) time to get through RB-Insert up to the call of RB-Insert. Within RB-Insert: – Each iteration takes O(1) time. – Each iteration but the last moves z up 2 levels. – O(lg n) levels  O(lg n) time. – Thus, insertion in a red-black tree takes O(lg n) time. 35
  • 36. Problems????? • Let a, b, c be arbitrary nodes in sub-trees , ,  in the tree below. How do the depths of a, b, c change when a left rotation is performed on node x? – a: increases by 1 – b: stays the same– b: stays the same – c: decreases by 1 36
  • 37. Problems????? • What is the ratio between the longest path and the shortest path in a red-black tree? - The shortest path is at least bh(root)- The shortest path is at least bh(root) - The longest path is equal to h(root) - We know that h(root)≤2bh(root) - Therefore, the ratio is ≤2 37