B Trees
B Trees
(B Trees)
Multiway Search Trees
An m-way search tree is a tree in which, for
some integer m called the order of the tree,
each node has at most m children.
If n <= m is the number of children of a node,
then it contains exactly n-1 keys, which
partition all the keys into n subsets
– consisting of all the keys less than the first key in the
node, all the keys between a pair of keys in the
node, and all keys greater than the largest key in the
node.
Formal Definition
15 20 30 80
18
Cont…
In m-way search tree,
– each node may have more than two child nodes
– there is a specific ordering relationship among the
nodes
m-way search tree if it is unbalanced then the
purpose is defeated.
To achieve a better performance, it has to be
balanced m-way search tree.
Particular variety of balanced m-way search
tree is called B-tree.
Motivation - (for B-Trees)
Remember that performance is related to the
height of the tree.
We want to minimize the height of the tree.
Used to process external records (information
too large to put into memory), minimizes
number of accesses to secondary peripheral.
Index structures for large datasets that cannot
be stored in main memory.
Storing it on disk requires different approach to
efficiency.
Motivation – contd…
Assume that we use an AVL tree to store
about 20 million records
We end up with a very deep binary tree with
lots of different disk accesses; log2 20,000,000
is about 24, so this takes about 0.2 seconds
We know we can’t improve on the (log2n) lower
bound on search for a binary tree.
But, the solution is to use more branches and
thus reduce the height of the tree!
– As branching increases, depth decreases
Cont…
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
– 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)
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:
– all leaves are on the same level
– all non-leaf nodes except the root have at least ⎡m /
2⎤ children
– the root is either a leaf node, or it has from 2 to m
children
– a leaf node contains no more than m – 1 keys
The number m should always be odd.
Example B tree
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
An example B-Tree
26 A B-tree of order 5
6 12 containing 26 items
42 51 62
1 2 4 7 8 13 15 18 25
27 29 45 46 48 53 55 60 64 70 90
1 2 12 25
1 2 6 12 14 25 28
Example – Cont…
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
8 17
1 2 6 12 14 25 28
1 2 6 7 12 14 16 25 28 48 52
Contd.
Adding 68 causes us to split the right most leaf, promoting
48 to the root, and adding 3 causes us to split the left
most leaf, promoting 3 to the root; 26, 29, 53, 55 then go
into the leaves
3 8 17 48
1 2 6 7 12 14 16 25 26 28 29 52 53 55 68
25 26 28 29
17
3 8 28 48
1 2 6 7 12 14 16 25 26 29 45 52 53 55 68
Deletion from a B-tree
During insertion, the key always goes into a
leaf. For deletion, 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 we can delete the key and promote the
predecessor or successor key to the non-leaf deleted
key’s position.
Deletion – Contd..
If (1) or (2) leads to a leaf node containing less than the
minimum number of keys then 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
Delete 70…
Now delete 100…
Now delete 80…
Example: Simple leaf deletion
Assuming a B-Tree 12 29 52
of order 5.
2 7 9 15 22 31 43 56 69 72
12 29 56
52 Delete 52
7 9 15 22 31 43 56 69 72
12 29 56
7 9 15 22 31 43 69 72
Delete 72
Too few
DELETE: 2, 52, 72, 22 keys!
Delete 72:
Note when printed: this slide is animated
Cont...Too few keys in node and its siblings
12 29
7 9 15 22 31 43 56 69
12 29
Demote root key and
promote leaf key
7 9 15 22 31 43 56 69
Delete 22
Delete 22:
12 31
7 9 15 29 43 56 69
20 70 120 150