0% found this document useful (0 votes)
108 views51 pages

B Trees

The document describes B-trees and provides examples of operations like insertion, deletion, and splitting nodes. It begins with key properties of B-trees like all leaves being at the same level and internal nodes containing a minimum and maximum number of child pointers and keys. Examples then demonstrate inserting values into an empty B-tree, handling overflow by splitting nodes, and repairing underflow during deletion by borrowing or merging nodes.

Uploaded by

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

B Trees

The document describes B-trees and provides examples of operations like insertion, deletion, and splitting nodes. It begins with key properties of B-trees like all leaves being at the same level and internal nodes containing a minimum and maximum number of child pointers and keys. Examples then demonstrate inserting values into an empty B-tree, handling overflow by splitting nodes, and repairing underflow during deletion by borrowing or merging nodes.

Uploaded by

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

B.

Trees
B-Tree vs. Binary Search Tree
B-Trees
• B-trees are balanced search trees designed to
work well on magnetic disks or other direct-
access secondary storage devices.
• they are better at minimizing disk I/O operations
• Many database systems use B-trees, or variants
of B-trees, to store information.
• B-tree nodes may have many children, from a
handful to thousands as compared to binary.
• If an internal B-tree node x contains n[x] keys,
then x has n[x] + 1 children
B Tree
Properties of B-Tree
1) All leaves are at same level.(Max. child possible m)
2) A B-Tree is defined by the term minimum degree (child nodes) ‘t’.
(= ceil[m/2] children )
3) Every node except root must contain at least t-1(ceil[m/2] -1)
keys. Root may contain minimum 1 key.
4) All nodes (including root) may contain at most m-1 keys.
5) Number of children of a node is equal to the number of keys in it
plus 1.
6) All keys of a node are sorted in increasing order. The child
between two keys k1 and k2 contains all keys in the range from k1
and k2.
7) B-Tree grows and shrinks from the root which is unlike Binary
Search Tree. Binary Search Trees grow downward and also shrink
from downward.
Example
B Tree Insertion
• Inserted in leaf as per order.
• If Leaf node gets full after insertion then split
the node from middle. And move middle
element to parent node.(as shown)

• Step 2 is done recursively.


For a tree with m=6(or t=3)
Insert sequence of integers
10, 20, 30, 40, 50, 60, 70, 80 and 90 in
an initially empty B-Tree.
Insert 10
?
Deletion: If key to delete is in leaf
A-> leaf node contain more than min. of keys.
Simply delete
B-> leaf node contain min. no. of keys
i) Borrow key from Immediate left if available(left-
>parent->leaf)
ii) Borrow key from Immediate Right if
available(right->parent>leaf)
iii) Borrow key from Parent if left and right not
available then either left or right and parent is
merged and required key is deleted. Connect the
merged node to parent as per order. After merging
any case may again arise so work accordingly.
Deletion: If key is part of internal node,

A- Check inorder predecessor, Inorder successor/


i) if Node having inorder predecessor is having
more than min. number of keys. Replace with
inorder predecessor.
ii) Check Inorder successor node having more than
min. then replace the inorder successor.
iii) If both Inorder successor and Predecessor having
min. no. of keys. Then apply merge with inorder
predecessor node, parent value and inorder
successor node.
Given Tree of order 5 is – Delete- F
After deleting F
Now delete- M
After Deleting M
Now delete G
After Deleting G.
B-Tree Example with m = 5

12

2 3 8 13 27

The root has been 2 to m children.


Each non-root internal node has between m/2 and m children. Or m/2
-1 keys
All external nodes/leaf nodes are at the same level.

20
Insert 10

12

2 3 8 10 13 27

We find the location for 10 by following a path from the root using the stored
key values to guide the search.
The search falls out the tree at the 4th child of the 1st child of the root.
The 1st child of the root has room for the new element, so we store it there.

21
Insert 11

12

2 3 8 10 11 13 27

We fall out of the tree at the child to the right of key 10.
But there is no more room in the left child of the root to hold 11.
Therefore, we must split this node...

22
Insert 11 (Continued)

8 12

2 3 10 11 13 27

The m + 1 children are divided evenly between the old and new nodes.
The parent gets one new child. (If the parent become overfull, then it, too, will
have to be split).

23
Remove 8

8 12

2 3 10 11 13 27

Removing 8 might force us to move another key up from one of the children. It
could either be the 3 from the 1st child or the 10 from the second child.
However, neither child has more than the minimum number of children (3), so
the two nodes will have to be merged. Nothing moves up.

24
Remove 8 (Continued)

12

2 3 10 11 13 27

The root contains one fewer key, and has one fewer child.

25
Remove 13

12

2 3 10 11 13 27

Removing 13 would cause the node containing it to become underfull.


To fix this, we try to reassign one key from a sibling that has spares.

26
Remove 13 (Cont)

11

2 3 10 12 27

The 13 is replaced by the parent’s key 12.


The parent’s key 12 is replaced by the spare key 11 from the left sibling.
The sibling has one fewer element.

27
Remove 11

11

2 3 10 12 27

11 is in a non-leaf, so replace it by the value immediately preceding: 10.


10 is at leaf, and this node has spares, so just delete it there.

28
Remove 11 (Cont)

10

2 3 12 27

29
Remove 2

10

2 3 12 27

Although 2 is at leaf level, removing it leads to an underfull node.


The node has no left sibling. It does have a right sibling, but that node is at its
minimum occupancy already.
Therefore, the node must be merged with its right sibling.

30
Remove 2 (Cont)

3 10 12 27

The result is illegal, because the root does not have at least 2 children.
Therefore, we must remove the root, making its child the new root.

31
Remove 2 (Cont)

3 10 12 27

The new B-tree has only one node, the root.

32
Insert 49

3 10 12 27

Let’s put an element into this B-tree.

33
Insert 49 (Cont)

3 10 12 27 49

Adding this key make the node overfull, so it must be split into two.
But this node was the root.
So we must construct a new root, and make these its children.

34
Insert 49 (Cont)

12

3 10 27 49

The middle key (12) is moved up into the root.


The result is a B-tree with one more level.

35
Construct B Tree with Following data:
50,80,10,20,60,70,75,90,95,4,5,6,14,23,27,
15,16,51,52,64,68,65,72,78,77,79,73,92,93
,110,100,111,81,82,89
50,80,10,20,60,70,75,90,95,4,5,6,14,2
3,27,15,16,51,52,64,68,65,72,78,77,79
,73,92,93,110,100,111,81,82,89
Insert 50,80,10,20

10 20 50 80

Insert 60
10 20 50 60 80

Split at 50
50

10 20 60 80
50,80,10,20,60,70,75,90,95,4,5,6,14,
23,27,15,16,51,52,64,68,65,72,78,77,7
9,73,92,93,110,100,111,81,82,89

50
Insert 70,75
10 20 60 70 75 80

Insert 90 60 70 75 80 90

50 75

10 20 60 70 80 90
• Insert 95,4,5
50 75

4 5 10 20 60 70 80 90 95

• Insert 6
6 50 75

10 20 60 70 80 90 95
4 5
6 50 75
• Insert 14,23
10 14 20 23 60 70 80 90 95
4 5

• Insert 27 using split


6 20 50 75

23 27 60 70 80 90 95
4 5 10 14
• Insert 15,16,51,52 6 20 50 75

23 27 51 52 60 70 80 90 95
4 5 10 14 15 16

• Insert 64 using split but parent exceeds


capacity so further devide
6 20 50 60 75

51 52 64 70
• Insert 64 using split(2 level split required)

50

6 20 60 75

4 5 51 52 64 70 80 90 95
10 14 15 16 23 27
• Insert 68,65

50

6 20 60 75

4 5 23 27 51 52 64 65 68 70 80 90 95
10 14 15 16
50

• Insert 72,77
6 20 60 68 75

4 5 23 27 51 52 77 80 90 95
10 14 15 16

64 65 70 72
50

• Insert 79
6 20 60 68 75 80

4 5 23 27 51 52 77 79 90 95
10 14 15 16

64 65 70 72
50

• Insert 73,92,93
6 20 60 68 75 80

77 79 90 92 93 95

4 5 23 27 51 52
10 14 15 16

64 65 70 72 73
50

• Insert 110 , Resolve 93


6 20 60 68 75 80 93

90 92 95 110

4 5 10 14 15 16 23 27 51 52 77 79

64 65 70 72 73
Resolving 93
• Insert 110 , Resolve 93
50 75
,

6 20 60 68 80 93

90 92 95 110

10 14 15 16 23 27 51 52 77 79
4 5

64 65 70 72 73
• Insert100, 111
50 75
,

6 20 60 68 80 93

90 92 95 100 110 111

10 14 15 16 23 27 51 52 77 79
4 5

64 65 70 72 73
• Insert 81,82,89
50 75
,

6 20 60 68 80 89 93
95 100 110 111
4 5

10 14 15 16 23 27 51 52 77 79
81 82 90 92
64 65 70 72 73
Delete 64,23,72, 65, 20,70,95, 77, 80,
100, 6,27,60,16,50,

You might also like