SlideShare a Scribd company logo
1 of 23
PRAVIN D’SILVA
1109
MCA , GOA UNIVERSITY
Acknowledgement
I would like express my sincere gratitude to Dr.
Jyoti Pawar, for the support and guidance as
well as the feedback she has provided
throughout this study.
Definition
• A red-black tree is a type of self balancing
binary search tree, with one extra attribute for
each node: the colour, which is either red or
black.
• A red–black tree is a binary search tree that
inserts and deletes in such a way that the tree is
always reasonably balanced.
Balanced binary tree
• A non-empty binary tree T is
balanced if:
1) Left subtree of T is balanced
2) Right subtree of T is balanced
3) The difference between heights of
left subtree and right subtree is not
more than 1.
Red-Black tree
• Recall binary search tree
▫ Key values in the left subtree <= the node value
▫ Key values in the right subtree >= the node value

• Operations:
▫ insertion, deletion
▫ Search, maximum, minimum, successor, predecessor.
▫ O(h), h is the height of the tree.
Red-black trees
•

Definition: a binary tree, satisfying:
1.
2.
3.
4.
5.

•
•

Every node is red or black
The root is black
Every leaf is NIL and is black
If a node is red, then both its children are black
For each node, all paths from the node to
descendant leaves contain the same number of
black nodes.

Purpose: keep the tree balanced.
Other balanced search tree:
▫

AVL tree, 2-3-4 tree, Splay tree, Treap
Example of red black trees

Each Red Node can have only Black children
Not a red black tree
Black height of a red black tree
• Black height does not count the root itself.
• we use "nil leaves" or "null leaves", which contain no data and
merely serve to indicate where the tree ends
7

BH=2
BH=2

3

BH=1

BH=0
22
BH=1
8

10

BH=1,
ignore red
nodes!!

NIL
pointer
Complexity
• The persistent version of red-black trees
requires O(log n) space for each insertion or
deletion.
Some operations in log(n)
• Search, minimum, maximum,
successor, predecessor.
• Let us discuss insert and delete.
Inserting in a red black tree
• Let k be the key being inserted.
• As in case of a BST, we first search for k; this
gives us the place where we have to insert k.
• We create a new node with key k and insert it at
this place
• The new node is colored red.
• Since inserted node is Red , the black height of
the tree remains unchanged.
• However if the parent node is also red, then we
have a double red problem

k
k
NO PROBLEM

DOUBLE RED PROBLEM
INSERTION : CASE 1
• Parent of node (a) must be black (b).
• The other child of (b) is black (c) .
• Rotation corrects the defect.
b
c

a
b

a
k

c

k
Insertion case 2
• Parent of node (a) is red
• Parent of (a) must be black (b)
• The other child of (b) is also red (c)

c|b|a

b

b

h+1

c

h

c

a
k
h

k

h

h
h

h+1

a

h

h

h
h

h
Deletion
• To delete a node we proceed as in a BST
• Hence the node which is deleted is the parent of
an external node
• Hence it is either a leaf or parent of a leaf
• Steps:
▫
▫
▫
▫

Search
Identify
Leaf, then delete
If internal node, find successor or predecessor,
swap, then delete the successor or predecessor
Consider a red black
a

a

b

c

b

c
Case 1

a

c

b
a

h-1

h-1

h-1
h-1

h-1

h-1

h-1

h-1

b

a

a
b
h-1

h-1 h-1

h-1

c

|

h-1

h-1
h-1

h-1

| b| c

| a
Case 2
•
•
•
•

If parent is a red node (a)
Then it has a child (b) which must be black
If (b) has no red child
Recoloring solves the problem
a

a

b

b

h-1

h-1

h-1

h-1

h-1

h-1
Case 3
a

a

b

b

h-1

h-1

h-1

h-1

h-1

h-1
Deletion Summary
• In most cases, deletion can be completed by
simple rotation/ coloring.
• In case 3, the height of the subtree reduces and
so we need to proceed up the tree
• But in case 3, we only recolor the nodes
• Thus, if we proceed up the tree, then we only
need to recolor. Eventually we would do a
rotation.
References
• "Introduction to Algorithms, Third Edition," by Thomas H.
Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein.
• Lecture Series on Data Structures and Algorithms by Dr. Naveen
Garg, Department of Computer Science and Engineering, IIT Delhi.
http://nptel.iitm.ac.in
• Lecture 10: Red-black Trees, Rotations, Insertions, Deletions:
http://videolectures.net/mit6046jf05_demaine_lec10/
• http://mitpress.mit.edu/algorithms/solutions/chap13-solutions.pdf
• http://www.cs.purdue.edu/homes/ayg/CS251/slides/chap13c.pdf
• Left leaning Red black trees
• http://www.cs.princeton.edu/~rs/talks/LLRB/RedBlack.pdf
• http://www.cs.princeton.edu/~rs/talks/LLRB/LLRB.pdf
Thank
You

More Related Content

What's hot (20)

Balanced Tree(AVL Tree,Red Black Tree)
Balanced Tree(AVL Tree,Red Black Tree)Balanced Tree(AVL Tree,Red Black Tree)
Balanced Tree(AVL Tree,Red Black Tree)
 
Red black tree
Red black treeRed black tree
Red black tree
 
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 presentation
Red black trees presentationRed black trees presentation
Red black trees presentation
 
10 Red-Black Trees
10 Red-Black Trees10 Red-Black Trees
10 Red-Black Trees
 
Advanced data structures and implementation
Advanced data structures and implementationAdvanced data structures and implementation
Advanced data structures and implementation
 
Red-black trees
Red-black treesRed-black trees
Red-black trees
 
Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
 
Lec14
Lec14Lec14
Lec14
 
Balance tree. Short overview
Balance tree. Short overviewBalance tree. Short overview
Balance tree. Short overview
 
lecture 14
lecture 14lecture 14
lecture 14
 
16 rbtrees
16 rbtrees16 rbtrees
16 rbtrees
 
Chap 7 binary threaded tree
Chap 7 binary threaded treeChap 7 binary threaded tree
Chap 7 binary threaded tree
 
Red black trees
Red black treesRed black trees
Red black trees
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREES
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
Avl trees
Avl treesAvl trees
Avl trees
 

Viewers also liked

Red black tree
Red black treeRed black tree
Red black treeRajendran
 
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
 
Insertion in RED BLACK TREE
Insertion in RED BLACK TREEInsertion in RED BLACK TREE
Insertion in RED BLACK TREEnikhilarora2211
 
Algebra 1 2.8 Algebraic Proof
Algebra 1 2.8 Algebraic ProofAlgebra 1 2.8 Algebraic Proof
Algebra 1 2.8 Algebraic ProofJaqueline Vallejo
 
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 sahniHitesh Wagle
 
Intro to Graphing Data Powerpoint-7th and 8th Grade
Intro to Graphing Data Powerpoint-7th and 8th GradeIntro to Graphing Data Powerpoint-7th and 8th Grade
Intro to Graphing Data Powerpoint-7th and 8th GradeBaily OrBust
 
Indexing and-hashing
Indexing and-hashingIndexing and-hashing
Indexing and-hashingAmi Ranjit
 
Best for b trees
Best for b treesBest for b trees
Best for b treesDineshRaaja
 
Indexing and hashing
Indexing and hashingIndexing and hashing
Indexing and hashingJeet Poria
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data StructureAnuj Modi
 
Introduction to graph class 8
Introduction to graph class 8Introduction to graph class 8
Introduction to graph class 8Monika Rana
 

Viewers also liked (20)

Red black tree
Red black treeRed black tree
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)
 
Insertion in RED BLACK TREE
Insertion in RED BLACK TREEInsertion in RED BLACK TREE
Insertion in RED BLACK TREE
 
Algebra 1 2.8 Algebraic Proof
Algebra 1 2.8 Algebraic ProofAlgebra 1 2.8 Algebraic Proof
Algebra 1 2.8 Algebraic Proof
 
Logic gates with laws
Logic gates with lawsLogic gates with laws
Logic gates with laws
 
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
 
Intro to Graphing Data Powerpoint-7th and 8th Grade
Intro to Graphing Data Powerpoint-7th and 8th GradeIntro to Graphing Data Powerpoint-7th and 8th Grade
Intro to Graphing Data Powerpoint-7th and 8th Grade
 
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
 
B tree
B treeB tree
B tree
 
b+ tree
b+ treeb+ tree
b+ tree
 
B trees dbms
B trees dbmsB trees dbms
B trees dbms
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data Structure
 
Introduction to graph class 8
Introduction to graph class 8Introduction to graph class 8
Introduction to graph class 8
 

Similar to Red black trees1109

anastasio-red-black-trees-1-1-091222204455-phpapp02.pdf
anastasio-red-black-trees-1-1-091222204455-phpapp02.pdfanastasio-red-black-trees-1-1-091222204455-phpapp02.pdf
anastasio-red-black-trees-1-1-091222204455-phpapp02.pdfAayushAdhikari27
 
Search tree,Tree and binary tree and heap tree
Search tree,Tree  and binary tree and heap treeSearch tree,Tree  and binary tree and heap tree
Search tree,Tree and binary tree and heap treezia eagle
 
Binary search tree
Binary search treeBinary search tree
Binary search treeSana Yameen
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptxMouDhara1
 
redblacktreeindatastructure-200409083949.pptx
redblacktreeindatastructure-200409083949.pptxredblacktreeindatastructure-200409083949.pptx
redblacktreeindatastructure-200409083949.pptxRAtna29
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structureAnusruti Mitra
 
Red black tree in data structure
Red black tree in data structureRed black tree in data structure
Red black tree in data structureVrushali Dhanokar
 
Binary Search Tree.pptx
Binary Search Tree.pptxBinary Search Tree.pptx
Binary Search Tree.pptxRaaviKapoor
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsAakash deep Singhal
 

Similar to Red black trees1109 (20)

Binary tree
Binary treeBinary tree
Binary tree
 
Red blacktrees
Red blacktreesRed blacktrees
Red blacktrees
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Tree
TreeTree
Tree
 
anastasio-red-black-trees-1-1-091222204455-phpapp02.pdf
anastasio-red-black-trees-1-1-091222204455-phpapp02.pdfanastasio-red-black-trees-1-1-091222204455-phpapp02.pdf
anastasio-red-black-trees-1-1-091222204455-phpapp02.pdf
 
Search tree,Tree and binary tree and heap tree
Search tree,Tree  and binary tree and heap treeSearch tree,Tree  and binary tree and heap tree
Search tree,Tree and binary tree and heap tree
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
 
6_1 (1).ppt
6_1 (1).ppt6_1 (1).ppt
6_1 (1).ppt
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
 
Binary trees
Binary treesBinary trees
Binary trees
 
redblacktreeindatastructure-200409083949.pptx
redblacktreeindatastructure-200409083949.pptxredblacktreeindatastructure-200409083949.pptx
redblacktreeindatastructure-200409083949.pptx
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Red black tree in data structure
Red black tree in data structureRed black tree in data structure
Red black tree in data structure
 
Binary tree
Binary treeBinary tree
Binary tree
 
Binary Search Tree.pptx
Binary Search Tree.pptxBinary Search Tree.pptx
Binary Search Tree.pptx
 
Unit III.ppt
Unit III.pptUnit III.ppt
Unit III.ppt
 
DSA-Unit-2.pptx
DSA-Unit-2.pptxDSA-Unit-2.pptx
DSA-Unit-2.pptx
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 

Recently uploaded

Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 

Recently uploaded (20)

Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 

Red black trees1109

  • 2. Acknowledgement I would like express my sincere gratitude to Dr. Jyoti Pawar, for the support and guidance as well as the feedback she has provided throughout this study.
  • 3. Definition • A red-black tree is a type of self balancing binary search tree, with one extra attribute for each node: the colour, which is either red or black. • A red–black tree is a binary search tree that inserts and deletes in such a way that the tree is always reasonably balanced.
  • 4. Balanced binary tree • A non-empty binary tree T is balanced if: 1) Left subtree of T is balanced 2) Right subtree of T is balanced 3) The difference between heights of left subtree and right subtree is not more than 1.
  • 5. Red-Black tree • Recall binary search tree ▫ Key values in the left subtree <= the node value ▫ Key values in the right subtree >= the node value • Operations: ▫ insertion, deletion ▫ Search, maximum, minimum, successor, predecessor. ▫ O(h), h is the height of the tree.
  • 6. Red-black trees • Definition: a binary tree, satisfying: 1. 2. 3. 4. 5. • • Every node is red or black The root is black Every leaf is NIL and is black If a node is red, then both its children are black For each node, all paths from the node to descendant leaves contain the same number of black nodes. Purpose: keep the tree balanced. Other balanced search tree: ▫ AVL tree, 2-3-4 tree, Splay tree, Treap
  • 7. Example of red black trees Each Red Node can have only Black children
  • 8. Not a red black tree
  • 9. Black height of a red black tree • Black height does not count the root itself. • we use "nil leaves" or "null leaves", which contain no data and merely serve to indicate where the tree ends 7 BH=2 BH=2 3 BH=1 BH=0 22 BH=1 8 10 BH=1, ignore red nodes!! NIL pointer
  • 10. Complexity • The persistent version of red-black trees requires O(log n) space for each insertion or deletion.
  • 11. Some operations in log(n) • Search, minimum, maximum, successor, predecessor. • Let us discuss insert and delete.
  • 12. Inserting in a red black tree • Let k be the key being inserted. • As in case of a BST, we first search for k; this gives us the place where we have to insert k. • We create a new node with key k and insert it at this place • The new node is colored red.
  • 13. • Since inserted node is Red , the black height of the tree remains unchanged. • However if the parent node is also red, then we have a double red problem k k NO PROBLEM DOUBLE RED PROBLEM
  • 14. INSERTION : CASE 1 • Parent of node (a) must be black (b). • The other child of (b) is black (c) . • Rotation corrects the defect. b c a b a k c k
  • 15. Insertion case 2 • Parent of node (a) is red • Parent of (a) must be black (b) • The other child of (b) is also red (c) c|b|a b b h+1 c h c a k h k h h h h+1 a h h h h h
  • 16. Deletion • To delete a node we proceed as in a BST • Hence the node which is deleted is the parent of an external node • Hence it is either a leaf or parent of a leaf • Steps: ▫ ▫ ▫ ▫ Search Identify Leaf, then delete If internal node, find successor or predecessor, swap, then delete the successor or predecessor
  • 17. Consider a red black a a b c b c
  • 19. Case 2 • • • • If parent is a red node (a) Then it has a child (b) which must be black If (b) has no red child Recoloring solves the problem a a b b h-1 h-1 h-1 h-1 h-1 h-1
  • 21. Deletion Summary • In most cases, deletion can be completed by simple rotation/ coloring. • In case 3, the height of the subtree reduces and so we need to proceed up the tree • But in case 3, we only recolor the nodes • Thus, if we proceed up the tree, then we only need to recolor. Eventually we would do a rotation.
  • 22. References • "Introduction to Algorithms, Third Edition," by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein. • Lecture Series on Data Structures and Algorithms by Dr. Naveen Garg, Department of Computer Science and Engineering, IIT Delhi. http://nptel.iitm.ac.in • Lecture 10: Red-black Trees, Rotations, Insertions, Deletions: http://videolectures.net/mit6046jf05_demaine_lec10/ • http://mitpress.mit.edu/algorithms/solutions/chap13-solutions.pdf • http://www.cs.purdue.edu/homes/ayg/CS251/slides/chap13c.pdf • Left leaning Red black trees • http://www.cs.princeton.edu/~rs/talks/LLRB/RedBlack.pdf • http://www.cs.princeton.edu/~rs/talks/LLRB/LLRB.pdf