SlideShare a Scribd company logo
Balanced Search Trees
R-B Trees
Subject- Design and Analysis of Algorithms
Submitted by- Prabhat Mishra
Abhiraj Chaudhry
RED-BLACK TREES
 Definition: a binary search tree with nodes colored
red and black such that:
 the paths from the root to any leaf have the same
number of black nodes,
 there are no two consecutive red nodes, If a node is
red, then both of its children are black
 the root is black.
A red-black tree of height for h even and h odd
The total number of leaves is of the form:
1 + 2i1
+ 2i2
+ 2i3
+· · ·+2ih
,
So for h even the number of leaves is:
1 + 2(20
+ 21
+ 22
+· · ·+2(h/2)−1)
= 2(h/2)+1
− 1,
and for h odd it is:
1 + 2(20
+ 21
+ 22
+· · ·+2((h−1)/2)−1
) + 2(h−1)/2
= 2(h−1)/2
− 1
So the worst-case height of a red-black tree is really 2 log n − O(1).
Definition: The black-height of a node, x, in a red-black tree is
the number of black nodes on any path to a leaf, not counting x.
The height of red-black tree
Black-Height of the root = 2
Black-Height of the root = 3
Height-balanced trees
We have to maintain the following balancedness property
- Each path from the root to a leaf contains the same number of black
nodes
7
3 18
10 22
26
11
8
bh =2
bh =2
bh =1
bh =1 bh =1
bh =1
bh =1
• 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
* We have some cases for deletion
P
S
U
V
Case A:
- V’s sibling, S, is Red
Rotate S around P and recolor S & P
delete
P
S
V
P
S
V
Rotate S around P
P
V
S
Recolor S
& P
P
S
U
V
Case B:
- V’s sibling, S, is black and has two black children.
Recolor S to be Red
delete
Red or Black and don’t care
P
S
V
P
S
V
Recolor S to be Red
P
S
U
V
Case C:
- S is black
S’s RIGHT child is RED (Left child either color)
Rotate S around P
Swap colors of S and P, and color S’s Right child
Black
delete
P
S
V
P
S
V
Rotate S around P
P
S
V
Recolor: Swap colors of S
and P, and color S’s Right
child Black
P
S
U
V
delete
Case D:
- S is Black, S’s right child is Black and S’s left child is
Red
i) Rotate S’s left child around S
ii) Swap color of S and S’s left child
P
S
V
P
S
V
Rotate S’s
left child
around S
P
S
V
Recolor: Swap
color of S and S’s
left child
Analysis of deletion
- A red-black tree has O(log n) height
- Search for deletion location takes O(log n) time
- The swaping and deletion is O(1).
- Each rotation or recoloring is O(1).
- Thus, the deletion in a red-black tree takes O(log n)
time

More Related Content

PPTX
Data structure
PPTX
Red black trees1109
PDF
Red-Black Tree Presentation By Mobin Nesari.pdf
PPTX
Red black trees
PPTX
Kishan Kaushik - Red Black Tree Presentation
PDF
Red black trees
PPTX
Red black trees
PPT
Red black 1
Data structure
Red black trees1109
Red-Black Tree Presentation By Mobin Nesari.pdf
Red black trees
Kishan Kaushik - Red Black Tree Presentation
Red black trees
Red black trees
Red black 1

Similar to Red-black-tree presentation in Algorithm (20)

PPT
16 rbtrees
PDF
Cse 225 rbt_red_black_tree
PPT
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
PPT
rbtrees.ppt
PPTX
Red black tree
PPTX
Red black tree
PPTX
Red Black Tree,Red black tree, Cases-insertion, Deletion
PPTX
Balanced Tree (AVL Tree & Red-Black Tree)
PPTX
Red black trees
PPTX
Balanced Tree(AVL Tree,Red Black Tree)
PDF
Red black tree
PDF
anastasio-red-black-trees-1-1-091222204455-phpapp02.pdf
PPT
lecture 14
PPTX
Red black tree
PPT
Red blacktrees
PPTX
red black tree.pptxMMMMMMMMMMMMMMMMMMMMMMMMMM
PDF
02_balanced_trees for pitt dsa yayay.pdf
PPT
RedBlackTrees_2.pptNNNNNNNNNNNNNNNNNNNNNN
PPT
RedBlackTrees_2.pptmmmmmmmmmmmmmmmmmmmmmmmmmm
16 rbtrees
Cse 225 rbt_red_black_tree
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
rbtrees.ppt
Red black tree
Red black tree
Red Black Tree,Red black tree, Cases-insertion, Deletion
Balanced Tree (AVL Tree & Red-Black Tree)
Red black trees
Balanced Tree(AVL Tree,Red Black Tree)
Red black tree
anastasio-red-black-trees-1-1-091222204455-phpapp02.pdf
lecture 14
Red black tree
Red blacktrees
red black tree.pptxMMMMMMMMMMMMMMMMMMMMMMMMMM
02_balanced_trees for pitt dsa yayay.pdf
RedBlackTrees_2.pptNNNNNNNNNNNNNNNNNNNNNN
RedBlackTrees_2.pptmmmmmmmmmmmmmmmmmmmmmmmmmm
Ad

More from PRABHATMISHRA969924 (12)

PPTX
B best on presentation on tree ppt.pptx
PPTX
Cloud Computing fcc best on clouds ppt.pptx
PPTX
crm-240313041817-d17a72e3 (1).pptx best one
PPTX
PPT WT Prabhat.pptx quantum university ppt
PPTX
CRM PPT -1.pptxon crm rules good ppt on crm
PPTX
Learning Management System PPT on LMS Project
PPTX
Gulshan serialization inJava PPT ex.pptx
PPTX
Control Bulb using SmartPhone IOT .pptx
PDF
Iot presentation report of project LED.pdf
PDF
Ace the Aptitude Evenet management and Rules for Participants
PPTX
INTERNSHIP PRESENTATION prabhat.pptx QU ROORKE
PDF
PPT presentation about Death penalty is not right
B best on presentation on tree ppt.pptx
Cloud Computing fcc best on clouds ppt.pptx
crm-240313041817-d17a72e3 (1).pptx best one
PPT WT Prabhat.pptx quantum university ppt
CRM PPT -1.pptxon crm rules good ppt on crm
Learning Management System PPT on LMS Project
Gulshan serialization inJava PPT ex.pptx
Control Bulb using SmartPhone IOT .pptx
Iot presentation report of project LED.pdf
Ace the Aptitude Evenet management and Rules for Participants
INTERNSHIP PRESENTATION prabhat.pptx QU ROORKE
PPT presentation about Death penalty is not right
Ad

Recently uploaded (20)

PDF
Well-logging-methods_new................
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Construction Project Organization Group 2.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
DOCX
573137875-Attendance-Management-System-original
PPTX
Lecture Notes Electrical Wiring System Components
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Sustainable Sites - Green Building Construction
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Digital Logic Computer Design lecture notes
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Well-logging-methods_new................
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
CYBER-CRIMES AND SECURITY A guide to understanding
Automation-in-Manufacturing-Chapter-Introduction.pdf
Construction Project Organization Group 2.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Foundation to blockchain - A guide to Blockchain Tech
573137875-Attendance-Management-System-original
Lecture Notes Electrical Wiring System Components
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
CH1 Production IntroductoryConcepts.pptx
Sustainable Sites - Green Building Construction
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Digital Logic Computer Design lecture notes
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...

Red-black-tree presentation in Algorithm

  • 1. Balanced Search Trees R-B Trees Subject- Design and Analysis of Algorithms Submitted by- Prabhat Mishra Abhiraj Chaudhry
  • 2. RED-BLACK TREES  Definition: a binary search tree with nodes colored red and black such that:  the paths from the root to any leaf have the same number of black nodes,  there are no two consecutive red nodes, If a node is red, then both of its children are black  the root is black.
  • 3. A red-black tree of height for h even and h odd The total number of leaves is of the form: 1 + 2i1 + 2i2 + 2i3 +· · ·+2ih , So for h even the number of leaves is: 1 + 2(20 + 21 + 22 +· · ·+2(h/2)−1) = 2(h/2)+1 − 1, and for h odd it is: 1 + 2(20 + 21 + 22 +· · ·+2((h−1)/2)−1 ) + 2(h−1)/2 = 2(h−1)/2 − 1 So the worst-case height of a red-black tree is really 2 log n − O(1).
  • 4. Definition: The black-height of a node, x, in a red-black tree is the number of black nodes on any path to a leaf, not counting x. The height of red-black tree Black-Height of the root = 2
  • 6. Height-balanced trees We have to maintain the following balancedness property - Each path from the root to a leaf contains the same number of black nodes 7 3 18 10 22 26 11 8 bh =2 bh =2 bh =1 bh =1 bh =1 bh =1 bh =1
  • 7. • 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
  • 8. * We have some cases for deletion P S U V Case A: - V’s sibling, S, is Red Rotate S around P and recolor S & P delete
  • 9. P S V P S V Rotate S around P P V S Recolor S & P
  • 10. P S U V Case B: - V’s sibling, S, is black and has two black children. Recolor S to be Red delete Red or Black and don’t care
  • 12. P S U V Case C: - S is black S’s RIGHT child is RED (Left child either color) Rotate S around P Swap colors of S and P, and color S’s Right child Black delete
  • 13. P S V P S V Rotate S around P P S V Recolor: Swap colors of S and P, and color S’s Right child Black
  • 14. P S U V delete Case D: - S is Black, S’s right child is Black and S’s left child is Red i) Rotate S’s left child around S ii) Swap color of S and S’s left child
  • 15. P S V P S V Rotate S’s left child around S P S V Recolor: Swap color of S and S’s left child
  • 16. Analysis of deletion - A red-black tree has O(log n) height - Search for deletion location takes O(log n) time - The swaping and deletion is O(1). - Each rotation or recoloring is O(1). - Thus, the deletion in a red-black tree takes O(log n) time