SlideShare a Scribd company logo
1 of 37
Download to read offline
Data Structures and Algorithms
Amrita Vishwa Vidyapeetham
36 pag.
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
Red-Black Trees
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
2
Red-Black Trees
• “Balanced” binary trees guarantee an
running time on the basic dynamic-set
operations
• Red-black tree
– Binary tree with an additional attribute for its nodes:
color which can be red or black
– The nodes inherit all the other attributes from the
binary-search trees: key, left, right, p
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
O(lgn)
Red-Black Trees
• Constrains the way nodes can be colored on
any path from the root to a leaf.
– Ensures that no path is more than twice as long as
another ⇒ the tree is balanced
Balanced tree  O(logN)
3
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
Example: RED-BLACK-TREE
• For convenience, we add NIL nodes and refer to them as
the leaves of the tree.
– Color[NIL] = BLACK
26
17 41
30 47
38 50
NIL NIL
NIL
NIL NIL NIL NIL
NIL
4
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
Red-Black-Trees Properties
(**Binary search tree property is satisfied**)
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
• No two consecutive red nodes on a simple path from
the root to a leaf
5. For each node, all paths from that node to a leaf
contain the same number of black nodes
5
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
Definitions
• Height of a node: the number of edges in the longest
path to a leaf.
• Black-height bh(x) of a node x: the number of black
nodes (including NIL) on the path from x to a leaf, not
counting x.
26
17 41
30 47
38 50
NIL NIL
NIL
NIL NIL NIL NIL
NIL
h = 4
bh = 2
h = 3
bh = 2
h = 2
bh = 1
h = 1
bh = 1
h = 1
bh = 1
h = 2
bh = 1 h = 1
bh = 1
6
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
Height of Red-Black-Trees
A red-black tree with n internal nodes
has height at most 2log(N+1)
• Need to prove two claims first …
7
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
Claim 1
• Any node x with height h(x) has bh(x) ≥ h(x)/2
• Proof
– By property 4, at most h/2 red nodes on the path from
the node to a leaf
– Hence, at least h/2 are black
26
17 41
30 47
38 50
NIL NIL
NIL
NIL NIL NIL NIL
NIL
h = 4
bh = 2
h = 3
bh = 2
h = 2
bh = 1
h = 1
bh = 1
h = 1
bh = 1
h = 2
bh = 1 h = 1
bh = 1
8
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
Claim 2
• The subtree rooted at any node x contains
at least 2bh(x) - 1 internal nodes
26
17 41
30 47
38 50
NIL NIL
NIL
NIL NIL NIL NIL
NIL
h = 4
bh = 2
h = 3
bh = 2
h = 2
bh = 1
h = 1
bh = 1
h = 1
bh = 1
h = 2
bh = 1 h = 1
bh = 1
9
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
Claim 2 (cont’d)
Proof: By induction on h[x]
Basis: h[x] = 0 ⇒
x is a leaf (NIL[T]) ⇒
bh(x) = 0 ⇒
# of internal nodes: 20 - 1 = 0
Inductive Hypothesis: assume it is true for
h[x]=h-1
Inductive step:
• Prove it for h[x]=h
NIL
x
10
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
Claim 2 (cont’d)
Prove it for h[x]=h
internal nodes at x=
internal nodes at l +
internal nodes at r + 1
Using inductive hypothesis:
internal nodes at x ≥ (2bh(l) – 1) + (2bh(r) – 1) + 1
x
l r
h
h-1
11
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
Claim 2 (cont’d)
• Let bh(x) = b,
• then any child y of x has:
– bh (y) =
– bh (y) =
b (if the child is red), or
b - 1 (if the child is black)
26
17 41
30 47
38 50
x
y1 y2
bh = 2
bh = 2
bh = 1
NIL NIL
NIL NIL
NIL
NIL
bh(y)≥bh(x)-1
12
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
Claim 2 (cont’d)
• So, back to our proof:
internal nodes at x ≥ (2bh(l) – 1) + (2bh(r) – 1) + 1
≥ (2bh(x) - 1 – 1) + (2bh(x) - 1 – 1) + 1
= 2 · (2bh(x) - 1 - 1) + 1
= 2bh(x) - 1 internal nodes
x
l r
h
h-1
13
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
Height of Red-Black-Trees (cont’d)
A red-black tree with N internal nodes has height
at most 2log(N+1).
Proof:
N
• Solve for h:
N + 1 ≥ 2h/2
log(N + 1) ≥ h/2 ⇒
h ≤ 2 log(N + 1)
root
l r
height(root) = h
bh(root) = b
number
of internal
nodes
≥ 2b - 1 ≥ 2h/2 - 1
Claim 2 Claim 1
14
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
Operations on Red-Black-Trees
• Non-modifying binary-search-tree operations (e.g.,
RetrieveItem, GetNextItem, ResetTree), run in
O(h)=O(logN) time
• What about InsertItem and DeleteItem?
– They will still run on O(logN)
– Need to guarantee that the modified tree will still be a
valid red-black tree
15
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
16
InsertItem
What color to make the new node?
• Red? Let’s insert 35!
– Property 4 is violated: if a node is red, then both its
children are black
• Black? Let’s insert 14!
– Property 5 is violated: all paths from a node to its leaves
contain the same number of black nodes
26
17 41
30 47
38 50
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
Rotations
• Operations for re-structuring the tree after insert
and delete operations
– Together with some node re-coloring, they help restore
the red-black-tree property
– Change some of the pointer structure
– Preserve the binary-search tree property
• Two types of rotations:
– Left & right rotations
17
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
Left Rotations
• Assumptions for a left rotation on a node x:
– The right child y of x is not NIL
• Idea:
– Pivots around the link from x to y
– Makes y the new root of the subtree
– x becomes y’s left child
– y’s left child becomes x’s right child
18
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
19
Example: LEFT-ROTATE
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
Right Rotations
• Assumptions for a right rotation on a node x:
– The left child x of y 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
20
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
InsertItem
• Goal:
– Insert a new node z into a red-black tree
• Idea:
– Insert node z into the tree as for an ordinary binary
search tree
– Color the node red
– Restore the red-black tree properties
• i.e., use RB-INSERT-FIXUP
21
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
RB Properties Affected by Insert
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 paths
from the node to descendant
leaves contain the same number
of black nodes
OK!
If z is the root
⇒ not OK
OK!
26
17 41
47
38
50
If p(z) is red ⇒ not OK
z and p(z) are both red
OK!
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
23
RB-INSERT-FIXUP
Case 1: z’s “uncle” (y) is red
(z could be either left or right child)
Idea:
• p[p[z]] (z’s grandparent) must be black
• color p[z] ← black
• color y ← black
• color p[p[z]] ← red
• z = p[p[z]]
– Push the “red” violation up the tree
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
RB-INSERT-FIXUP
Case 2:
• z’s “uncle” (y) is black
• z is a left child
Case 2
Idea:
• color p[z] ← black
• color p[p[z]] ← red
• RIGHT-ROTATE(T, p[p[z]])
• No longer have 2 reds in a row
• p[z] is now black
24
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
25
RB-INSERT-FIXUP
Case 3:
• z’s “uncle” (y) is black
• z is a right child
Idea:
• z ← p[z]
• LEFT-ROTATE(T, z)
⇒ now z is a left child, and both z and p[z] are red ⇒ case 2
Case 3 Case 2
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
Example
11
Insert 4
2 14
1 15
7
8
5
4
y
11
2 14
1 15
7
8
5
4
z
Case 1
y
z and p[z] are both red
z’s uncle y is red
z
z and p[z] are both red
z’s uncle y is black
z is a right child
Case 3
11
2
14
1
15
7
8
5
4
z
y
Case 2
z and p[z] are red
z’s uncle y is black
z is a left child
11
2
14
1
15
7
8
5
4
z
26
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
RB-INSERT-FIXUP(T, z)
1. while color[p[z]] = RED
2. do if p[z] = left[p[p[z]]]
3. then y ← right[p[p[z]]]
4. if color[y] = RED
5. then Case1
6. else if z = right[p[z]]
7. then Case2
8. Case3
9. else (same as then clause with “right” and “left”
exchanged)
10. color[root[T]] ← BLACK
The while loop repeats only when
case1 is executed: O(logN) times
Set the value of x’s “uncle”
We just inserted the root, or
The red violation reached the root
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
Analysis of InsertItem
• Inserting the new element into the tree O(logN)
• RB-INSERT-FIXUP
– The while loop repeats only if CASE 1 is executed
– The number of times the while loop can be executed
is O(logN)
• Total running time of InsertItem: O(logN)
28
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
DeleteItem
• Delete as usually, then re-color/rotate
• A bit more complicated though …
• Demo
– http://gauss.ececs.uc.edu/RedBlack/redblack.html
29
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
DeleteItem
What color was the
node that was removed? Red?
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 paths from the node to descendant
leaves contain the same number of black nodes
OK!
OK!
OK!
OK! Does not create
two red nodes in a row
Not OK! Could change the
black heights of some nodes
26
17 41
30 47
38 50
30
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
DeleteItem
What color was the
node that was removed? Black?
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 paths from the node to descendant
leaves contain the same number of black nodes
OK!
OK!
Not OK! Could create
two red nodes in a row
Not OK! Could change the
black heights of some nodes
26
17 41
30 47
38 50
Not OK! If removing
the root and the child
that replaces it is red
31
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
Red-Black Trees - Summary
• Operations on red-black trees:
– SEARCH O(h)
– PREDECESSOR O(h)
– SUCCESOR O(h)
– MINIMUM O(h)
– MAXIMUM O(h)
– INSERT O(h)
– DELETE O(h)
• Red-black trees guarantee that the height of the
tree will be O(lgn)
CS 477/677 - Lecture 12
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
Problems
33
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
Problems
• What red-black tree property is violated in the tree below?
How would you restore the red-black tree property in this
case?
– Property violated: if a node is red, both its children are black
– Fixup: color 7 black, 11 red, then right-rotate around 11
11
2
14
1
15
7
8
5
4
z
34
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
Problems
• Let a, b, c be arbitrary nodes in subtrees α, β, γ 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
– c: decreases by 1
35
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
Problems
• When we insert a node into a red-black tree, we
initially set the color of the new node to red.
• Why didn’t we choose to set the color to black?
• Would inserting a new node to a red-black tree
and then immediately deleting it, change the
tree?
36
Docsity.com
Document shared on www.docsity.com
Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)

More Related Content

Recently uploaded

Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
MaherOthman7
 
Artificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdfArtificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdf
Kira Dess
 
electrical installation and maintenance.
electrical installation and maintenance.electrical installation and maintenance.
electrical installation and maintenance.
benjamincojr
 
21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx
rahulmanepalli02
 

Recently uploaded (20)

Passive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.pptPassive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.ppt
 
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
 
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)
 
Developing a smart system for infant incubators using the internet of things ...
Developing a smart system for infant incubators using the internet of things ...Developing a smart system for infant incubators using the internet of things ...
Developing a smart system for infant incubators using the internet of things ...
 
Artificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdfArtificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdf
 
CLOUD COMPUTING SERVICES - Cloud Reference Modal
CLOUD COMPUTING SERVICES - Cloud Reference ModalCLOUD COMPUTING SERVICES - Cloud Reference Modal
CLOUD COMPUTING SERVICES - Cloud Reference Modal
 
21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological university21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological university
 
Artificial Intelligence in due diligence
Artificial Intelligence in due diligenceArtificial Intelligence in due diligence
Artificial Intelligence in due diligence
 
Raashid final report on Embedded Systems
Raashid final report on Embedded SystemsRaashid final report on Embedded Systems
Raashid final report on Embedded Systems
 
15-Minute City: A Completely New Horizon
15-Minute City: A Completely New Horizon15-Minute City: A Completely New Horizon
15-Minute City: A Completely New Horizon
 
Software Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdfSoftware Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdf
 
Interfacing Analog to Digital Data Converters ee3404.pdf
Interfacing Analog to Digital Data Converters ee3404.pdfInterfacing Analog to Digital Data Converters ee3404.pdf
Interfacing Analog to Digital Data Converters ee3404.pdf
 
Filters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsFilters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility Applications
 
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 ...
 
electrical installation and maintenance.
electrical installation and maintenance.electrical installation and maintenance.
electrical installation and maintenance.
 
21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx
 
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxSLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
 
UNIT-2 image enhancement.pdf Image Processing Unit 2 AKTU
UNIT-2 image enhancement.pdf Image Processing Unit 2 AKTUUNIT-2 image enhancement.pdf Image Processing Unit 2 AKTU
UNIT-2 image enhancement.pdf Image Processing Unit 2 AKTU
 
engineering chemistry power point presentation
engineering chemistry  power point presentationengineering chemistry  power point presentation
engineering chemistry power point presentation
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

docsity-red-black-trees-data-structures-lecture-slides_2.pdf

  • 1. Data Structures and Algorithms Amrita Vishwa Vidyapeetham 36 pag. Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 2. Red-Black Trees Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 3. 2 Red-Black Trees • “Balanced” binary trees guarantee an running time on the basic dynamic-set operations • Red-black tree – Binary tree with an additional attribute for its nodes: color which can be red or black – The nodes inherit all the other attributes from the binary-search trees: key, left, right, p Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com) O(lgn)
  • 4. Red-Black Trees • Constrains the way nodes can be colored on any path from the root to a leaf. – Ensures that no path is more than twice as long as another ⇒ the tree is balanced Balanced tree  O(logN) 3 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 5. Example: RED-BLACK-TREE • For convenience, we add NIL nodes and refer to them as the leaves of the tree. – Color[NIL] = BLACK 26 17 41 30 47 38 50 NIL NIL NIL NIL NIL NIL NIL NIL 4 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 6. Red-Black-Trees Properties (**Binary search tree property is satisfied**) 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 • No two consecutive red nodes on a simple path from the root to a leaf 5. For each node, all paths from that node to a leaf contain the same number of black nodes 5 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 7. Definitions • Height of a node: the number of edges in the longest path to a leaf. • Black-height bh(x) of a node x: the number of black nodes (including NIL) on the path from x to a leaf, not counting x. 26 17 41 30 47 38 50 NIL NIL NIL NIL NIL NIL NIL NIL h = 4 bh = 2 h = 3 bh = 2 h = 2 bh = 1 h = 1 bh = 1 h = 1 bh = 1 h = 2 bh = 1 h = 1 bh = 1 6 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 8. Height of Red-Black-Trees A red-black tree with n internal nodes has height at most 2log(N+1) • Need to prove two claims first … 7 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 9. Claim 1 • Any node x with height h(x) has bh(x) ≥ h(x)/2 • Proof – By property 4, at most h/2 red nodes on the path from the node to a leaf – Hence, at least h/2 are black 26 17 41 30 47 38 50 NIL NIL NIL NIL NIL NIL NIL NIL h = 4 bh = 2 h = 3 bh = 2 h = 2 bh = 1 h = 1 bh = 1 h = 1 bh = 1 h = 2 bh = 1 h = 1 bh = 1 8 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 10. Claim 2 • The subtree rooted at any node x contains at least 2bh(x) - 1 internal nodes 26 17 41 30 47 38 50 NIL NIL NIL NIL NIL NIL NIL NIL h = 4 bh = 2 h = 3 bh = 2 h = 2 bh = 1 h = 1 bh = 1 h = 1 bh = 1 h = 2 bh = 1 h = 1 bh = 1 9 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 11. Claim 2 (cont’d) Proof: By induction on h[x] Basis: h[x] = 0 ⇒ x is a leaf (NIL[T]) ⇒ bh(x) = 0 ⇒ # of internal nodes: 20 - 1 = 0 Inductive Hypothesis: assume it is true for h[x]=h-1 Inductive step: • Prove it for h[x]=h NIL x 10 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 12. Claim 2 (cont’d) Prove it for h[x]=h internal nodes at x= internal nodes at l + internal nodes at r + 1 Using inductive hypothesis: internal nodes at x ≥ (2bh(l) – 1) + (2bh(r) – 1) + 1 x l r h h-1 11 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 13. Claim 2 (cont’d) • Let bh(x) = b, • then any child y of x has: – bh (y) = – bh (y) = b (if the child is red), or b - 1 (if the child is black) 26 17 41 30 47 38 50 x y1 y2 bh = 2 bh = 2 bh = 1 NIL NIL NIL NIL NIL NIL bh(y)≥bh(x)-1 12 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 14. Claim 2 (cont’d) • So, back to our proof: internal nodes at x ≥ (2bh(l) – 1) + (2bh(r) – 1) + 1 ≥ (2bh(x) - 1 – 1) + (2bh(x) - 1 – 1) + 1 = 2 · (2bh(x) - 1 - 1) + 1 = 2bh(x) - 1 internal nodes x l r h h-1 13 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 15. Height of Red-Black-Trees (cont’d) A red-black tree with N internal nodes has height at most 2log(N+1). Proof: N • Solve for h: N + 1 ≥ 2h/2 log(N + 1) ≥ h/2 ⇒ h ≤ 2 log(N + 1) root l r height(root) = h bh(root) = b number of internal nodes ≥ 2b - 1 ≥ 2h/2 - 1 Claim 2 Claim 1 14 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 16. Operations on Red-Black-Trees • Non-modifying binary-search-tree operations (e.g., RetrieveItem, GetNextItem, ResetTree), run in O(h)=O(logN) time • What about InsertItem and DeleteItem? – They will still run on O(logN) – Need to guarantee that the modified tree will still be a valid red-black tree 15 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 17. 16 InsertItem What color to make the new node? • Red? Let’s insert 35! – Property 4 is violated: if a node is red, then both its children are black • Black? Let’s insert 14! – Property 5 is violated: all paths from a node to its leaves contain the same number of black nodes 26 17 41 30 47 38 50 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 18. Rotations • Operations for re-structuring the tree after insert and delete operations – Together with some node re-coloring, they help restore the red-black-tree property – Change some of the pointer structure – Preserve the binary-search tree property • Two types of rotations: – Left & right rotations 17 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 19. Left Rotations • Assumptions for a left rotation on a node x: – The right child y of x is not NIL • Idea: – Pivots around the link from x to y – Makes y the new root of the subtree – x becomes y’s left child – y’s left child becomes x’s right child 18 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 20. 19 Example: LEFT-ROTATE Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 21. Right Rotations • Assumptions for a right rotation on a node x: – The left child x of y 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 20 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 22. InsertItem • Goal: – Insert a new node z into a red-black tree • Idea: – Insert node z into the tree as for an ordinary binary search tree – Color the node red – Restore the red-black tree properties • i.e., use RB-INSERT-FIXUP 21 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 23. RB Properties Affected by Insert 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 paths from the node to descendant leaves contain the same number of black nodes OK! If z is the root ⇒ not OK OK! 26 17 41 47 38 50 If p(z) is red ⇒ not OK z and p(z) are both red OK! Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 24. 23 RB-INSERT-FIXUP Case 1: z’s “uncle” (y) is red (z could be either left or right child) Idea: • p[p[z]] (z’s grandparent) must be black • color p[z] ← black • color y ← black • color p[p[z]] ← red • z = p[p[z]] – Push the “red” violation up the tree Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 25. RB-INSERT-FIXUP Case 2: • z’s “uncle” (y) is black • z is a left child Case 2 Idea: • color p[z] ← black • color p[p[z]] ← red • RIGHT-ROTATE(T, p[p[z]]) • No longer have 2 reds in a row • p[z] is now black 24 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 26. 25 RB-INSERT-FIXUP Case 3: • z’s “uncle” (y) is black • z is a right child Idea: • z ← p[z] • LEFT-ROTATE(T, z) ⇒ now z is a left child, and both z and p[z] are red ⇒ case 2 Case 3 Case 2 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 27. Example 11 Insert 4 2 14 1 15 7 8 5 4 y 11 2 14 1 15 7 8 5 4 z Case 1 y z and p[z] are both red z’s uncle y is red z z and p[z] are both red z’s uncle y is black z is a right child Case 3 11 2 14 1 15 7 8 5 4 z y Case 2 z and p[z] are red z’s uncle y is black z is a left child 11 2 14 1 15 7 8 5 4 z 26 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 28. RB-INSERT-FIXUP(T, z) 1. while color[p[z]] = RED 2. do if p[z] = left[p[p[z]]] 3. then y ← right[p[p[z]]] 4. if color[y] = RED 5. then Case1 6. else if z = right[p[z]] 7. then Case2 8. Case3 9. else (same as then clause with “right” and “left” exchanged) 10. color[root[T]] ← BLACK The while loop repeats only when case1 is executed: O(logN) times Set the value of x’s “uncle” We just inserted the root, or The red violation reached the root Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 29. Analysis of InsertItem • Inserting the new element into the tree O(logN) • RB-INSERT-FIXUP – The while loop repeats only if CASE 1 is executed – The number of times the while loop can be executed is O(logN) • Total running time of InsertItem: O(logN) 28 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 30. DeleteItem • Delete as usually, then re-color/rotate • A bit more complicated though … • Demo – http://gauss.ececs.uc.edu/RedBlack/redblack.html 29 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 31. DeleteItem What color was the node that was removed? Red? 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 paths from the node to descendant leaves contain the same number of black nodes OK! OK! OK! OK! Does not create two red nodes in a row Not OK! Could change the black heights of some nodes 26 17 41 30 47 38 50 30 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 32. DeleteItem What color was the node that was removed? Black? 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 paths from the node to descendant leaves contain the same number of black nodes OK! OK! Not OK! Could create two red nodes in a row Not OK! Could change the black heights of some nodes 26 17 41 30 47 38 50 Not OK! If removing the root and the child that replaces it is red 31 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 33. Red-Black Trees - Summary • Operations on red-black trees: – SEARCH O(h) – PREDECESSOR O(h) – SUCCESOR O(h) – MINIMUM O(h) – MAXIMUM O(h) – INSERT O(h) – DELETE O(h) • Red-black trees guarantee that the height of the tree will be O(lgn) CS 477/677 - Lecture 12 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 34. Problems 33 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 35. Problems • What red-black tree property is violated in the tree below? How would you restore the red-black tree property in this case? – Property violated: if a node is red, both its children are black – Fixup: color 7 black, 11 red, then right-rotate around 11 11 2 14 1 15 7 8 5 4 z 34 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 36. Problems • Let a, b, c be arbitrary nodes in subtrees α, β, γ 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 – c: decreases by 1 35 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)
  • 37. Problems • When we insert a node into a red-black tree, we initially set the color of the new node to red. • Why didn’t we choose to set the color to black? • Would inserting a new node to a red-black tree and then immediately deleting it, change the tree? 36 Docsity.com Document shared on www.docsity.com Downloaded by: AayushAdhikari (adhikariaayush37@gmail.com)