SlideShare a Scribd company logo
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

Red black trees
Red black treesRed black trees
Red black trees
Amit Kumar Rathi
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
Syed Zaid Irshad
 
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
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
Avl trees
Avl treesAvl trees
Avl trees
amna izzat
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
Dhrumil Panchal
 
Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
Varun Mahajan
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
ghhgj jhgh
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
Krish_ver2
 
Red black tree insertion
Red black tree insertionRed black tree insertion
Red black tree insertion
Kousalya M
 
Red black trees
Red black treesRed black trees
Red black trees
Maira Shahnawaz
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
shameen khan
 
Heap sort
Heap sortHeap sort
Heap sort
Mohd Arif
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Dr Shashikant Athawale
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
AVL TreeAVL Tree
AVL Tree
Chhatra Thapa
 
Optimal binary search tree dynamic programming
Optimal binary search tree   dynamic programmingOptimal binary search tree   dynamic programming
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
sandeep54552
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
Kamran Zafar
 
Backtracking
BacktrackingBacktracking
Backtracking
subhradeep mitra
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )
Sazzad Hossain
 

What's hot (20)

Red black trees
Red black treesRed black trees
Red black trees
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
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
 
Avl trees
Avl treesAvl trees
Avl trees
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Red black tree insertion
Red black tree insertionRed black tree insertion
Red black tree insertion
 
Red black trees
Red black treesRed black trees
Red black trees
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Heap sort
Heap sortHeap sort
Heap sort
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Optimal binary search tree dynamic programming
Optimal binary search tree   dynamic programmingOptimal binary search tree   dynamic programming
Optimal binary search tree dynamic programming
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
 
Backtracking
BacktrackingBacktracking
Backtracking
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )
 

Similar to Red black tree

rbtrees.ppt
rbtrees.pptrbtrees.ppt
rbtrees.ppt
Umesh Hengaju
 
Advanced data structures and implementation
Advanced data structures and implementationAdvanced data structures and implementation
Advanced data structures and implementation
Umang Gupta
 
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
AayushAdhikari27
 
Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
Piotr Szymański
 
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
Sheba41
 
Cse 225 rbt_red_black_tree
Cse 225 rbt_red_black_treeCse 225 rbt_red_black_tree
Cse 225 rbt_red_black_tree
Sazidur Rahman Sadi
 
16 rbtrees
16 rbtrees16 rbtrees
16 rbtrees
Dheeraj Singh
 
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)
United International University
 
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)
B.Sc in CSE, United International University - UIU, Dhaka
 
lecture 14
lecture 14lecture 14
lecture 14
sajinsc
 
BST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptx
BST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptxBST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptx
BST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptx
ssuser7b7f4e
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
AdityaK92
 
lecture14.ppt
lecture14.pptlecture14.ppt
lecture14.ppt
PallaviDhade1
 
Red black trees1109
Red black trees1109Red black trees1109
Red black trees1109
Pravin Dsilva
 
Red black tree
Red black treeRed black tree
Red black tree
vinitha96
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
ER Punit Jain
 
Lec14
Lec14Lec14
Red black 1
Red black 1Red black 1
Red black 1
Core Condor
 
Red-black trees
Red-black treesRed-black trees
Red-black trees
sathish sak
 
Red blacktrees
Red blacktreesRed blacktrees
Red blacktrees
Core Condor
 

Similar to Red black tree (20)

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
 
BST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptx
BST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptxBST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptx
BST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptx
 
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

Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 

Recently uploaded (20)

Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.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