SlideShare a Scribd company logo
1 of 14
Download to read offline
National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
1
Red Black Tree
By
Amlan Pati
Shivanand Sahu
Under the guidance of
Mr. Ajaya Kumar Das
A123001
A123007
National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
2
 Introduction
 Properties
 Insertion
 Deletion
TOPICS TO BE DISCUSSED
National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
3
International
Institute
of
Information
of
Technology
• 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.
• The longest path from the root to the leaf node cannot be more than twice the
shortest path.
• Each node of the tree now contains the attributes color, key, left, right, and p.
• If a child or the parent of a node does not exist, the corresponding pointer
attribute of the node contains the value NIL.
• Think of these NILs as pointers to leaves (external nodes) of the binary search
tree and the normal, key-bearing nodes as internal nodes of the tree.
INTRODUCTION
National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
4
Red-black Tree -Example
National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
5
A red-black tree is a binary search tree that satisfies the
following red black
properties:
1. Every node is either red or black.
2. The root 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.
Properties
National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
6
INSERTION
Cases
1. If tree is empty, create new node as root node with color
black.
2. If tree is not empty, create new node as leaf node with
color red.
3. If parent of new node is black then exit.
4. If parent of new node is red, then check the color of
parent’s sibling of new node.
a. If color is black or null then do suitable rotation and
recolor.
b. If color is red then recolor and also check if parent’s
parents of new node is not root node then recolor it
and recheck.
National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
7
Insertion
Example- 10,18,7,15,16,30,25,40,60,2,1,70
16
1
25
2
7
40
18
30
70
60
10
15
National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
8
Analysis of Insertion
• Search for insertion location takes O(log n) time
because we visit O(log n) nodes.
• Addition to the node takes O(1) time.
• Rotation or recoloring takes O(log n) time.
• O(log n) recoloring, each taking O(1) time, and
at most one rotation taking O(1) time.
• Thus, an insertion in a red-black tree takes O(log n)
time.
National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
9
DELETION
•Deleting a node from a red-black tree is a bit more
complicated than inserting a node.
•If the node is red?
Not a problem – no RB properties violated.
•If the node is black?
deleting it will change the black-height along some path.
National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
10
Cases
Case-1
•If node to be deleted is red, just delete it.
Case-2
•If root is double black(DB) ,just remove DB.
Case-3
•If DB’s sibling is black and both its children are black.
•Remove DB
•Add black to its parent (P)
•If parent is red it becomes black
•If parent is black it becomes double black
• Make sibling red.
• if still DB exist apply other cases.
National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
11
Cases
Case-4
• If DB’s sibling red.
• Swap color’s of parent its sibling of double black.
• Rotated parent towards double black direction.
• Reapply cases
Case-5
• DB’s sibling is black, sibling’s child who is far from DB is black, but
near child to DB is red.
• Swap color of DB’s sibling and sibling child who is near to DB.
• Rotate sibling in opposite direction to DB
• Apply case-6
National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
12
Cases
Case-6
• If DB’s sibling is black ,far child is red.
• Swap color of parent and sibling.
• Rotate parent in DB’s direction.
• Remove DB.
• Change color of red child to black.
National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
13
Analysis of Deletion
• A red-black tree has O(log n) height
• Search for deletion location takes O(log n) time
• Each rotation or recoloring is O(1).
• Thus, the deletion in a red-black tree takes O(log n) time
National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
14
CONCLUSION
• Red-Black Trees are preferred in scenarios where there is a
need for faster insertions and deletions, and the tree can
tolerate a slightly less balanced structure.
• AVL Trees may be preferred when a more strictly balanced
tree is required, and the focus is on maintaining a consistent
performance in search operations.
•The choice between the two depends on the specific
characteristics and requirements of the application.

More Related Content

Similar to Red Black Tree,Red black tree, Cases-insertion, Deletion

Similar to Red Black Tree,Red black tree, Cases-insertion, Deletion (6)

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
 
Advanced data structures and implementation
Advanced data structures and implementationAdvanced data structures and implementation
Advanced data structures and implementation
 
lecture 14
lecture 14lecture 14
lecture 14
 
Unit 2_1 Tree.pdf
Unit 2_1 Tree.pdfUnit 2_1 Tree.pdf
Unit 2_1 Tree.pdf
 
Quantifying the bias in data links
Quantifying the bias in data linksQuantifying the bias in data links
Quantifying the bias in data links
 

Recently uploaded

Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxkalpana413121
 
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...ronahami
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxpritamlangde
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Ramkumar k
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 
Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxNANDHAKUMARA10
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...HenryBriggs2
 
Path loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelPath loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelDrAjayKumarYadav4
 
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdflitvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdfAlexander Litvinenko
 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...ppkakm
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdfKamal Acharya
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdfKamal Acharya
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdfKamal Acharya
 
Post office management system project ..pdf
Post office management system project ..pdfPost office management system project ..pdf
Post office management system project ..pdfKamal Acharya
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...josephjonse
 
Adsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) pptAdsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) pptjigup7320
 
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...Payal Garg #K09
 

Recently uploaded (20)

Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptx
 
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptx
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
Path loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelPath loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata Model
 
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdflitvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Post office management system project ..pdf
Post office management system project ..pdfPost office management system project ..pdf
Post office management system project ..pdf
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
 
Adsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) pptAdsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) ppt
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
 

Red Black Tree,Red black tree, Cases-insertion, Deletion

  • 1. National Institute of Science & Technology M. Tech 1th Sem (CSE) Batch:2023-25 International Institute of Information of Technology 1 Red Black Tree By Amlan Pati Shivanand Sahu Under the guidance of Mr. Ajaya Kumar Das A123001 A123007
  • 2. National Institute of Science & Technology M. Tech 1th Sem (CSE) Batch:2023-25 International Institute of Information of Technology 2  Introduction  Properties  Insertion  Deletion TOPICS TO BE DISCUSSED
  • 3. National Institute of Science & Technology M. Tech 1th Sem (CSE) Batch:2023-25 International Institute of Information of Technology 3 International Institute of Information of Technology • 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. • The longest path from the root to the leaf node cannot be more than twice the shortest path. • Each node of the tree now contains the attributes color, key, left, right, and p. • If a child or the parent of a node does not exist, the corresponding pointer attribute of the node contains the value NIL. • Think of these NILs as pointers to leaves (external nodes) of the binary search tree and the normal, key-bearing nodes as internal nodes of the tree. INTRODUCTION
  • 4. National Institute of Science & Technology M. Tech 1th Sem (CSE) Batch:2023-25 International Institute of Information of Technology 4 Red-black Tree -Example
  • 5. National Institute of Science & Technology M. Tech 1th Sem (CSE) Batch:2023-25 International Institute of Information of Technology 5 A red-black tree is a binary search tree that satisfies the following red black properties: 1. Every node is either red or black. 2. The root 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. Properties
  • 6. National Institute of Science & Technology M. Tech 1th Sem (CSE) Batch:2023-25 International Institute of Information of Technology 6 INSERTION Cases 1. If tree is empty, create new node as root node with color black. 2. If tree is not empty, create new node as leaf node with color red. 3. If parent of new node is black then exit. 4. If parent of new node is red, then check the color of parent’s sibling of new node. a. If color is black or null then do suitable rotation and recolor. b. If color is red then recolor and also check if parent’s parents of new node is not root node then recolor it and recheck.
  • 7. National Institute of Science & Technology M. Tech 1th Sem (CSE) Batch:2023-25 International Institute of Information of Technology 7 Insertion Example- 10,18,7,15,16,30,25,40,60,2,1,70 16 1 25 2 7 40 18 30 70 60 10 15
  • 8. National Institute of Science & Technology M. Tech 1th Sem (CSE) Batch:2023-25 International Institute of Information of Technology 8 Analysis of Insertion • Search for insertion location takes O(log n) time because we visit O(log n) nodes. • Addition to the node takes O(1) time. • Rotation or recoloring takes O(log n) time. • O(log n) recoloring, each taking O(1) time, and at most one rotation taking O(1) time. • Thus, an insertion in a red-black tree takes O(log n) time.
  • 9. National Institute of Science & Technology M. Tech 1th Sem (CSE) Batch:2023-25 International Institute of Information of Technology 9 DELETION •Deleting a node from a red-black tree is a bit more complicated than inserting a node. •If the node is red? Not a problem – no RB properties violated. •If the node is black? deleting it will change the black-height along some path.
  • 10. National Institute of Science & Technology M. Tech 1th Sem (CSE) Batch:2023-25 International Institute of Information of Technology 10 Cases Case-1 •If node to be deleted is red, just delete it. Case-2 •If root is double black(DB) ,just remove DB. Case-3 •If DB’s sibling is black and both its children are black. •Remove DB •Add black to its parent (P) •If parent is red it becomes black •If parent is black it becomes double black • Make sibling red. • if still DB exist apply other cases.
  • 11. National Institute of Science & Technology M. Tech 1th Sem (CSE) Batch:2023-25 International Institute of Information of Technology 11 Cases Case-4 • If DB’s sibling red. • Swap color’s of parent its sibling of double black. • Rotated parent towards double black direction. • Reapply cases Case-5 • DB’s sibling is black, sibling’s child who is far from DB is black, but near child to DB is red. • Swap color of DB’s sibling and sibling child who is near to DB. • Rotate sibling in opposite direction to DB • Apply case-6
  • 12. National Institute of Science & Technology M. Tech 1th Sem (CSE) Batch:2023-25 International Institute of Information of Technology 12 Cases Case-6 • If DB’s sibling is black ,far child is red. • Swap color of parent and sibling. • Rotate parent in DB’s direction. • Remove DB. • Change color of red child to black.
  • 13. National Institute of Science & Technology M. Tech 1th Sem (CSE) Batch:2023-25 International Institute of Information of Technology 13 Analysis of Deletion • A red-black tree has O(log n) height • Search for deletion location takes O(log n) time • Each rotation or recoloring is O(1). • Thus, the deletion in a red-black tree takes O(log n) time
  • 14. National Institute of Science & Technology M. Tech 1th Sem (CSE) Batch:2023-25 International Institute of Information of Technology 14 CONCLUSION • Red-Black Trees are preferred in scenarios where there is a need for faster insertions and deletions, and the tree can tolerate a slightly less balanced structure. • AVL Trees may be preferred when a more strictly balanced tree is required, and the focus is on maintaining a consistent performance in search operations. •The choice between the two depends on the specific characteristics and requirements of the application.