SlideShare a Scribd company logo
1 of 13
Red-Black Trees

Niño Guerrero
Dexter Paul Gumahad
Rene Madera
Reymart Pagente
Data Structure || JRMSU Computer Science
Red-Black Trees
• A Red-Black tree is a binary search tree with
these traits:
– Every node is either red or black.
– The root is always black.
– If a node is red, its children must be black.
– Every path from any node to a null must have the
same number of black nodes.

• Operations on Red-Black trees are O(log N).
Data Structure || JRMSU Computer Science
Red-Black tree
c

a

f

b

d

g

e

Data Structure || JRMSU Computer Science
Red-Black Trees
• Red-black trees can be thought of as binary
tree representations of a 2-3-4 tree.
• A 2-3-4 tree is a B-tree of degree 4.
• This means each internal node has either 2, 3,
or 4 children.
• The next slide illustrates this.

Data Structure || JRMSU Computer Science
2-3-4 tree

c

a

b

f

d

e

Data Structure || JRMSU Computer Science

g
How the Trees Relate
c

c

a

f

d

e

f
a
b

d

b

g

g

e

Each black node represents a node in the 2-3-4 tree.
Each red node is in the same node with its black parent.
This explains why every path has the same number of black nodes.
Data Structure || JRMSU Computer Science
Mappings
a

a

b

a
or
a

b

a

b

b
a

b

c
a
Data Structure || JRMSU Computer Science

c
Inserting into a Red-Black Tree
• An inserted node is placed as in a binary
search tree as a red node, unless it is the root.
• If the parent of the new node is also red,
called a “double red”, the tree will have to be
adjusted, since a red child must have a black
parent.

Data Structure || JRMSU Computer Science
Two Cases
1.

a

u

w

c
b

2.

a

Inserting node z causes double red,
and z’s parent has black sibling w.
This occurs when a 4-node is
malformed: must restructure.

v

z

u
c

w
b

z

v

Inserting node z causes double red,
and z’s parent has red sibling w.
This occurs when a 4-node
overflows: must recolor.
Note: these are partial trees so not all
Data Structure || JRMSU Computer subtrees are shown.
of their Science
Restructuring
a

c

w

c

w

a

b

a
w

c
b

b
c

b

Double rotation

w

a

Single rotation

b

The malformed 4-node is
now a well-formed 4-node.

a

c

Data Structure || JRMSU Computer Science

Parent is black,
children are red.
Recoloring
b

b
w

d

a

w

d

a
c

c

b
w

a

b
c

w

a

d

Note: since b is now red, this can cause a double red with b’s parent.
If so, the two cases are considered again for Science its parent.
Data Structure || JRMSU Computer b and

c
d
Example
e

e

e
h

h
k

1. Insert e

2. Insert h

h

h

e

k

e

3. Insert k, h has
null (black) sibling
h
e

k
n

4. Restructure

5. Insert n, k has
red sibling
Data Structure || JRMSU Computer Science

k
n

6. Recolor (root left
black)
End

Data Structure || JRMSU Computer Science

More Related Content

What's hot

Back tracking and branch and bound class 20
Back tracking and branch and bound class 20Back tracking and branch and bound class 20
Back tracking and branch and bound class 20
Kumar
 
Pipeline and data hazard
Pipeline and data hazardPipeline and data hazard
Pipeline and data hazard
Waed Shagareen
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
Tech_MX
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
Nikhil Sharma
 

What's hot (20)

Red Black Tree
Red Black TreeRed Black Tree
Red Black Tree
 
Np complete
Np completeNp complete
Np complete
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Back tracking and branch and bound class 20
Back tracking and branch and bound class 20Back tracking and branch and bound class 20
Back tracking and branch and bound class 20
 
Production system in ai
Production system in aiProduction system in ai
Production system in ai
 
Elements of dynamic programming
Elements of dynamic programmingElements of dynamic programming
Elements of dynamic programming
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Avl trees
Avl treesAvl trees
Avl trees
 
Operating system Dead lock
Operating system Dead lockOperating system Dead lock
Operating system Dead lock
 
Deadlock Detection in Distributed Systems
Deadlock Detection in Distributed SystemsDeadlock Detection in Distributed Systems
Deadlock Detection in Distributed Systems
 
Merge sort
Merge sortMerge sort
Merge sort
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Pipeline and data hazard
Pipeline and data hazardPipeline and data hazard
Pipeline and data hazard
 
Huffman's algorithm in Data Structure
 Huffman's algorithm in Data Structure Huffman's algorithm in Data Structure
Huffman's algorithm in Data Structure
 
Fp growth
Fp growthFp growth
Fp growth
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Red Black Tree Insertion & Deletion
Red Black Tree Insertion & DeletionRed Black Tree Insertion & Deletion
Red Black Tree Insertion & Deletion
 

Viewers also liked

Algebra 1 2.8 Algebraic Proof
Algebra 1 2.8 Algebraic ProofAlgebra 1 2.8 Algebraic Proof
Algebra 1 2.8 Algebraic Proof
Jaqueline Vallejo
 
Struktur data 06 (red black tree)
Struktur data 06 (red black tree)Struktur data 06 (red black tree)
Struktur data 06 (red black tree)
Sunarya Marwah
 
Fundamentals of data structures ellis horowitz & sartaj sahni
Fundamentals of data structures   ellis horowitz & sartaj sahniFundamentals of data structures   ellis horowitz & sartaj sahni
Fundamentals of data structures ellis horowitz & sartaj sahni
Hitesh Wagle
 
Indexing and-hashing
Indexing and-hashingIndexing and-hashing
Indexing and-hashing
Ami Ranjit
 
Indexing and hashing
Indexing and hashingIndexing and hashing
Indexing and hashing
Jeet Poria
 

Viewers also liked (20)

Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
 
Red black tree
Red black treeRed black tree
Red black tree
 
Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
 
10 Red-Black Trees
10 Red-Black Trees10 Red-Black Trees
10 Red-Black Trees
 
Red black trees
Red black treesRed black trees
Red black trees
 
Algebra 1 2.8 Algebraic Proof
Algebra 1 2.8 Algebraic ProofAlgebra 1 2.8 Algebraic Proof
Algebra 1 2.8 Algebraic Proof
 
Insertion in RED BLACK TREE
Insertion in RED BLACK TREEInsertion in RED BLACK TREE
Insertion in RED BLACK TREE
 
Struktur data 06 (red black tree)
Struktur data 06 (red black tree)Struktur data 06 (red black tree)
Struktur data 06 (red black tree)
 
Splay trees by NIKHIL ARORA (www.internetnotes.in)
Splay trees by NIKHIL ARORA (www.internetnotes.in)Splay trees by NIKHIL ARORA (www.internetnotes.in)
Splay trees by NIKHIL ARORA (www.internetnotes.in)
 
Logic gates with laws
Logic gates with lawsLogic gates with laws
Logic gates with laws
 
Red black trees1109
Red black trees1109Red black trees1109
Red black trees1109
 
B-Tree
B-TreeB-Tree
B-Tree
 
B tree long
B tree longB tree long
B tree long
 
B+ Tree
B+ TreeB+ Tree
B+ Tree
 
B tree
B treeB tree
B tree
 
Fundamentals of data structures ellis horowitz & sartaj sahni
Fundamentals of data structures   ellis horowitz & sartaj sahniFundamentals of data structures   ellis horowitz & sartaj sahni
Fundamentals of data structures ellis horowitz & sartaj sahni
 
Indexing and-hashing
Indexing and-hashingIndexing and-hashing
Indexing and-hashing
 
B Trees
B TreesB Trees
B Trees
 
Best for b trees
Best for b treesBest for b trees
Best for b trees
 
Indexing and hashing
Indexing and hashingIndexing and hashing
Indexing and hashing
 

Similar to Red black trees presentation (12)

6_1 (1).ppt
6_1 (1).ppt6_1 (1).ppt
6_1 (1).ppt
 
redblacktreeindatastructure-200409083949.pptx
redblacktreeindatastructure-200409083949.pptxredblacktreeindatastructure-200409083949.pptx
redblacktreeindatastructure-200409083949.pptx
 
Advanced data structures and implementation
Advanced data structures and implementationAdvanced data structures and implementation
Advanced data structures and implementation
 
AA trees (20PT25 and 20PT27 - ADS Presentation)
AA trees (20PT25 and 20PT27 - ADS Presentation)AA trees (20PT25 and 20PT27 - ADS Presentation)
AA trees (20PT25 and 20PT27 - ADS Presentation)
 
lecture 14
lecture 14lecture 14
lecture 14
 
Lec14
Lec14Lec14
Lec14
 
Week 8 (trees)
Week 8 (trees)Week 8 (trees)
Week 8 (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
 
Red Black Tree,Red black tree, Cases-insertion, Deletion
Red Black Tree,Red black tree, Cases-insertion, DeletionRed Black Tree,Red black tree, Cases-insertion, Deletion
Red Black Tree,Red black tree, Cases-insertion, Deletion
 
Red black tree
Red black treeRed black tree
Red black tree
 
Red-black trees
Red-black treesRed-black trees
Red-black trees
 
Red black trees and their properties
Red black trees and their propertiesRed black trees and their properties
Red black trees and their properties
 

More from Dexter Paul Gumahad

Glord's Brochure || Created by Me Using MS Word
Glord's Brochure || Created by Me Using MS WordGlord's Brochure || Created by Me Using MS Word
Glord's Brochure || Created by Me Using MS Word
Dexter Paul Gumahad
 
Lacason's Brochure || Designed by Me Using MS Word
Lacason's Brochure || Designed by Me Using MS WordLacason's Brochure || Designed by Me Using MS Word
Lacason's Brochure || Designed by Me Using MS Word
Dexter Paul Gumahad
 
Tulisana's Brochure Presentation || Designed by Me Using MS Powerpoint
Tulisana's Brochure Presentation || Designed by Me Using MS PowerpointTulisana's Brochure Presentation || Designed by Me Using MS Powerpoint
Tulisana's Brochure Presentation || Designed by Me Using MS Powerpoint
Dexter Paul Gumahad
 
Tulisana's Brochure || Designed by Me Using MS Word & Adobe PS
Tulisana's Brochure || Designed by Me Using MS Word & Adobe PSTulisana's Brochure || Designed by Me Using MS Word & Adobe PS
Tulisana's Brochure || Designed by Me Using MS Word & Adobe PS
Dexter Paul Gumahad
 
Valerie's Brochure || Designed by Me Using MS Word @ Adobe PS
Valerie's Brochure || Designed by Me Using MS Word @ Adobe PSValerie's Brochure || Designed by Me Using MS Word @ Adobe PS
Valerie's Brochure || Designed by Me Using MS Word @ Adobe PS
Dexter Paul Gumahad
 

More from Dexter Paul Gumahad (6)

Glord's Brochure || Created by Me Using MS Word
Glord's Brochure || Created by Me Using MS WordGlord's Brochure || Created by Me Using MS Word
Glord's Brochure || Created by Me Using MS Word
 
Lacason's Brochure || Designed by Me Using MS Word
Lacason's Brochure || Designed by Me Using MS WordLacason's Brochure || Designed by Me Using MS Word
Lacason's Brochure || Designed by Me Using MS Word
 
Tulisana's Brochure Presentation || Designed by Me Using MS Powerpoint
Tulisana's Brochure Presentation || Designed by Me Using MS PowerpointTulisana's Brochure Presentation || Designed by Me Using MS Powerpoint
Tulisana's Brochure Presentation || Designed by Me Using MS Powerpoint
 
Tulisana's Brochure || Designed by Me Using MS Word & Adobe PS
Tulisana's Brochure || Designed by Me Using MS Word & Adobe PSTulisana's Brochure || Designed by Me Using MS Word & Adobe PS
Tulisana's Brochure || Designed by Me Using MS Word & Adobe PS
 
Valerie's Brochure || Designed by Me Using MS Word @ Adobe PS
Valerie's Brochure || Designed by Me Using MS Word @ Adobe PSValerie's Brochure || Designed by Me Using MS Word @ Adobe PS
Valerie's Brochure || Designed by Me Using MS Word @ Adobe PS
 
Bayobay's Programme || My Work - Simpliest
Bayobay's Programme || My Work - SimpliestBayobay's Programme || My Work - Simpliest
Bayobay's Programme || My Work - Simpliest
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 

Red black trees presentation

  • 1. Red-Black Trees Niño Guerrero Dexter Paul Gumahad Rene Madera Reymart Pagente Data Structure || JRMSU Computer Science
  • 2. Red-Black Trees • A Red-Black tree is a binary search tree with these traits: – Every node is either red or black. – The root is always black. – If a node is red, its children must be black. – Every path from any node to a null must have the same number of black nodes. • Operations on Red-Black trees are O(log N). Data Structure || JRMSU Computer Science
  • 4. Red-Black Trees • Red-black trees can be thought of as binary tree representations of a 2-3-4 tree. • A 2-3-4 tree is a B-tree of degree 4. • This means each internal node has either 2, 3, or 4 children. • The next slide illustrates this. Data Structure || JRMSU Computer Science
  • 5. 2-3-4 tree c a b f d e Data Structure || JRMSU Computer Science g
  • 6. How the Trees Relate c c a f d e f a b d b g g e Each black node represents a node in the 2-3-4 tree. Each red node is in the same node with its black parent. This explains why every path has the same number of black nodes. Data Structure || JRMSU Computer Science
  • 8. Inserting into a Red-Black Tree • An inserted node is placed as in a binary search tree as a red node, unless it is the root. • If the parent of the new node is also red, called a “double red”, the tree will have to be adjusted, since a red child must have a black parent. Data Structure || JRMSU Computer Science
  • 9. Two Cases 1. a u w c b 2. a Inserting node z causes double red, and z’s parent has black sibling w. This occurs when a 4-node is malformed: must restructure. v z u c w b z v Inserting node z causes double red, and z’s parent has red sibling w. This occurs when a 4-node overflows: must recolor. Note: these are partial trees so not all Data Structure || JRMSU Computer subtrees are shown. of their Science
  • 10. Restructuring a c w c w a b a w c b b c b Double rotation w a Single rotation b The malformed 4-node is now a well-formed 4-node. a c Data Structure || JRMSU Computer Science Parent is black, children are red.
  • 11. Recoloring b b w d a w d a c c b w a b c w a d Note: since b is now red, this can cause a double red with b’s parent. If so, the two cases are considered again for Science its parent. Data Structure || JRMSU Computer b and c d
  • 12. Example e e e h h k 1. Insert e 2. Insert h h h e k e 3. Insert k, h has null (black) sibling h e k n 4. Restructure 5. Insert n, k has red sibling Data Structure || JRMSU Computer Science k n 6. Recolor (root left black)
  • 13. End Data Structure || JRMSU Computer Science