SlideShare a Scribd company logo
B-Trees
Dr. AMIT KUMAR @JUET
Motivation for B-Trees
• So far we have assumed that we can store an entire data
structure in main memory
• What if we have so much data that it won’t fit?
• We will have to use disk storage but when this happens our
time complexity fails
• The problem is that Big-Oh analysis assumes that all
operations take roughly equal time
• This is not the case when disk access is involved
Dr. AMIT KUMAR @JUET
Motivation (cont.)
• Assume that a disk spins at 3600 RPM
• In 1 minute it makes 3600 revolutions, hence one revolution
occurs in 1/60 of a second, or 16.7ms
• On average what we want is half way round this disk – it will
take 8ms
• This sounds good until you realize that we get 120 disk
accesses a second – the same time as 25 million instructions
• In other words, one disk access takes about the same time as
200,000 instructions
• It is worth executing lots of instructions to avoid a disk access
Dr. AMIT KUMAR @JUET
Motivation (cont.)
• Assume that we use an Binary tree to store all the details of
people in Canada (about 32 million records)
• We still end up with a very deep tree with lots of different
disk accesses; log2 20,000,000 is about 25, so this takes about
0.21 seconds (if there is only one user of the program)
• We know we can’t improve on the log n for a binary tree
• But, the solution is to use more branches and thus less height!
• As branching increases, depth decreases
Dr. AMIT KUMAR @JUET
Definition of a B-tree
• A B-tree of order m is an m-way tree (i.e., a tree where each
node may have up to m children) in which:
1. the number of keys in each non-leaf node is one less than the number
of its children and these keys partition the keys in the children in the
fashion of a search tree
2. all leaves are on the same level
3. all non-leaf nodes except the root have at least m / 2 children
4. the root is either a leaf node, or it has from two to m children
5. a leaf node contains no more than m – 1 keys
• The number m should always be odd
Dr. AMIT KUMAR @JUET
An example B-Tree
51 6242
6 12
26
55 60 7064 9045
1 2 4 7 8 13 15 18 25
27 29 46 48 53
A B-tree of order 5
containing 26 items
Note that all the leaves are at the same level
Dr. AMIT KUMAR @JUET
• Suppose we start with an empty B-tree and keys arrive in the
following order:1 12 8 2 25 6 14 28 17 7 52 16 48 68
3 26 29 53 55 45
• We want to construct a B-tree of order 5
• The first four items go into the root:
• To put the fifth item in the root would violate condition 5
• Therefore, when 25 arrives, pick the middle key to make a
new root
Constructing a B-tree
1281 2
Dr. AMIT KUMAR @JUET
Constructing a B-tree
Add 25 to the tree
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
1281 2 25
Exceeds Order.
Promote middle and
split.
Dr. AMIT KUMAR @JUET
Constructing a B-tree (contd.)
6, 14, 28 get added to the leaf nodes:
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
12
8
1 2 25
12
8
1 2 2561 2 2814
Dr. AMIT KUMAR @JUET
Constructing a B-tree (contd.)
Adding 17 to the right leaf node would over-fill it, so we take
the middle key, promote it (to the root) and split the leaf
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
12
8
2 2561 2 2814 2817
Dr. AMIT KUMAR @JUET
Constructing a B-tree (contd.)
7, 52, 16, 48 get added to the leaf nodes
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
12
8
2561 2 2814
17
7 5216 48
Dr. AMIT KUMAR @JUET
Constructing a B-tree (contd.)
Adding 68 causes us to split the right most leaf,
promoting 48 to the root
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
8 17
7621 161412 52482825 68
Dr. AMIT KUMAR @JUET
Constructing a B-tree (contd.)
Adding 3 causes us to split the left most leaf
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
48178
7621 161412 25 28 52 683 7
Dr. AMIT KUMAR @JUET
Constructing a B-tree (contd.)
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
Add 26, 29, 53, 55 then go into the leaves
481783
1 2 6 7 52 6825 28161412 26 29 53 55
Dr. AMIT KUMAR @JUET
Constructing a B-tree (contd.)
Add 45 increases the trees level
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
481783
29282625 685553521614126 71 2 45
Exceeds Order.
Promote middle and
split.
Exceeds Order.
Promote middle and
split.
Dr. AMIT KUMAR @JUET
Inserting into a B-Tree
• Attempt to insert the new key into a leaf
• If this would result in that leaf becoming too big, split the leaf
into two, promoting the middle key to the leaf’s parent
• If this would result in the parent becoming too big, split the
parent into two, promoting the middle key
• This strategy might have to be repeated all the way to the top
• If necessary, the root is split in two and the middle key is
promoted to a new root, making the tree one level higher
Dr. AMIT KUMAR @JUET
Exercise in Inserting a B-Tree
• Insert the following keys to a 5-way B-tree:
• 3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4, 31, 35, 56
Dr. AMIT KUMAR @JUET
Answer to Exercise
Java Applet Source
Dr. AMIT KUMAR @JUET
Removal from a B-tree
• During insertion, the key always goes into a leaf. For deletion
we wish to remove from a leaf. There are three possible ways
we can do this:
• 1 - If the key is already in a leaf node, and removing it doesn’t
cause that leaf node to have too few keys, then simply remove
the key to be deleted.
• 2 - If the key is not in a leaf then it is guaranteed (by the
nature of a B-tree) that its predecessor or successor will be in
a leaf -- in this case can we delete the key and promote the
predecessor or successor key to the non-leaf deleted key’s
position.
Dr. AMIT KUMAR @JUET
Removal from a B-tree (2)
• If (1) or (2) lead to a leaf node containing less than the
minimum number of keys then we have to look at the siblings
immediately adjacent to the leaf in question:
– 3: if one of them has more than the min’ number of keys then we can
promote one of its keys to the parent and take the parent key into our
lacking leaf
– 4: if neither of them has more than the min’ number of keys then the
lacking leaf and one of its neighbours can be combined with their
shared parent (the opposite of promoting a key) and the new leaf will
have the correct number of keys; if this step leave the parent with too
few keys then we repeat the process up to the root itself, if required
Dr. AMIT KUMAR @JUET
Type #1: Simple leaf deletion
12 29 52
2 7 9 15 22 56 69 7231 43
Delete 2: Since there are enough
keys in the node, just delete it
Assuming a 5-way
B-Tree, as before...
Note when printed: this slide is animated
Dr. AMIT KUMAR @JUET
Type #2: Simple non-leaf deletion
12 29 52
7 9 15 22 56 69 7231 43
Delete 52
Borrow the predecessor
or (in this case) successor
56
Note when printed: this slide is animated
Dr. AMIT KUMAR @JUET
Type #4: Too few keys in node and
its siblings
12 29 56
7 9 15 22 69 7231 43
Delete 72
Too few keys!
Join back together
Note when printed: this slide is animated
Dr. AMIT KUMAR @JUET
Type #4: Too few keys in node and
its siblings
12 29
7 9 15 22 695631 43
Note when printed: this slide is animated
Dr. AMIT KUMAR @JUET
Type #3: Enough siblings
12 29
7 9 15 22 695631 43
Delete 22
Demote root key and
promote leaf key
Note when printed: this slide is animated
Dr. AMIT KUMAR @JUET
Type #3: Enough siblings
12
297 9 15
31
695643
Note when printed: this slide is animated
Dr. AMIT KUMAR @JUET
Exercise in Removal from a B-Tree
• Given 5-way B-tree created by these data (last exercise):
• 3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4, 31, 35, 56
• Add these further keys: 2, 6,12
• Delete these keys: 4, 5, 7, 3, 14
Dr. AMIT KUMAR @JUET
Answer to Exercise
Java Applet Source
Dr. AMIT KUMAR @JUET
Analysis of B-Trees
• The maximum number of items in a B-tree of order m and height h:
root m – 1
level 1 m(m – 1)
level 2 m2(m – 1)
. . .
level h mh(m – 1)
• So, the total number of items is
(1 + m + m2 + m3 + … + mh)(m – 1) =
[(mh+1 – 1)/ (m – 1)] (m – 1) = mh+1 – 1
• When m = 5 and h = 2 this gives 53 – 1 = 124
Dr. AMIT KUMAR @JUET
Reasons for using B-Trees
• When searching tables held on disc, the cost of each disc
transfer is high but doesn't depend much on the amount of
data transferred, especially if consecutive items are transferred
– If we use a B-tree of order 101, say, we can transfer each node in one
disc read operation
– A B-tree of order 101 and height 3 can hold 1014 – 1 items
(approximately 100 million) and any item can be accessed with 3 disc
reads (assuming we hold the root in memory)
• If we take m = 3, we get a 2-3 tree, in which non-leaf nodes
have two or three children (i.e., one or two keys)
– B-Trees are always balanced (since the leaves are all at the same
level), so 2-3 trees make a good type of balanced tree
Dr. AMIT KUMAR @JUET

More Related Content

What's hot

Stattistic ii - mode, median, mean
Stattistic ii - mode, median, meanStattistic ii - mode, median, mean
Stattistic ii - mode, median, meanamsy1224
 
Notes 3-3
Notes 3-3Notes 3-3
Notes 3-3
Jimbo Lamb
 
Learning short cuts of vedic mathematics
Learning short cuts of vedic mathematicsLearning short cuts of vedic mathematics
Learning short cuts of vedic mathematics
Dr.Jaganmohana Rao Gurugubelli
 
Mean, mode, median
Mean, mode, medianMean, mode, median
Mean, mode, median
Patricia Rossouw
 
Lesson 1.7 grade 8
Lesson 1.7   grade 8Lesson 1.7   grade 8
Lesson 1.7 grade 8
wzuri
 
3.3 Mean, Median, Mode, Formulas
3.3 Mean, Median, Mode, Formulas3.3 Mean, Median, Mode, Formulas
3.3 Mean, Median, Mode, Formulas
Jessca Lundin
 
epsy applied-cog-psy
epsy applied-cog-psyepsy applied-cog-psy
epsy applied-cog-psy
vpletap
 
Sherri Collie
Sherri CollieSherri Collie
Sherri Collie
smcbubbles
 
Week 6 lecture_math_221_dec_2012
Week 6 lecture_math_221_dec_2012Week 6 lecture_math_221_dec_2012
Week 6 lecture_math_221_dec_2012
Brent Heard
 
Lesson 1.6 (8)
Lesson 1.6 (8)Lesson 1.6 (8)
Lesson 1.6 (8)
wzuri
 
(8) Lesson 9.5
(8) Lesson 9.5(8) Lesson 9.5
(8) Lesson 9.5
wzuri
 
Unit Equations
Unit EquationsUnit Equations
Unit Equations
Xavier Tan
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
Megha V
 
VEDIVISION – A FAST BCD DIVISION ALGORITHM FACILITATED BY VEDIC MATHEMATICS
VEDIVISION – A FAST BCD DIVISION ALGORITHM FACILITATED BY VEDIC MATHEMATICSVEDIVISION – A FAST BCD DIVISION ALGORITHM FACILITATED BY VEDIC MATHEMATICS
VEDIVISION – A FAST BCD DIVISION ALGORITHM FACILITATED BY VEDIC MATHEMATICS
ijcsit
 
STATISTIKA (Ukuran Penyebaran Data) - Pertemuan 3
STATISTIKA (Ukuran Penyebaran Data) - Pertemuan 3STATISTIKA (Ukuran Penyebaran Data) - Pertemuan 3
STATISTIKA (Ukuran Penyebaran Data) - Pertemuan 3
Shinta Novianti
 
MEAN MODE MEDIAN AND RANGE
MEAN MODE MEDIAN AND RANGEMEAN MODE MEDIAN AND RANGE
MEAN MODE MEDIAN AND RANGE
RidaZaman1
 
7.ee.1 unit 2 quiz 1 review
7.ee.1 unit 2 quiz 1 review7.ee.1 unit 2 quiz 1 review
7.ee.1 unit 2 quiz 1 review
Beverly Watola
 

What's hot (19)

Stattistic ii - mode, median, mean
Stattistic ii - mode, median, meanStattistic ii - mode, median, mean
Stattistic ii - mode, median, mean
 
P5 ungrouped data
P5 ungrouped dataP5 ungrouped data
P5 ungrouped data
 
Notes 3-3
Notes 3-3Notes 3-3
Notes 3-3
 
Learning short cuts of vedic mathematics
Learning short cuts of vedic mathematicsLearning short cuts of vedic mathematics
Learning short cuts of vedic mathematics
 
Mean, mode, median
Mean, mode, medianMean, mode, median
Mean, mode, median
 
Lesson 1.7 grade 8
Lesson 1.7   grade 8Lesson 1.7   grade 8
Lesson 1.7 grade 8
 
3.3 Mean, Median, Mode, Formulas
3.3 Mean, Median, Mode, Formulas3.3 Mean, Median, Mode, Formulas
3.3 Mean, Median, Mode, Formulas
 
epsy applied-cog-psy
epsy applied-cog-psyepsy applied-cog-psy
epsy applied-cog-psy
 
Sherri Collie
Sherri CollieSherri Collie
Sherri Collie
 
Week 6 lecture_math_221_dec_2012
Week 6 lecture_math_221_dec_2012Week 6 lecture_math_221_dec_2012
Week 6 lecture_math_221_dec_2012
 
Chapter 3.2
Chapter 3.2Chapter 3.2
Chapter 3.2
 
Lesson 1.6 (8)
Lesson 1.6 (8)Lesson 1.6 (8)
Lesson 1.6 (8)
 
(8) Lesson 9.5
(8) Lesson 9.5(8) Lesson 9.5
(8) Lesson 9.5
 
Unit Equations
Unit EquationsUnit Equations
Unit Equations
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
 
VEDIVISION – A FAST BCD DIVISION ALGORITHM FACILITATED BY VEDIC MATHEMATICS
VEDIVISION – A FAST BCD DIVISION ALGORITHM FACILITATED BY VEDIC MATHEMATICSVEDIVISION – A FAST BCD DIVISION ALGORITHM FACILITATED BY VEDIC MATHEMATICS
VEDIVISION – A FAST BCD DIVISION ALGORITHM FACILITATED BY VEDIC MATHEMATICS
 
STATISTIKA (Ukuran Penyebaran Data) - Pertemuan 3
STATISTIKA (Ukuran Penyebaran Data) - Pertemuan 3STATISTIKA (Ukuran Penyebaran Data) - Pertemuan 3
STATISTIKA (Ukuran Penyebaran Data) - Pertemuan 3
 
MEAN MODE MEDIAN AND RANGE
MEAN MODE MEDIAN AND RANGEMEAN MODE MEDIAN AND RANGE
MEAN MODE MEDIAN AND RANGE
 
7.ee.1 unit 2 quiz 1 review
7.ee.1 unit 2 quiz 1 review7.ee.1 unit 2 quiz 1 review
7.ee.1 unit 2 quiz 1 review
 

Similar to B trees

BTrees-fall2010.ppt
BTrees-fall2010.pptBTrees-fall2010.ppt
BTrees-fall2010.ppt
zalanmvb
 
B trees
B treesB trees
B tree by-jash acharya
B tree by-jash acharyaB tree by-jash acharya
B tree by-jash acharya
Jash Acharya
 
B trees
B treesB trees
Presentation on b trees [autosaved]
Presentation on b trees [autosaved]Presentation on b trees [autosaved]
Presentation on b trees [autosaved]
AKASHKUMAR1542
 
16807097.ppt b tree are a good data structure
16807097.ppt b tree are a good data structure16807097.ppt b tree are a good data structure
16807097.ppt b tree are a good data structure
SadiaSharmin40
 
Lecture 9 b tree
Lecture 9 b treeLecture 9 b tree
Lecture 9 b tree
Abirami A
 
trees
trees trees
B trees2
B trees2B trees2
B trees2
Aman Jatain
 
B tree-180214044656
B tree-180214044656B tree-180214044656
B tree-180214044656
kirupasuchi1996
 
B tree
B  treeB  tree
4-b-tree.ppt
4-b-tree.ppt4-b-tree.ppt
4-b-tree.ppt
meenamadhuvandhi2
 
B trees
B treesB trees
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data StructureAnuj Modi
 
B TREE ( a to z concept ) in data structure or DBMS
B TREE ( a to z concept ) in data structure  or DBMSB TREE ( a to z concept ) in data structure  or DBMS
B TREE ( a to z concept ) in data structure or DBMS
MathkeBhoot
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
Ashish Arun
 
Data structures trees - B Tree & B+Tree.pptx
Data structures trees - B Tree & B+Tree.pptxData structures trees - B Tree & B+Tree.pptx
Data structures trees - B Tree & B+Tree.pptx
MalligaarjunanN
 

Similar to B trees (20)

BTrees-fall2010.ppt
BTrees-fall2010.pptBTrees-fall2010.ppt
BTrees-fall2010.ppt
 
B trees
B treesB trees
B trees
 
08 B Trees
08 B Trees08 B Trees
08 B Trees
 
B tree by-jash acharya
B tree by-jash acharyaB tree by-jash acharya
B tree by-jash acharya
 
Btree
BtreeBtree
Btree
 
B trees
B treesB trees
B trees
 
Presentation on b trees [autosaved]
Presentation on b trees [autosaved]Presentation on b trees [autosaved]
Presentation on b trees [autosaved]
 
16807097.ppt b tree are a good data structure
16807097.ppt b tree are a good data structure16807097.ppt b tree are a good data structure
16807097.ppt b tree are a good data structure
 
Lecture 9 b tree
Lecture 9 b treeLecture 9 b tree
Lecture 9 b tree
 
trees
trees trees
trees
 
B trees2
B trees2B trees2
B trees2
 
B tree-180214044656
B tree-180214044656B tree-180214044656
B tree-180214044656
 
B tree
B  treeB  tree
B tree
 
Btrees
BtreesBtrees
Btrees
 
4-b-tree.ppt
4-b-tree.ppt4-b-tree.ppt
4-b-tree.ppt
 
B trees
B treesB trees
B trees
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data Structure
 
B TREE ( a to z concept ) in data structure or DBMS
B TREE ( a to z concept ) in data structure  or DBMSB TREE ( a to z concept ) in data structure  or DBMS
B TREE ( a to z concept ) in data structure or DBMS
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
 
Data structures trees - B Tree & B+Tree.pptx
Data structures trees - B Tree & B+Tree.pptxData structures trees - B Tree & B+Tree.pptx
Data structures trees - B Tree & B+Tree.pptx
 

More from Amit Kumar Rathi

Hybrid Systems using Fuzzy, NN and GA (Soft Computing)
Hybrid Systems using Fuzzy, NN and GA (Soft Computing)Hybrid Systems using Fuzzy, NN and GA (Soft Computing)
Hybrid Systems using Fuzzy, NN and GA (Soft Computing)
Amit Kumar Rathi
 
Fundamentals of Genetic Algorithms (Soft Computing)
Fundamentals of Genetic Algorithms (Soft Computing)Fundamentals of Genetic Algorithms (Soft Computing)
Fundamentals of Genetic Algorithms (Soft Computing)
Amit Kumar Rathi
 
Fuzzy Systems by using fuzzy set (Soft Computing)
Fuzzy Systems by using fuzzy set (Soft Computing)Fuzzy Systems by using fuzzy set (Soft Computing)
Fuzzy Systems by using fuzzy set (Soft Computing)
Amit Kumar Rathi
 
Fuzzy Set Theory and Classical Set Theory (Soft Computing)
Fuzzy Set Theory and Classical Set Theory (Soft Computing)Fuzzy Set Theory and Classical Set Theory (Soft Computing)
Fuzzy Set Theory and Classical Set Theory (Soft Computing)
Amit Kumar Rathi
 
Associative Memory using NN (Soft Computing)
Associative Memory using NN (Soft Computing)Associative Memory using NN (Soft Computing)
Associative Memory using NN (Soft Computing)
Amit Kumar Rathi
 
Back Propagation Network (Soft Computing)
Back Propagation Network (Soft Computing)Back Propagation Network (Soft Computing)
Back Propagation Network (Soft Computing)
Amit Kumar Rathi
 
Fundamentals of Neural Network (Soft Computing)
Fundamentals of Neural Network (Soft Computing)Fundamentals of Neural Network (Soft Computing)
Fundamentals of Neural Network (Soft Computing)
Amit Kumar Rathi
 
Introduction to Soft Computing (intro to the building blocks of SC)
Introduction to Soft Computing (intro to the building blocks of SC)Introduction to Soft Computing (intro to the building blocks of SC)
Introduction to Soft Computing (intro to the building blocks of SC)
Amit Kumar Rathi
 
Topological sorting
Topological sortingTopological sorting
Topological sorting
Amit Kumar Rathi
 
String matching, naive,
String matching, naive,String matching, naive,
String matching, naive,
Amit Kumar Rathi
 
Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
Amit Kumar Rathi
 
Sccd and topological sorting
Sccd and topological sortingSccd and topological sorting
Sccd and topological sorting
Amit Kumar Rathi
 
Red black trees
Red black treesRed black trees
Red black trees
Amit Kumar Rathi
 
Recurrence and master theorem
Recurrence and master theoremRecurrence and master theorem
Recurrence and master theorem
Amit Kumar Rathi
 
Rabin karp string matcher
Rabin karp string matcherRabin karp string matcher
Rabin karp string matcher
Amit Kumar Rathi
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
Amit Kumar Rathi
 
Merge sort analysis
Merge sort analysisMerge sort analysis
Merge sort analysis
Amit Kumar Rathi
 
Loop invarient
Loop invarientLoop invarient
Loop invarient
Amit Kumar Rathi
 
Linear sort
Linear sortLinear sort
Linear sort
Amit Kumar Rathi
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
Amit Kumar Rathi
 

More from Amit Kumar Rathi (20)

Hybrid Systems using Fuzzy, NN and GA (Soft Computing)
Hybrid Systems using Fuzzy, NN and GA (Soft Computing)Hybrid Systems using Fuzzy, NN and GA (Soft Computing)
Hybrid Systems using Fuzzy, NN and GA (Soft Computing)
 
Fundamentals of Genetic Algorithms (Soft Computing)
Fundamentals of Genetic Algorithms (Soft Computing)Fundamentals of Genetic Algorithms (Soft Computing)
Fundamentals of Genetic Algorithms (Soft Computing)
 
Fuzzy Systems by using fuzzy set (Soft Computing)
Fuzzy Systems by using fuzzy set (Soft Computing)Fuzzy Systems by using fuzzy set (Soft Computing)
Fuzzy Systems by using fuzzy set (Soft Computing)
 
Fuzzy Set Theory and Classical Set Theory (Soft Computing)
Fuzzy Set Theory and Classical Set Theory (Soft Computing)Fuzzy Set Theory and Classical Set Theory (Soft Computing)
Fuzzy Set Theory and Classical Set Theory (Soft Computing)
 
Associative Memory using NN (Soft Computing)
Associative Memory using NN (Soft Computing)Associative Memory using NN (Soft Computing)
Associative Memory using NN (Soft Computing)
 
Back Propagation Network (Soft Computing)
Back Propagation Network (Soft Computing)Back Propagation Network (Soft Computing)
Back Propagation Network (Soft Computing)
 
Fundamentals of Neural Network (Soft Computing)
Fundamentals of Neural Network (Soft Computing)Fundamentals of Neural Network (Soft Computing)
Fundamentals of Neural Network (Soft Computing)
 
Introduction to Soft Computing (intro to the building blocks of SC)
Introduction to Soft Computing (intro to the building blocks of SC)Introduction to Soft Computing (intro to the building blocks of SC)
Introduction to Soft Computing (intro to the building blocks of SC)
 
Topological sorting
Topological sortingTopological sorting
Topological sorting
 
String matching, naive,
String matching, naive,String matching, naive,
String matching, naive,
 
Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
 
Sccd and topological sorting
Sccd and topological sortingSccd and topological sorting
Sccd and topological sorting
 
Red black trees
Red black treesRed black trees
Red black trees
 
Recurrence and master theorem
Recurrence and master theoremRecurrence and master theorem
Recurrence and master theorem
 
Rabin karp string matcher
Rabin karp string matcherRabin karp string matcher
Rabin karp string matcher
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Merge sort analysis
Merge sort analysisMerge sort analysis
Merge sort analysis
 
Loop invarient
Loop invarientLoop invarient
Loop invarient
 
Linear sort
Linear sortLinear sort
Linear sort
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 

Recently uploaded

Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
ChristineTorrepenida1
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
ssuser7dcef0
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
symbo111
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 

Recently uploaded (20)

Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 

B trees

  • 2. Motivation for B-Trees • So far we have assumed that we can store an entire data structure in main memory • What if we have so much data that it won’t fit? • We will have to use disk storage but when this happens our time complexity fails • The problem is that Big-Oh analysis assumes that all operations take roughly equal time • This is not the case when disk access is involved Dr. AMIT KUMAR @JUET
  • 3. Motivation (cont.) • Assume that a disk spins at 3600 RPM • In 1 minute it makes 3600 revolutions, hence one revolution occurs in 1/60 of a second, or 16.7ms • On average what we want is half way round this disk – it will take 8ms • This sounds good until you realize that we get 120 disk accesses a second – the same time as 25 million instructions • In other words, one disk access takes about the same time as 200,000 instructions • It is worth executing lots of instructions to avoid a disk access Dr. AMIT KUMAR @JUET
  • 4. Motivation (cont.) • Assume that we use an Binary tree to store all the details of people in Canada (about 32 million records) • We still end up with a very deep tree with lots of different disk accesses; log2 20,000,000 is about 25, so this takes about 0.21 seconds (if there is only one user of the program) • We know we can’t improve on the log n for a binary tree • But, the solution is to use more branches and thus less height! • As branching increases, depth decreases Dr. AMIT KUMAR @JUET
  • 5. Definition of a B-tree • A B-tree of order m is an m-way tree (i.e., a tree where each node may have up to m children) in which: 1. the number of keys in each non-leaf node is one less than the number of its children and these keys partition the keys in the children in the fashion of a search tree 2. all leaves are on the same level 3. all non-leaf nodes except the root have at least m / 2 children 4. the root is either a leaf node, or it has from two to m children 5. a leaf node contains no more than m – 1 keys • The number m should always be odd Dr. AMIT KUMAR @JUET
  • 6. An example B-Tree 51 6242 6 12 26 55 60 7064 9045 1 2 4 7 8 13 15 18 25 27 29 46 48 53 A B-tree of order 5 containing 26 items Note that all the leaves are at the same level Dr. AMIT KUMAR @JUET
  • 7. • Suppose we start with an empty B-tree and keys arrive in the following order:1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 • We want to construct a B-tree of order 5 • The first four items go into the root: • To put the fifth item in the root would violate condition 5 • Therefore, when 25 arrives, pick the middle key to make a new root Constructing a B-tree 1281 2 Dr. AMIT KUMAR @JUET
  • 8. Constructing a B-tree Add 25 to the tree 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 1281 2 25 Exceeds Order. Promote middle and split. Dr. AMIT KUMAR @JUET
  • 9. Constructing a B-tree (contd.) 6, 14, 28 get added to the leaf nodes: 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 12 8 1 2 25 12 8 1 2 2561 2 2814 Dr. AMIT KUMAR @JUET
  • 10. Constructing a B-tree (contd.) Adding 17 to the right leaf node would over-fill it, so we take the middle key, promote it (to the root) and split the leaf 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 12 8 2 2561 2 2814 2817 Dr. AMIT KUMAR @JUET
  • 11. Constructing a B-tree (contd.) 7, 52, 16, 48 get added to the leaf nodes 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 12 8 2561 2 2814 17 7 5216 48 Dr. AMIT KUMAR @JUET
  • 12. Constructing a B-tree (contd.) Adding 68 causes us to split the right most leaf, promoting 48 to the root 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 8 17 7621 161412 52482825 68 Dr. AMIT KUMAR @JUET
  • 13. Constructing a B-tree (contd.) Adding 3 causes us to split the left most leaf 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 48178 7621 161412 25 28 52 683 7 Dr. AMIT KUMAR @JUET
  • 14. Constructing a B-tree (contd.) 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 Add 26, 29, 53, 55 then go into the leaves 481783 1 2 6 7 52 6825 28161412 26 29 53 55 Dr. AMIT KUMAR @JUET
  • 15. Constructing a B-tree (contd.) Add 45 increases the trees level 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 481783 29282625 685553521614126 71 2 45 Exceeds Order. Promote middle and split. Exceeds Order. Promote middle and split. Dr. AMIT KUMAR @JUET
  • 16. Inserting into a B-Tree • Attempt to insert the new key into a leaf • If this would result in that leaf becoming too big, split the leaf into two, promoting the middle key to the leaf’s parent • If this would result in the parent becoming too big, split the parent into two, promoting the middle key • This strategy might have to be repeated all the way to the top • If necessary, the root is split in two and the middle key is promoted to a new root, making the tree one level higher Dr. AMIT KUMAR @JUET
  • 17. Exercise in Inserting a B-Tree • Insert the following keys to a 5-way B-tree: • 3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4, 31, 35, 56 Dr. AMIT KUMAR @JUET
  • 18. Answer to Exercise Java Applet Source Dr. AMIT KUMAR @JUET
  • 19. Removal from a B-tree • During insertion, the key always goes into a leaf. For deletion we wish to remove from a leaf. There are three possible ways we can do this: • 1 - If the key is already in a leaf node, and removing it doesn’t cause that leaf node to have too few keys, then simply remove the key to be deleted. • 2 - If the key is not in a leaf then it is guaranteed (by the nature of a B-tree) that its predecessor or successor will be in a leaf -- in this case can we delete the key and promote the predecessor or successor key to the non-leaf deleted key’s position. Dr. AMIT KUMAR @JUET
  • 20. Removal from a B-tree (2) • If (1) or (2) lead to a leaf node containing less than the minimum number of keys then we have to look at the siblings immediately adjacent to the leaf in question: – 3: if one of them has more than the min’ number of keys then we can promote one of its keys to the parent and take the parent key into our lacking leaf – 4: if neither of them has more than the min’ number of keys then the lacking leaf and one of its neighbours can be combined with their shared parent (the opposite of promoting a key) and the new leaf will have the correct number of keys; if this step leave the parent with too few keys then we repeat the process up to the root itself, if required Dr. AMIT KUMAR @JUET
  • 21. Type #1: Simple leaf deletion 12 29 52 2 7 9 15 22 56 69 7231 43 Delete 2: Since there are enough keys in the node, just delete it Assuming a 5-way B-Tree, as before... Note when printed: this slide is animated Dr. AMIT KUMAR @JUET
  • 22. Type #2: Simple non-leaf deletion 12 29 52 7 9 15 22 56 69 7231 43 Delete 52 Borrow the predecessor or (in this case) successor 56 Note when printed: this slide is animated Dr. AMIT KUMAR @JUET
  • 23. Type #4: Too few keys in node and its siblings 12 29 56 7 9 15 22 69 7231 43 Delete 72 Too few keys! Join back together Note when printed: this slide is animated Dr. AMIT KUMAR @JUET
  • 24. Type #4: Too few keys in node and its siblings 12 29 7 9 15 22 695631 43 Note when printed: this slide is animated Dr. AMIT KUMAR @JUET
  • 25. Type #3: Enough siblings 12 29 7 9 15 22 695631 43 Delete 22 Demote root key and promote leaf key Note when printed: this slide is animated Dr. AMIT KUMAR @JUET
  • 26. Type #3: Enough siblings 12 297 9 15 31 695643 Note when printed: this slide is animated Dr. AMIT KUMAR @JUET
  • 27. Exercise in Removal from a B-Tree • Given 5-way B-tree created by these data (last exercise): • 3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4, 31, 35, 56 • Add these further keys: 2, 6,12 • Delete these keys: 4, 5, 7, 3, 14 Dr. AMIT KUMAR @JUET
  • 28. Answer to Exercise Java Applet Source Dr. AMIT KUMAR @JUET
  • 29. Analysis of B-Trees • The maximum number of items in a B-tree of order m and height h: root m – 1 level 1 m(m – 1) level 2 m2(m – 1) . . . level h mh(m – 1) • So, the total number of items is (1 + m + m2 + m3 + … + mh)(m – 1) = [(mh+1 – 1)/ (m – 1)] (m – 1) = mh+1 – 1 • When m = 5 and h = 2 this gives 53 – 1 = 124 Dr. AMIT KUMAR @JUET
  • 30. Reasons for using B-Trees • When searching tables held on disc, the cost of each disc transfer is high but doesn't depend much on the amount of data transferred, especially if consecutive items are transferred – If we use a B-tree of order 101, say, we can transfer each node in one disc read operation – A B-tree of order 101 and height 3 can hold 1014 – 1 items (approximately 100 million) and any item can be accessed with 3 disc reads (assuming we hold the root in memory) • If we take m = 3, we get a 2-3 tree, in which non-leaf nodes have two or three children (i.e., one or two keys) – B-Trees are always balanced (since the leaves are all at the same level), so 2-3 trees make a good type of balanced tree Dr. AMIT KUMAR @JUET