SlideShare a Scribd company logo
Red-Black Trees
• Name : TCHAYE-KONDI JUDE
Simulator Link : https://www.cs.usfca.edu/~galles/visualization/RedBlack.html
RESUME
I. INTRODUCTION
1. TREE
2. BINARY TREE
3. BINARY SEARCH TREE
II. RED BLACK TREE
III. OPERATIONS
INTRODUCTION
Tree
• Tree is a non-linear data structure which organizes data in hierarchical
structure and this is a recursive definition.
• Root • Edge
• Leaf
• Internal Nodes
• Level
• Height
• Path
BINARY TREE
• A tree in which every node can have a maximum of two children is called as Binary
Tree.
• A binary tree has the following time complexities :
• Search Operation - O(n)
• Insertion Operation - O(n)
• Deletion Operation - O(n)
BINARY SEARCH TREE
• Binary Search Tree is a binary tree in
which every node contains only smaller
values in its left subtree and only larger
values in its right subtree.
BINARY SEARCH TREE
• left subtree of every
node contains nodes
with smaller values
and right subtree of
every node contains
larger values.
Algorithm Average Worst case
Search O(log n) O(n)
Insert O(log n) O(n)
Delete O(log n) O(n)
Time complexity
Balanced Binary Search Trees
• A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N)
• A balanced tree supports efficient operations,
• There are many implementations of balanced BSTs, including AVL trees,
RedBlack trees and AA trees.
RED BLACK TREE
• 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.
• By constraining the node colors on any simple path from the root to a leaf, red-
black trees ensure that no such path is more than twice as long as any other.
• 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.
PROPERTIES
• A red-black tree is a binary 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.
BLACK-HEIGHT
• the number of black nodes on any simple path from, but not including, a
node x down to a leaf the black-height of the node, denoted bh(x) or B
• We define the black-height of a red-black tree to be the black-height of its
root.
OPERATIONS
&
COMPLEXITY
OPERATIONS
1. INSERTION, DELETION, ROTATIONS
2. SEARCH, MINIMUM, MAXIMUM, SUCCESSOR, AND PREDECESSOR…
THE NUMBER OF INTERNAL
NODES OF A NODE X
N(X) ≤ 𝟐 𝒃𝒉(𝒙) − 𝟏
INTERNAL NODES OF X
Let prove by induction that : the subtree rooted at any node x contains at least 𝟐 𝒃𝒉(𝒙)
− 𝟏
internal nodes.
1. If the height of x is 0 then x must be a leaf 𝟐 𝒃𝒉(𝒙) − 𝟏 = 𝟐 𝟎 − 𝟏 =
0
2. For the inductive step, consider a node x that has positive height and is an internal node with
two children :
• Each child has a black-height of either bh(x) or bh(x) - 1, depending on whether its color is
red or black, respectively.
• We conclude that each child has at least : 𝟐 𝒃𝒉(𝒙)−𝟏
− 𝟏 internal nodes (the height of a child
of x is less than the height of x itself)
3. each child has at least 𝟐 𝒃𝒉(𝒙)−𝟏
− 𝟏 internal nodes.
Then the subtree rooted at x contains at least (𝟐 𝒃𝒉(𝒙)−𝟏 − 𝟏)+(𝟐 𝒃𝒉(𝒙)−𝟏 − 𝟏) +1 = 𝟐 𝒃𝒉(𝒙) − 𝟏
Conclusion
• THE NUMBER OF INTERNAL NODES OF A NODE X ( N(X) )
• N(X) ≤ 𝟐 𝒃𝒉(𝒙)
− 𝟏
HEIGHT OF A RB
TREE
Knowing the number of internal nodes (n)
HEIGHT OF A RB TREE(H)
• A red-black tree with n internal nodes has height at most H ≤ 2lg(n+ 1).
According to property 4(If a node is red, then both its children are black.), at least
half the nodes on any simple path from the root to a leaf, not including the
root, must be black. Consequently, the black-height of the root must be at
least h=2; thus,
As an immediate consequence of this lemma, we can implement the dynamic-set
operations SEARCH, MINIMUM, MAXIMUM, SUCCESSOR, and
PREDECESSOR in Olg(n) time on red-black trees,
OPERATIONS
They do not directly support the dynamic-set operations INSERT and DELETE,
since they do not guarantee that the modified binary search tree will be a red-black
tree.
ROTATIONS
LEFT ROTATION :
RIGHT ROTATION
Rotation is followed by Recolor operation
INSERTION
INSERTION
• We can insert a node into an n-node red-black tree in Olg(n) time.
INSERTION
Big-O Complexity
Chart
Red black trees

More Related Content

What's hot

Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
ghhgj jhgh
 

What's hot (20)

358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10
 
Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
 
Red black 1
Red black 1Red black 1
Red black 1
 
Data structure tree- advance
Data structure tree- advanceData structure tree- advance
Data structure tree- advance
 
Red black tree
Red black treeRed black tree
Red black tree
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 
Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++
 
Red black trees
Red black treesRed black trees
Red black trees
 
introduction to_trees
introduction to_treesintroduction to_trees
introduction to_trees
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Chapter 8 ds
Chapter 8 dsChapter 8 ds
Chapter 8 ds
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREES
 
Red black tree
Red black treeRed black tree
Red black tree
 
10 Red-Black Trees
10 Red-Black Trees10 Red-Black Trees
10 Red-Black Trees
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Tree
TreeTree
Tree
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
 
Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
 

Similar to Red black trees

Advanced data structures and implementation
Advanced data structures and implementationAdvanced data structures and implementation
Advanced data structures and implementation
Umang Gupta
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
MouDhara1
 

Similar to Red black trees (20)

Red-black trees
Red-black treesRed-black trees
Red-black trees
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
Advanced data structures and implementation
Advanced data structures and implementationAdvanced data structures and implementation
Advanced data structures and implementation
 
Unit 3 trees
Unit 3   treesUnit 3   trees
Unit 3 trees
 
DSA-Unit-2.pptx
DSA-Unit-2.pptxDSA-Unit-2.pptx
DSA-Unit-2.pptx
 
Balance tree. Short overview
Balance tree. Short overviewBalance tree. Short overview
Balance tree. Short overview
 
Data structure(Part 2)
Data structure(Part 2)Data structure(Part 2)
Data structure(Part 2)
 
Introduction to tree ds
Introduction to tree dsIntroduction to tree ds
Introduction to tree ds
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Tree 11.ppt
Tree 11.pptTree 11.ppt
Tree 11.ppt
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
 
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
 
Data Structures 4
Data Structures 4Data Structures 4
Data Structures 4
 
Tree
TreeTree
Tree
 
Binary trees
Binary treesBinary trees
Binary trees
 
part4-trees.ppt
part4-trees.pptpart4-trees.ppt
part4-trees.ppt
 
Data structures 3
Data structures 3Data structures 3
Data structures 3
 
Unit III.ppt
Unit III.pptUnit III.ppt
Unit III.ppt
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
 

Recently uploaded

Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 

Recently uploaded (20)

The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 

Red black trees

  • 1. Red-Black Trees • Name : TCHAYE-KONDI JUDE Simulator Link : https://www.cs.usfca.edu/~galles/visualization/RedBlack.html
  • 2. RESUME I. INTRODUCTION 1. TREE 2. BINARY TREE 3. BINARY SEARCH TREE II. RED BLACK TREE III. OPERATIONS
  • 4. Tree • Tree is a non-linear data structure which organizes data in hierarchical structure and this is a recursive definition.
  • 11. BINARY TREE • A tree in which every node can have a maximum of two children is called as Binary Tree. • A binary tree has the following time complexities : • Search Operation - O(n) • Insertion Operation - O(n) • Deletion Operation - O(n)
  • 12. BINARY SEARCH TREE • Binary Search Tree is a binary tree in which every node contains only smaller values in its left subtree and only larger values in its right subtree.
  • 13. BINARY SEARCH TREE • left subtree of every node contains nodes with smaller values and right subtree of every node contains larger values. Algorithm Average Worst case Search O(log n) O(n) Insert O(log n) O(n) Delete O(log n) O(n) Time complexity
  • 14. Balanced Binary Search Trees • A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) • A balanced tree supports efficient operations, • There are many implementations of balanced BSTs, including AVL trees, RedBlack trees and AA trees.
  • 15. RED BLACK TREE • 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. • By constraining the node colors on any simple path from the root to a leaf, red- black trees ensure that no such path is more than twice as long as any other. • 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.
  • 16.
  • 17. PROPERTIES • A red-black tree is a binary 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.
  • 18. BLACK-HEIGHT • the number of black nodes on any simple path from, but not including, a node x down to a leaf the black-height of the node, denoted bh(x) or B • We define the black-height of a red-black tree to be the black-height of its root.
  • 20. OPERATIONS 1. INSERTION, DELETION, ROTATIONS 2. SEARCH, MINIMUM, MAXIMUM, SUCCESSOR, AND PREDECESSOR…
  • 21. THE NUMBER OF INTERNAL NODES OF A NODE X N(X) ≤ 𝟐 𝒃𝒉(𝒙) − 𝟏
  • 22.
  • 23. INTERNAL NODES OF X Let prove by induction that : the subtree rooted at any node x contains at least 𝟐 𝒃𝒉(𝒙) − 𝟏 internal nodes. 1. If the height of x is 0 then x must be a leaf 𝟐 𝒃𝒉(𝒙) − 𝟏 = 𝟐 𝟎 − 𝟏 = 0 2. For the inductive step, consider a node x that has positive height and is an internal node with two children : • Each child has a black-height of either bh(x) or bh(x) - 1, depending on whether its color is red or black, respectively. • We conclude that each child has at least : 𝟐 𝒃𝒉(𝒙)−𝟏 − 𝟏 internal nodes (the height of a child of x is less than the height of x itself) 3. each child has at least 𝟐 𝒃𝒉(𝒙)−𝟏 − 𝟏 internal nodes. Then the subtree rooted at x contains at least (𝟐 𝒃𝒉(𝒙)−𝟏 − 𝟏)+(𝟐 𝒃𝒉(𝒙)−𝟏 − 𝟏) +1 = 𝟐 𝒃𝒉(𝒙) − 𝟏
  • 24. Conclusion • THE NUMBER OF INTERNAL NODES OF A NODE X ( N(X) ) • N(X) ≤ 𝟐 𝒃𝒉(𝒙) − 𝟏
  • 25. HEIGHT OF A RB TREE Knowing the number of internal nodes (n)
  • 26. HEIGHT OF A RB TREE(H) • A red-black tree with n internal nodes has height at most H ≤ 2lg(n+ 1). According to property 4(If a node is red, then both its children are black.), at least half the nodes on any simple path from the root to a leaf, not including the root, must be black. Consequently, the black-height of the root must be at least h=2; thus, As an immediate consequence of this lemma, we can implement the dynamic-set operations SEARCH, MINIMUM, MAXIMUM, SUCCESSOR, and PREDECESSOR in Olg(n) time on red-black trees,
  • 27. OPERATIONS They do not directly support the dynamic-set operations INSERT and DELETE, since they do not guarantee that the modified binary search tree will be a red-black tree.
  • 28.
  • 30.
  • 31.
  • 32.
  • 35. Rotation is followed by Recolor operation
  • 36.
  • 38. INSERTION • We can insert a node into an n-node red-black tree in Olg(n) time.

Editor's Notes

  1. In a tree data structure, the first node is called as Root Node. In any tree, there must be only one root node. We never have multiple root nodes in a tree.
  2. Red-black trees are one of many search-tree schemes that are “balanced” in order to guarantee that basic dynamic-set operations take O.lg n/ time in the worst case.
  3. The time complexity is O(1) . We simply change the pointer of some nodes.