SlideShare a Scribd company logo
1 of 15
 Insertion in red-black trees 
 a-b trees 
 What are they? 
 Insertion and deletion in a-b trees
Insertion in red-black trees 
 Let k be the key being inserted 
 As in the 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.
Insertion(2) 
 Since inserted node is colored red, the 
black height of the tree remains 
unchanged. 
 However, if the parent of inserted node is 
also red then we have a double red 
problem. 
k k 
No problem Double red problem
Insertion: case 1 
 Parent of inserted node (a) is red. 
 Parent of (a) must be black (b) 
 The other child of (b) is black (c). 
b 
c 
k 
a 
a 
c 
b k 
The 2-4 tree node contains {b,a,k} and is malformed. 
The rotation corrects the defect.
Insertion: Case 2 
 Parent of inserted node (a) is red. 
 Parent of (a) must be black (b) 
 The other child of (b) is also red (c). 
b 
c 
k 
a 
b 
c 
k 
a 
k 
c b a 
a k 
b 
c
Insertion: case 2 (contd) 
 The parent of b could also be red. In that case, 
the double red problem moves up a level. 
 We repeat this process at the next level. 
 Eventually, we might color the root red. 
 In this case we recolor the root black. This 
increases the black depth of every external node 
by 1. 
 In the 2-4 tree this corresponds to splitting the 
root.
Insertion and Deletion: Summary 
 In both insertion and deletion we need to 
make at most one rotation. 
We might have to move up the tree but in 
doing so we only recolor nodes. 
 Time taken is O(log n)
(a,b) Trees 
 A multiway search tree. 
 Each node has at least a 
and at most b children. 
 Root can have less than 
a children but it has at 
least 2 children. 
 All leaf nodes are at the 
same level. 
 Height h of (a,b) tree is 
at least logb n and at 
most loga n. 
12 
5 10 15 
3 4 6 8 11 13 14 17
Insertion 
 No problem if the node 
has empty space 
13 
3 8 
18 25 
29 
1 2 4 5 
9 14 20 24 26 
28 
21 23 7 
22 
15
Insertion(2)  Nodes get split if there is 
insufficient space. 
 The median key is promoted to the 
parent node and inserted there 
25 
24 26 
13 
29 7 
23 
22 
3 8 18 
1 2 4 5 9 14 15 
20 28
Insertion(3) 
 A node is split when it has exactly b keys. 
One of these is promoted to the parent 
and the remaining are split between two 
nodes. 
 Thus one node gets and the other 
keys. 
 This implies that a-1 >=
Deletion 
 If after deleting a key a node becomes empty 
then we borrow a key from its sibling. 
 Delete 20 
1 4 9 11 14 20 
24 
28 
26 
18 
3 
13 
12 
8 10 
6 
5 
22 
15 
2 23 29 30 
7
Deletion(2) 
 If sibling has only one key then we merge with it. 
 The key in the parent node separating these two 
siblings moves down into the merged node. 
 Delete 23 
18 
3 
13 
1 4 9 11 12 
14 20 
8 10 
6 
5 
2 
22 
7 15 
24 
28 
23 26 29 30
Deletion(3) 
 In an (a,b) tree we will merge a node with 
its sibling if the node has a-2 keys and its 
sibling has a-1 keys. 
 Thus the merged node has 2(a-1) keys. 
 This implies that 2(a-1) <= b-1 which is 
equivalent to a-1 <= . 
 Earlier too we argued that a-1 <= 
 This implies b >= 2a-1 
 For a=2, b >= 3
Conclusion 
 The height of a (a,b) tree is O(log n). 
 b >= 2a-1. 
 For insertion and deletion we take time 
proportional to the height.

More Related Content

What's hot

1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search treeKrish_ver2
 
Notes - Polynomial Division
Notes - Polynomial DivisionNotes - Polynomial Division
Notes - Polynomial DivisionLori Rapp
 
Algebra factoring
Algebra factoringAlgebra factoring
Algebra factoringTrabahoLang
 
Alg complex numbers
Alg complex numbersAlg complex numbers
Alg complex numbersTrabahoLang
 
Simplifying algebraic expressions
Simplifying algebraic expressionsSimplifying algebraic expressions
Simplifying algebraic expressionsmanswag123
 
EVEN GRACEFUL LABELLING OF A CLASS OF TREES
EVEN GRACEFUL LABELLING OF A CLASS OF TREESEVEN GRACEFUL LABELLING OF A CLASS OF TREES
EVEN GRACEFUL LABELLING OF A CLASS OF TREESFransiskeran
 
4 7polynomial operations-vertical
4 7polynomial operations-vertical4 7polynomial operations-vertical
4 7polynomial operations-verticalmath123a
 
DSA (Data Structure and Algorithm) Questions
DSA (Data Structure and Algorithm) QuestionsDSA (Data Structure and Algorithm) Questions
DSA (Data Structure and Algorithm) QuestionsRESHAN FARAZ
 
2 4 solving rational equations
2 4 solving rational equations2 4 solving rational equations
2 4 solving rational equationsmath123b
 
March 6, 2014
March 6, 2014March 6, 2014
March 6, 2014khyps13
 
11 applications of factoring
11 applications of factoring11 applications of factoring
11 applications of factoringelem-alg-sample
 
Formation of Triples Consist Some Special Numbers with Interesting Property
Formation of Triples Consist Some Special Numbers with Interesting PropertyFormation of Triples Consist Some Special Numbers with Interesting Property
Formation of Triples Consist Some Special Numbers with Interesting PropertyIRJET Journal
 

What's hot (20)

Btrees
BtreesBtrees
Btrees
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
Notes - Polynomial Division
Notes - Polynomial DivisionNotes - Polynomial Division
Notes - Polynomial Division
 
Algebra factoring
Algebra factoringAlgebra factoring
Algebra factoring
 
3
33
3
 
Chapter 09-Trees
Chapter 09-TreesChapter 09-Trees
Chapter 09-Trees
 
Factorising
FactorisingFactorising
Factorising
 
Alg complex numbers
Alg complex numbersAlg complex numbers
Alg complex numbers
 
Simplifying algebraic expressions
Simplifying algebraic expressionsSimplifying algebraic expressions
Simplifying algebraic expressions
 
factoring
factoringfactoring
factoring
 
Digital Logic
Digital LogicDigital Logic
Digital Logic
 
EVEN GRACEFUL LABELLING OF A CLASS OF TREES
EVEN GRACEFUL LABELLING OF A CLASS OF TREESEVEN GRACEFUL LABELLING OF A CLASS OF TREES
EVEN GRACEFUL LABELLING OF A CLASS OF TREES
 
4 7polynomial operations-vertical
4 7polynomial operations-vertical4 7polynomial operations-vertical
4 7polynomial operations-vertical
 
DSA (Data Structure and Algorithm) Questions
DSA (Data Structure and Algorithm) QuestionsDSA (Data Structure and Algorithm) Questions
DSA (Data Structure and Algorithm) Questions
 
2 4 solving rational equations
2 4 solving rational equations2 4 solving rational equations
2 4 solving rational equations
 
Coding
CodingCoding
Coding
 
March 6, 2014
March 6, 2014March 6, 2014
March 6, 2014
 
11 applications of factoring
11 applications of factoring11 applications of factoring
11 applications of factoring
 
Formation of Triples Consist Some Special Numbers with Interesting Property
Formation of Triples Consist Some Special Numbers with Interesting PropertyFormation of Triples Consist Some Special Numbers with Interesting Property
Formation of Triples Consist Some Special Numbers with Interesting Property
 
Rsa example
Rsa exampleRsa example
Rsa example
 

Viewers also liked (9)

LinkedIn
LinkedInLinkedIn
LinkedIn
 
Lec11
Lec11Lec11
Lec11
 
Lec17
Lec17Lec17
Lec17
 
Lec16
Lec16Lec16
Lec16
 
Lec14
Lec14Lec14
Lec14
 
Lec35
Lec35Lec35
Lec35
 
Lec22
Lec22Lec22
Lec22
 
Lec20
Lec20Lec20
Lec20
 
Lec23
Lec23Lec23
Lec23
 

Similar to Insertion and deletion in red-black trees and a-b trees

Similar to Insertion and deletion in red-black trees and a-b trees (20)

08 B Trees
08 B Trees08 B Trees
08 B Trees
 
Btree
BtreeBtree
Btree
 
CSE680-06HeapSort.ppt
CSE680-06HeapSort.pptCSE680-06HeapSort.ppt
CSE680-06HeapSort.ppt
 
Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02
 
B tree by-jash acharya
B tree by-jash acharyaB tree by-jash acharya
B tree by-jash acharya
 
2 4 Tree
2 4 Tree2 4 Tree
2 4 Tree
 
Red black 1
Red black 1Red black 1
Red black 1
 
b-tree.ppt
b-tree.pptb-tree.ppt
b-tree.ppt
 
Data structures
Data structuresData structures
Data structures
 
Lec13
Lec13Lec13
Lec13
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Trees
TreesTrees
Trees
 
Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Tree
 
Jurnal informatika
Jurnal informatika Jurnal informatika
Jurnal informatika
 
BTree.pptx
BTree.pptxBTree.pptx
BTree.pptx
 
7.tree
7.tree7.tree
7.tree
 
B tree
B  treeB  tree
B tree
 
PPT on Graph Theory ( Tree, Cotree, nodes, branches, incidence , tie set and ...
PPT on Graph Theory ( Tree, Cotree, nodes, branches, incidence , tie set and ...PPT on Graph Theory ( Tree, Cotree, nodes, branches, incidence , tie set and ...
PPT on Graph Theory ( Tree, Cotree, nodes, branches, incidence , tie set and ...
 
Review session2
Review session2Review session2
Review session2
 
Tree
TreeTree
Tree
 

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
 
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
 
Lec20
Lec20Lec20
Lec20
 

Insertion and deletion in red-black trees and a-b trees

  • 1.  Insertion in red-black trees  a-b trees  What are they?  Insertion and deletion in a-b trees
  • 2. Insertion in red-black trees  Let k be the key being inserted  As in the 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.
  • 3. Insertion(2)  Since inserted node is colored red, the black height of the tree remains unchanged.  However, if the parent of inserted node is also red then we have a double red problem. k k No problem Double red problem
  • 4. Insertion: case 1  Parent of inserted node (a) is red.  Parent of (a) must be black (b)  The other child of (b) is black (c). b c k a a c b k The 2-4 tree node contains {b,a,k} and is malformed. The rotation corrects the defect.
  • 5. Insertion: Case 2  Parent of inserted node (a) is red.  Parent of (a) must be black (b)  The other child of (b) is also red (c). b c k a b c k a k c b a a k b c
  • 6. Insertion: case 2 (contd)  The parent of b could also be red. In that case, the double red problem moves up a level.  We repeat this process at the next level.  Eventually, we might color the root red.  In this case we recolor the root black. This increases the black depth of every external node by 1.  In the 2-4 tree this corresponds to splitting the root.
  • 7. Insertion and Deletion: Summary  In both insertion and deletion we need to make at most one rotation. We might have to move up the tree but in doing so we only recolor nodes.  Time taken is O(log n)
  • 8. (a,b) Trees  A multiway search tree.  Each node has at least a and at most b children.  Root can have less than a children but it has at least 2 children.  All leaf nodes are at the same level.  Height h of (a,b) tree is at least logb n and at most loga n. 12 5 10 15 3 4 6 8 11 13 14 17
  • 9. Insertion  No problem if the node has empty space 13 3 8 18 25 29 1 2 4 5 9 14 20 24 26 28 21 23 7 22 15
  • 10. Insertion(2)  Nodes get split if there is insufficient space.  The median key is promoted to the parent node and inserted there 25 24 26 13 29 7 23 22 3 8 18 1 2 4 5 9 14 15 20 28
  • 11. Insertion(3)  A node is split when it has exactly b keys. One of these is promoted to the parent and the remaining are split between two nodes.  Thus one node gets and the other keys.  This implies that a-1 >=
  • 12. Deletion  If after deleting a key a node becomes empty then we borrow a key from its sibling.  Delete 20 1 4 9 11 14 20 24 28 26 18 3 13 12 8 10 6 5 22 15 2 23 29 30 7
  • 13. Deletion(2)  If sibling has only one key then we merge with it.  The key in the parent node separating these two siblings moves down into the merged node.  Delete 23 18 3 13 1 4 9 11 12 14 20 8 10 6 5 2 22 7 15 24 28 23 26 29 30
  • 14. Deletion(3)  In an (a,b) tree we will merge a node with its sibling if the node has a-2 keys and its sibling has a-1 keys.  Thus the merged node has 2(a-1) keys.  This implies that 2(a-1) <= b-1 which is equivalent to a-1 <= .  Earlier too we argued that a-1 <=  This implies b >= 2a-1  For a=2, b >= 3
  • 15. Conclusion  The height of a (a,b) tree is O(log n).  b >= 2a-1.  For insertion and deletion we take time proportional to the height.