B Tree
By Ritika Bateja
Introduction
• B-Tree is a self-balancing search tree.
• In most of the other self-balancing search trees (like AVL and Red-Black
Trees), it is assumed that everything is in main memory.
• To understand the use of B-Trees, we must think of the huge amount of data
that cannot fit in main memory.
• When the number of keys is high, the data is read from disk in the form of
blocks. Disk access time is very high compared to the main memory access
time. The main idea of using B-Trees is to reduce the number of disk
accesses.
• Most of the tree operations (search, insert, delete, max, min, ..etc ) require
O(h) disk accesses where h is the height of the tree. B-tree is a fat tree. The
height of B-Trees is kept low by putting maximum possible keys in a B-Tree
node. Generally, the B-Tree node size is kept equal to the disk block size.
• Since the height of the B-tree is low so total disk accesses for most of the
operations are reduced significantly compared to balanced Binary Search
Trees like AVL Tree, Red-Black Tree, ..etc.
Introduction to B-Tree
• B-trees are balanced search tree or balanced m-way
search tree where m is the order of tree.
• For m-way search tree, each node can have maximum
m-children and m-1 keys.
• It is generalization of BST, in which each node can have
more than one key and more than 2-children.
• More than two children are possible.
• B-Tree, stores all information in the leaves and stores
only keys and Child pointer.
Example of B-Tree
Another example of B-Tree
Application of B-Tree
It designed to work on magnetic disks or other (direct access)
secondary storage devices.
Also used for creating multilevel indexing.
Properties of B-Tree
• All leaves are at the same level.
• A B-Tree is defined by the term minimum degree ‘t’. The value of t depends
upon disk block size.
• Every node except root must contain at least t-1 keys. The root may contain
minimum 1 key.
• All nodes (including root) may contain at most 2*t – 1 keys.
• Number of children of a node is equal to the number of keys in it plus 1.
• 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.
• B-Tree grows and shrinks from the root which is unlike Binary Search Tree.
Binary Search Trees grow downward and also shrink from downward.
• Like other balanced Binary Search Trees, time complexity to search, insert
and delete is O(log n).
• Insertion of a Node in B-Tree happens only at Leaf Node.
• Following is an example of B-Tree of minimum order 5. Note that in practical
B-Trees, the value of the minimum order is much more than 5.
Height of B-tree
• Theorem:
If n ≥ 1, then for any n-key B-tree T of height h and minimum degree
t ≥ 2,
h ≤ logt (n+1)/2
• Proof:
– The root contains at least one key
– All other nodes contain at least t-1 keys.
– There are at least 2 nodes at depth 1, at least 2t nodes at depth 2, at
least 2ti-1 nodes at depth i and 2th-1 nodes at depth h
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Basic operation on B-tree
• B-TREE-SEARCH :-Searching in B Tree
• B-TREE-INSERT :-Inserting key in B Tree
• B-TREE-CREATE :-Creating a B Tree
• B-TREE-DELETE :- Deleting a key from B
Tree
Time Complexity of B-Tree:
Sr. No. Algorithm Time Complexity
1. Search O(log n)
2. Insert O(log n)
3. Delete O(log n)
X is a subtree and k is searching element
Complexity=O(t)
Complexity=
O(logt n)
Creating an empty B tree
INSERT
t=3
Insert Example
t=3 GMPX
ACDE JK NO RSTUV YZ
Insert B
GMPX
ABCDE JK NO RSTUV YZ
B-Trees
Insert Example (Continued)
GMPX
ABCDE JK NO RSTUV YZ
Insert Q
GMPTX
ABCDE JK NO QRS UV YZ
B-Trees - 20
Insert Example (Continued)
GMPTX
ABCDE JK NO QRS UV YZ
Insert L
GM TX
ABCDE JKL NO QRS UV YZ
B-Trees
Insert Example (Continued)
P
GM TX
ABCDE JKL NO QRS UV YZ
Insert F
CGM TX
AB DEF JKL NO QRS UV YZ
B-Trees - 22
Constructing a B-tree
• Let us understand the algorithm with an
example tree of minimum degree ‘t’ as 3
and a sequence of integers 10, 20, 30, 40,
50, 60, 70, 80 and 90 in an initially empty
B-Tree.
Initially root is NULL. Let us first insert 10.
Let us now insert 20, 30, 40 and 50. They all will be inserted in root because
he maximum number of keys a node can accommodate is 2*t – 1 which is 5.
Let us now insert 60. Since root node is full, it will first split into two,
then 60 will be inserted into the appropriate child.
Let us now insert 70 and 80. These new keys will be inserted into the appropriate leaf without any split.
1
1
2
Constructing a B-tree
8
2
2 Add 6 to the tree
5
6
1
4
2 Exceeds Order. Promote
8 middle and split.
1
7 1 2 8 12 25
7
5
2
1
6
4
8
6
8
1
1
2
Constructing a B-tree (contd.)
8
2
2 8
5
6
1
4
2 1 2 12 25
8
1 6, 14, 28 get added to the leaf nodes:
7
7 8
5
2
1
6
4 1 2 1 6 2 12 14
25 28
8
6
8
1
1
2
Constructing a B-tree (contd.)
8
2
2 Adding 17 to the right leaf node would over-fill it, so we take
5
6 the middle key, promote it (to the root) and split the leaf
1
4 8
2
8
1
7
7 1 2 6
2 25 28 28
12 14 17
5
2
1
6
4
8
6
8
1
1
2
Constructing a B-tree (contd.)
8
2
2 7, 52, 16, 48 get added to the leaf nodes
5
6
1
4 8 17
2
8
1
7
7 1 2 76 12 14
16 25 28 52
48
5
2
1
6
4
8
6
8
1
1
2
Constructing a B-tree (contd.)
8
2
2 Adding 68 causes us to split the right most leaf,
5
6 promoting 48 to the root
1
4
2
8
1 8 17
7
7
5
2 1 2 6 7 12 14 16 25 28 48 52 68
1
6
4
8
6
8
1
1
2
Constructing a B-tree (contd.)
8
2
2 Adding 3 causes us to split the left most leaf
5
6
1 8 17 48
4
2
8
1
7 1 2
3 6 7 12 14 16 25 28 52 68
7
5
2
1
6
4
8
6
8
1
1
2
Constructing a B-tree (contd.)
8
2
2 Add 26, 29, 53, 55 then go into the leaves
5
6
1 3 8 17 48
4
2
8
1
7 1 2 6 7 12 14 16 25262829 52536855
7
5
2
1
6
4
8
6
8
1
1
2
Constructing a B-tree (contd.)
8
2
2 Add 45 increases the trees level Exceeds Order. Promote
5 middle and split.
6
1
4
2 Exceeds Order. Promote
8
1
3 8 17 48 middle and split.
7
7
5
2 1 2 6 7 12 14 16 25 26 28 29 45 52 53 55 68
1
6
4
8
6
8
Exercise in Inserting a B-Tree
• Insert the following keys in B-tree when
t=3 :
• 3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8,
19, 4, 31, 35, 56
• Check your approach with a neighbour
and discuss any differences.
36
Deleting from B-Trees
The Concept
• You can delete a key entry from any node.
• Therefore, you must ensure that
before/after deletion, the B-Tree maintains
its properties.
• When deleting, you have to ensure that a
node doesn’t get too small (minimum node
size is T – 1). We prevent this by
combining nodes together.
Lets look at an example:
We’re given this valid B-Tree
Note: T = 3
Deletion Cases
• Case 1: If the key k is in node x and x is a
leaf node having atleast t keys - then
delete k from x.
x x
…k… leaf … …
t keys t–1 keys
Simple Deletion
Case 1: We delete “F”
Result: We remove “F” from the leaf node. No further
action needed.