SlideShare a Scribd company logo
Red-Black Trees 
 What are they? 
 Their relation to 2-4 trees 
 Deletion in red-black trees
Red-Black Trees 
 A red black tree is a binary search tree in which 
each node is colored red or black. 
 The root is colored black. 
 A red node can have only black children. 
 If a node of the BST does not have a left and/or 
right child then we add an external node. 
 External nodes are not colored. 
 The black depth of an external node is defined 
as the number of black ancestors it has. 
 In a red-black tree every external node has the 
same black depth.
Examples of red-black trees 
Black height of 
tree is 2 
Black height of 
tree is 2
Trees which are not red-black 
Double red Black height not uniform
Height of a red-black tree 
 Let h be the black height of a red-black 
tree on n nodes. 
 n is smallest when all nodes are black. In 
this case tree is a complete binary tree of 
height h and n=2h -1. 
 n is largest when alternate levels of tree 
are red. Then height of tree is 2h and 
n=22h-1. 
Hence, log4 n < h < 1+log2 n
Red-black trees to 2-4 trees 
 Any red-black tree can be converted into a 2-4 
tree. 
 Take a black node and its red children (at most 2) 
and combine them into one node of a 2-4 tree. 
 Each node so formed has at least 1 and at most 3 
keys. 
 Since black height of all external nodes is same, 
in the resulting 2-4 tree all leaves are at same 
level.
Example 
1 
19 
17 
13 
11 
9 
4 
7 
3 5 
2 
4 9 13 
1 2 3 5 7 11 17 19
2-4 trees to red-black trees 
 Any 2-4 tree can be converted into a red-black 
tree. 
We replace a node of the 2-4 tree with one 
black node and 0/1/2 red nodes which are 
children of the black node. 
 The height of the 2-4 tree is the black 
height of the red-black tree created. 
 Every red node has a black child.
Example 
13 
3 18 
1 4 9 11 12 
14 20 
8 10 
2 5 6 15 
2 
20 
18 
15 
13 
8 
10 
5 9 
3 
12 14 Black height is same as 
1 4 6 11 
height of 2-4 tree
Deletion: preliminaries 
 To delete a node we proceed as in a BST. 
 Thus the node which is deleted is the 
parent of an external node. 
 Hence it is either a leaf or the parent of a 
leaf. 
1 11 
19 
17 
17
Deletion: preliminaries(2) 
 Hence we can assume that the node deleted is a 
black leaf. 
 Removing this reduces black depth of an 
external node by 1. 
 Hence, in a general step, we consider how to 
reorganize the tree when the black height of 
some subtree goes down from h to h-1.
Deletion: the cases 
a is red a a is black 
b is red b is black 
a 
b 
a 
b 
c c 
a 
b 
c 
d d 
a 
b 
c c 
a 
b 
c 
Both c 
are black 
a 
b 
Some c is 
red 
a 
b 
c 
d 
Both d 
are black 
a 
b 
c 
Some d is red 
a 
b 
c 
a 
b 
Some c is 
red 
Both c 
are 
black
Deletion: case1.1 
 If parent is a red node (a). 
 Then it has a child (b) which must be black 
 If b has a red child (c) 
b 
a 
c 
b 
c a 
h-1 h-1 
b 
a 
c 
h to h-1 
h-1 
h-1 
h-1 h-1 
h to h-1 
h-1 h-1 h-1 h-1 
a 
b c
Deletion: case 1.2 
 If parent is a red node (a). 
 Then it has a child (b) which must be black 
 If b has no red child 
b 
a 
b 
a 
h to h-1 
h-1 h-1 
h-1 
h-1 h-1 
a 
b b a
Deletion: case 2.1.1 
 If parent is a black node (a). 
 If a has a red child (b) 
 Consider right child of b (c) which must be black. 
 If (c) has a red child (d). 
b 
a 
c 
d 
c 
b a 
d 
h to h-1 
h h 
h-1 
h-1 h-1 
h-1 
h-1 h-1 
h-1 
d 
b a 
b c 
c d a
Deletion: case 2.1.2 
 If parent is a black node (a). 
 If a has a red child (b) 
 Consider right child of b (c) which must be black. 
 If (c) has no red child. 
b 
a 
c 
a 
b 
h to h-1 c 
h-1 h-1 
h 
h 
h-1 h-1 
h-1 
c 
b a 
a 
b 
c
Deletion: case 2.2.1 
 If parent is a black node (a). 
 If the child of node a (c) is black. 
 If (c) has a red child (d) 
a 
c 
d 
c 
d 
h to h-1 a 
h-1 
h-1 
h-1 
h-1 h-1 h-1 h-1 
c 
a 
d 
d c a
Deletion: case 2.2.2 
 If parent is a black node (a). 
 If the child of node a (c) is black. 
 If (c) has no red child. 
a 
c 
a 
h to h-1 c 
h-1 h-1 
h-1 
h-1 h-1 
c 
a 
c a
Deletion: Summary 
 In all cases, except 2.2.2, deletion can be 
completed by a simple rotation/recoloring 
 In case 2.2.2, the height of the subtree 
reduces and so we need to proceed up 
the tree. 
 But in case 2.2.2 we only recolor nodes. 
 Thus, if we proceed up the tree then we 
only need to recolor. Eventually we would 
do a rotation.

More Related Content

What's hot

Red black trees1109
Red black trees1109Red black trees1109
Red black trees1109
Pravin Dsilva
 
Red black tree
Red black treeRed black tree
Red black tree
qamar mustafa
 
10 Red-Black Trees
10 Red-Black Trees10 Red-Black Trees
10 Red-Black Trees
Andres Mendez-Vazquez
 
Red black tree
Red black treeRed black tree
Red black tree
Dr Sandeep Kumar Poonia
 
lecture 14
lecture 14lecture 14
lecture 14sajinsc
 
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)
United International University
 
Advanced data structures and implementation
Advanced data structures and implementationAdvanced data structures and implementation
Advanced data structures and implementationUmang Gupta
 
Binary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red BlackBinary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red Black
Amrinder Arora
 
Chap 7 binary threaded tree
Chap 7 binary threaded treeChap 7 binary threaded tree
Chap 7 binary threaded tree
Raj Sarode
 
Balance tree. Short overview
Balance tree. Short overviewBalance tree. Short overview
Balance tree. Short overview
ElifTech
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
Om Prakash
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREES
TABISH HAMID
 
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
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
Daa unit 2
Daa unit 2Daa unit 2
Daa unit 2
Abhimanyu Mishra
 
Data structure tree- advance
Data structure tree- advanceData structure tree- advance
Data structure tree- advance
MD. MARUFUZZAMAN .
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
Dwight Sabio
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
Adam Mukharil Bachtiar
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
ManishPrajapati78
 

What's hot (20)

Red black trees1109
Red black trees1109Red black trees1109
Red black trees1109
 
Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
 
Red black 1
Red black 1Red black 1
Red black 1
 
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
 
Red black tree
Red black treeRed black tree
Red black tree
 
lecture 14
lecture 14lecture 14
lecture 14
 
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)
 
Advanced data structures and implementation
Advanced data structures and implementationAdvanced data structures and implementation
Advanced data structures and implementation
 
Binary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red BlackBinary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red Black
 
Chap 7 binary threaded tree
Chap 7 binary threaded treeChap 7 binary threaded tree
Chap 7 binary threaded tree
 
Balance tree. Short overview
Balance tree. Short overviewBalance tree. Short overview
Balance tree. Short overview
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
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
 
Daa unit 2
Daa unit 2Daa unit 2
Daa unit 2
 
Data structure tree- advance
Data structure tree- advanceData structure tree- advance
Data structure tree- advance
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
 

Viewers also liked

Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
Shoaib Khan
 
Red black trees
Red black treesRed black trees
Red black trees
mumairsadiq
 
Red black tree
Red black treeRed black tree
Red black tree
Rajendran
 

Viewers also liked (6)

Red black-trees-4
Red black-trees-4Red black-trees-4
Red black-trees-4
 
Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
 
Red black trees
Red black treesRed black trees
Red black trees
 
Red black tree
Red black treeRed black tree
Red black tree
 
Lec9
Lec9Lec9
Lec9
 
Lec8
Lec8Lec8
Lec8
 

Similar to Lec14

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
AayushAdhikari27
 
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
Sheba41
 
Data structure
Data structureData structure
Data structure
janani thirupathi
 
Red-black trees
Red-black treesRed-black trees
Red-black trees
sathish sak
 
Cse 225 rbt_red_black_tree
Cse 225 rbt_red_black_treeCse 225 rbt_red_black_tree
Cse 225 rbt_red_black_tree
Sazidur Rahman Sadi
 
redblacktreeindatastructure-200409083949.pptx
redblacktreeindatastructure-200409083949.pptxredblacktreeindatastructure-200409083949.pptx
redblacktreeindatastructure-200409083949.pptx
RAtna29
 
Red black tree in data structure
Red black tree in data structureRed black tree in data structure
Red black tree in data structure
Vrushali Dhanokar
 
rbtrees.ppt
rbtrees.pptrbtrees.ppt
rbtrees.ppt
Umesh Hengaju
 
Red black trees and their properties
Red black trees and their propertiesRed black trees and their properties
Red black trees and their properties
Kousalya M
 
binary tree.pptx
binary tree.pptxbinary tree.pptx
binary tree.pptx
DhanushSrinivasulu
 
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptxLecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
HamzaUsman48
 
RED BLACK TREE ADSA UNIT 1 ROTAITON RED B
RED BLACK TREE ADSA UNIT 1 ROTAITON RED BRED BLACK TREE ADSA UNIT 1 ROTAITON RED B
RED BLACK TREE ADSA UNIT 1 ROTAITON RED B
Salini P
 

Similar to Lec14 (15)

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
 
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
 
Data structure
Data structureData structure
Data structure
 
Lec15
Lec15Lec15
Lec15
 
Red blacktrees
Red blacktreesRed blacktrees
Red blacktrees
 
Red-black trees
Red-black treesRed-black trees
Red-black trees
 
Cse 225 rbt_red_black_tree
Cse 225 rbt_red_black_treeCse 225 rbt_red_black_tree
Cse 225 rbt_red_black_tree
 
redblacktreeindatastructure-200409083949.pptx
redblacktreeindatastructure-200409083949.pptxredblacktreeindatastructure-200409083949.pptx
redblacktreeindatastructure-200409083949.pptx
 
Red black tree in data structure
Red black tree in data structureRed black tree in data structure
Red black tree in data structure
 
rbtrees.ppt
rbtrees.pptrbtrees.ppt
rbtrees.ppt
 
Red black trees and their properties
Red black trees and their propertiesRed black trees and their properties
Red black trees and their properties
 
16 rbtrees
16 rbtrees16 rbtrees
16 rbtrees
 
binary tree.pptx
binary tree.pptxbinary tree.pptx
binary tree.pptx
 
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptxLecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
 
RED BLACK TREE ADSA UNIT 1 ROTAITON RED B
RED BLACK TREE ADSA UNIT 1 ROTAITON RED BRED BLACK TREE ADSA UNIT 1 ROTAITON RED B
RED BLACK TREE ADSA UNIT 1 ROTAITON RED B
 

More from Nikhil Chilwant

The joyless economy
The joyless economyThe joyless economy
The joyless economy
Nikhil Chilwant
 
I 7
I 7I 7
IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )
Nikhil Chilwant
 

More from Nikhil Chilwant (20)

The joyless economy
The joyless economyThe joyless economy
The joyless economy
 
I 7
I 7I 7
I 7
 
IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )
 
Lec39
Lec39Lec39
Lec39
 
Lec38
Lec38Lec38
Lec38
 
Lec37
Lec37Lec37
Lec37
 
Lec36
Lec36Lec36
Lec36
 
Lec35
Lec35Lec35
Lec35
 
Lec34
Lec34Lec34
Lec34
 
Lec33
Lec33Lec33
Lec33
 
Lec32
Lec32Lec32
Lec32
 
Lec30
Lec30Lec30
Lec30
 
Lec29
Lec29Lec29
Lec29
 
Lec28
Lec28Lec28
Lec28
 
Lec27
Lec27Lec27
Lec27
 
Lec26
Lec26Lec26
Lec26
 
Lec25
Lec25Lec25
Lec25
 
Lec24
Lec24Lec24
Lec24
 
Lec23
Lec23Lec23
Lec23
 
Lec21
Lec21Lec21
Lec21
 

Lec14

  • 1. Red-Black Trees  What are they?  Their relation to 2-4 trees  Deletion in red-black trees
  • 2. Red-Black Trees  A red black tree is a binary search tree in which each node is colored red or black.  The root is colored black.  A red node can have only black children.  If a node of the BST does not have a left and/or right child then we add an external node.  External nodes are not colored.  The black depth of an external node is defined as the number of black ancestors it has.  In a red-black tree every external node has the same black depth.
  • 3. Examples of red-black trees Black height of tree is 2 Black height of tree is 2
  • 4. Trees which are not red-black Double red Black height not uniform
  • 5. Height of a red-black tree  Let h be the black height of a red-black tree on n nodes.  n is smallest when all nodes are black. In this case tree is a complete binary tree of height h and n=2h -1.  n is largest when alternate levels of tree are red. Then height of tree is 2h and n=22h-1. Hence, log4 n < h < 1+log2 n
  • 6. Red-black trees to 2-4 trees  Any red-black tree can be converted into a 2-4 tree.  Take a black node and its red children (at most 2) and combine them into one node of a 2-4 tree.  Each node so formed has at least 1 and at most 3 keys.  Since black height of all external nodes is same, in the resulting 2-4 tree all leaves are at same level.
  • 7. Example 1 19 17 13 11 9 4 7 3 5 2 4 9 13 1 2 3 5 7 11 17 19
  • 8. 2-4 trees to red-black trees  Any 2-4 tree can be converted into a red-black tree. We replace a node of the 2-4 tree with one black node and 0/1/2 red nodes which are children of the black node.  The height of the 2-4 tree is the black height of the red-black tree created.  Every red node has a black child.
  • 9. Example 13 3 18 1 4 9 11 12 14 20 8 10 2 5 6 15 2 20 18 15 13 8 10 5 9 3 12 14 Black height is same as 1 4 6 11 height of 2-4 tree
  • 10.
  • 11.
  • 12. Deletion: preliminaries  To delete a node we proceed as in a BST.  Thus the node which is deleted is the parent of an external node.  Hence it is either a leaf or the parent of a leaf. 1 11 19 17 17
  • 13. Deletion: preliminaries(2)  Hence we can assume that the node deleted is a black leaf.  Removing this reduces black depth of an external node by 1.  Hence, in a general step, we consider how to reorganize the tree when the black height of some subtree goes down from h to h-1.
  • 14. Deletion: the cases a is red a a is black b is red b is black a b a b c c a b c d d a b c c a b c Both c are black a b Some c is red a b c d Both d are black a b c Some d is red a b c a b Some c is red Both c are black
  • 15. Deletion: case1.1  If parent is a red node (a).  Then it has a child (b) which must be black  If b has a red child (c) b a c b c a h-1 h-1 b a c h to h-1 h-1 h-1 h-1 h-1 h to h-1 h-1 h-1 h-1 h-1 a b c
  • 16. Deletion: case 1.2  If parent is a red node (a).  Then it has a child (b) which must be black  If b has no red child b a b a h to h-1 h-1 h-1 h-1 h-1 h-1 a b b a
  • 17. Deletion: case 2.1.1  If parent is a black node (a).  If a has a red child (b)  Consider right child of b (c) which must be black.  If (c) has a red child (d). b a c d c b a d h to h-1 h h h-1 h-1 h-1 h-1 h-1 h-1 h-1 d b a b c c d a
  • 18. Deletion: case 2.1.2  If parent is a black node (a).  If a has a red child (b)  Consider right child of b (c) which must be black.  If (c) has no red child. b a c a b h to h-1 c h-1 h-1 h h h-1 h-1 h-1 c b a a b c
  • 19. Deletion: case 2.2.1  If parent is a black node (a).  If the child of node a (c) is black.  If (c) has a red child (d) a c d c d h to h-1 a h-1 h-1 h-1 h-1 h-1 h-1 h-1 c a d d c a
  • 20. Deletion: case 2.2.2  If parent is a black node (a).  If the child of node a (c) is black.  If (c) has no red child. a c a h to h-1 c h-1 h-1 h-1 h-1 h-1 c a c a
  • 21. Deletion: Summary  In all cases, except 2.2.2, deletion can be completed by a simple rotation/recoloring  In case 2.2.2, the height of the subtree reduces and so we need to proceed up the tree.  But in case 2.2.2 we only recolor nodes.  Thus, if we proceed up the tree then we only need to recolor. Eventually we would do a rotation.