0% found this document useful (0 votes)
178 views62 pages

B Trees

Multi-way search trees, like B-trees, allow nodes to have more than two children. B-trees specifically keep the tree balanced to minimize height. They are commonly used to index large datasets that cannot fit in memory by storing the tree on disk. Insertion and deletion in B-trees involves splitting or merging nodes to balance the tree as keys are added or removed.

Uploaded by

Prakhar Anand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
178 views62 pages

B Trees

Multi-way search trees, like B-trees, allow nodes to have more than two children. B-trees specifically keep the tree balanced to minimize height. They are commonly used to index large datasets that cannot fit in memory by storing the tree on disk. Insertion and deletion in B-trees involves splitting or merging nodes to balance the tree as keys are added or removed.

Uploaded by

Prakhar Anand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

Multi-Way Search 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

„ A m-way search tree T is a tree in


which all nodes are of degree ≤ m.
„ If T = Null, then T is a m-way search
tree.
„ If T ≠ Null then it should satisfy the
properties listed on next slide.
Properties of m-way search tree
„ Properties:
i. T is a node of the type
[n, T0, (k1, T1), (k2, T2), …, (kn-1, Tn-1) ]
where Ti (0 ≤ i < n) are pointers to the sub-trees of T and ki
(1 ≤ i < n) are keys; and 1 ≤ n < m
ii. ki < ki+1 , (1 ≤ i < n)
III. All the keys in sub-tree Ti < ki+1 , (0 ≤ i < n-1)
IV. All the keys in sub-tree Tn-1 > kn-1
V. The sub-tree Ti , (0 ≤ i < n) are also m-way
search trees
Example M-way Search Tree
Example – 3-way search tree
25 78

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

Note that all the leaves are at the same level


Inserting into a B-Tree
„ Attempt to insert the new key into a leaf.
„ If this results in leaf becoming too big, split the leaf
into two, moving up the middle key to the parent of
that leaf.
„ If this again results in the parent becoming too big,
split the parent into two, move the middle key up.
„ 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.
Construction of B-tree of order 3
Contd…
50 Inserted

Now insert 70…


Now insert 90…
90 Inserted
Constructing a B-tree of order 5 - Example

„ Create B tree of order with the following


sequence of keys:

1, 8, 2, 14, 12, 6, 14, 28, 17, 7, 52,


16, 48, 68, 3, 26, 29, 53, 55, 45,
Constructing a B-tree of order 5 - Example

1 2 12 25

6, 14, 28 get added to the leaf nodes:


8

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

7, 52, 16, 48 get added to the leaf nodes


8 17

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

Adding 45 causes a split of and promoting 28 to the root


then causes the root to split
Constructing a B-tree (contd.)

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

DELETE: 2, 52, 72, 22


Delete 2: Since there are enough keys in the node, just delete it
Simple non-leaf deletion

12 29 56
52 Delete 52

7 9 15 22 31 43 56 69 72

Borrow the predecessor


DELETE: 2, 52, 72, 22 or (in this case) successor
Delete 52:
Note when printed: this slide is animated
Too few keys in node and its siblings

12 29 56

Join back together

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

DELETE: 2, 52, 72, 22


Delete 22:
Note when printed: this slide is animated
Enough siblings

12 29
Demote root key and
promote leaf key

7 9 15 22 31 43 56 69

Delete 22
Delete 22:

Note when printed: this slide is animated


Cont.. Enough siblings

12 31

7 9 15 29 43 56 69

Note when printed: this slide is animated


2-3 Search Trees
„ It is B-Tree of degree 3. It is balanced search tree.
„ Either the tree T is empty or it has two children:
– root contains 1 data item
– Root value is greater than each value in left subtree
– Root value is less than each value in right subtree
„ T is of the form: root, left subtree, middle subtree,
right subtree
– Root has two data items
– Smaller value in root is greater than everything in left
subtree and smaller than everything in middle subtree
– Larger value in root is greater than everything in middle
subtree and small than everything in right subtree
Example
50 90

20 70 120 150

10 30 40 60 80 100 110 130 140 160

• Nodes with 2 children must have 1 item


• Nodes with 3 children must have 2 items
• Leaves may contain 1 or 2 items

You might also like