0% found this document useful (0 votes)
30 views18 pages

Properties of A B-Tree

b

Uploaded by

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

Properties of A B-Tree

b

Uploaded by

saraladosuri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

B-tree

A B-tree is a self-balancing tree data structure designed for efficient storage and retrieval of large amounts of data

Properties of a B-Tree
A B Tree of order m can be defined as an m-way search tree which satisfies the following properties:
1. All leaf nodes of a B tree are at the same level, i.e. they have the same depth (height of the
tree).
2. The keys of each node of a B tree (in case of multiple keys), should be stored in the ascending
order.
3. In a B tree, all non-leaf nodes (except root node) should have at least m/2 children.
4. All nodes (except root node) should have at least m/2 - 1 keys.
5. If the root node is a leaf node (only node in the tree), then it will have no children and will have
at least one key. If the root node is a non-leaf node, then it will have at least 2 children and at
least one key.
6. A non-leaf node with n-1 key values should have n non NULL children.
Operations on B-Tree
Sr. No. Operation Time Complexity

1. Search O(log n)

2. Insert O(log n)

3. Delete O(log n)

4. Traverse O(n)

Note: "n" is the total number of elements in the B-tree


B-Tree Insert example

20 40 60 80
M = 6; t = 3

0 5 10 15 25 35 45 55 62 66 70 74 78 87 98

Insert 3:
20 40 60 80

0 3 5 10 15 25 35 45 55 62 66 70 74 78 87 98
20 40 60 80
M = 6; t = 3

0 3 5 10 15 25 35 45 55 62 66 70 74 78 87 98

Insert 61: 20 40 60 80
OVERFLOW

0 3 5 10 15 25 35 45 55 61 62 66 70 74 78 87 98

SPLIT IT
20 40 60 70 80

0 3 5 10 15 25 35 45 55 61 62 66 74 78 87 98
M = 6; t = 3
20 40 60 70 80
Insert 38:

0 3 5 10 15 25 35 45 55 61 62 66 74 78 87 98

20 40 60 70 80

0 3 5 10 15 25 35 38 45 55 61 62 66 74 78 87 98
Insert 4:
M = 6; t = 3
20 40 60 70 80

0 3 5 10 15 25 35 38 45 55 61 62 66 74 78 87 98

20 40 60 70 80
OVERFLOW

0 3 4 5 10 15 25 35 38 45 55 61 62 66 74 78 87 98

SPLIT IT
OVERFLOW
5 20 40 60 70 80
SPLIT IT

0 3 4 10 15 25 35 38 45 55 61 62 66 74 78 87 98
M = 6; t = 3

OVERFLOW
5 20 40 60 70 80
SPLIT IT

0 3 4 10 15 25 35 38 45 55 61 62 66 74 78 87 98

60

5 20 40 70 80

0 3 4 10 15 25 35 38 45 55 61 62 66 74 78 87 98
B Tree: Deletion
The deletion of nodes in a B-Tree can be broadly classified into two vivid cases:

 deletion at leaf node.

 deletion at internal node.


Let us say the node to be deleted is called the target key. The target key can
either be at the leaf node or an internal node. Let us now consider the various
cases that follow:
Example:
Let us consider the given tree. From the given tree we are to delete the following
elements:

A = 20 , 53 , 89 , 90 , 85.

Assuming we have order = 5; minimum keys = ⌈ m/2⌉ – 1 = 2; maximum keys =


⌈ m/2⌉ + 1 = 4; minimum children = ⌈ m/2⌉ = 3
maximum children = m = 5

1. If the target key is at the leaf node:

If the target key is at the leaf node, we further study the given data to check if any of
the following cases exist:

 Case 1: If the leaf node consists of the min number of keys according to the

given degree/order, then the key is simply deleted from the node.
 Case 2: If the leaf contains the minimum number of keys, then:

 Case 2a: The node can borrow a key from the immediate left sibling node,if it

has more than the minimum number of keys.The transfer of the keys take

place through the parent node, i.e, the maximum key of the left sibling moves

upwards and replaces the parent; while the parent key moves down to the

target node from where the target key is simply deleted.

 Case 2b: The node can borrow a key from the immediate right sibling node, if

it has more than the minimum number of keys.The transfer of the keys take

place through the parent node, i.e, the minimum key of the right sibling

moves upwards and replaces the parent; while the parent key moves down to

the target node from where the target key is simply deleted.

 Case 2c: If neither of the siblings have keys more than the minimum number

of keys required then, merge the target node with either the left or the right

sibling along with the parent key of respective node.


2.If the target key is at the internal node:

If the target key is at an internal node, we further study the given data to check if
any of the following cases exist:

 Case 1: If the left child has more than the minimum number of keys, the

target key in the internal node is replaced by its inorder predecessor ,i.e, the

largest element of the left child node.

 Case 2: If the right child has more than the minimum number of keys, the

target key in the internal node is replaced by it’s inorder successor ,i.e, the

smallest element of the right child node.


Step 1:

 The first element to be deleted from the tree structure is 20.

 We can see the key lies in the leaf node.


Step 2:

 The key 20 exists in a leaf node.

 The node has more than the minimum number of keys required.

 Thus the key is simply deleted from the node.

 The tree after deletion is shown as follows.

Step 3:

 The next element to be deleted from the tree is 53.

 We can see from the image that the key exists in the leaf node.
Step 4:

 Since the node in which the target key 53 exists has just the minimum

number of keys, we cannot delete it directly.

 We check if the target node can borrow a key from it’s sibling nodes.

 Since the target node doesn’t have any right sibling, it borrows from the

left sibling node.

 As we have studied above how the process of borrow and replace takes place,

we apply it to the given structure.

Step 5:
 The key 49 moves upwards to the parent node.

 The key 50 moves down to the target node.

Step 6:

 Now, since the target node has keys more than the minimum number of keys

required, the key can be deleted directly.

 The tree structure after deletion is shown as follows.

Step 7:

 The next element to be deleted is 89.


 The target key lies within a leaf node as seen from the image.

Step 8:

 Again, the target node holds just the minimum number of keys required and

hence the node cannot be deleted directly.

 The target node now has to borrow a key from either of it’s siblings.

 We check the left sibling; it also holds just the minimum number of keys

required.

 We check the right sibling node; it has one more than the minimum number of

nodes so the target node can borrow a key from it.


Step 9:

 The key 93 moves up to the parent node.

 The parent key 90 moves down to the target node.

Step 10:

 Now, as the target node has sufficient number of keys the target key can

directly be deleted from the target node.

 The tree structure after deletion is shown as follows.


Step 11:

 The next key to be deleted is 90.

 The key exists within a leaf node as shown in the image.

Step 12:

 We can see that the target node has just the minimum number of keys.

 The target node has to borrow a key from either of it’s siblings.

 Since each of the siblings just have the number of the minimum keys, it

cannot borrow the keys directly.


Step 13:

 Since the target node cannot borrow from either of the siblings, we merge the

target node, either of the sibling node and the corresponding parent to them.

 The process of merging is shown as follows.

Step 14:

 Since the target node now has sufficient number of keys, the target

key 90 can be deleted directly.

 The tree structure after the deletion of the element is shown as follows.
Step 15:

 The next target node is 85.

 Now here the node to be deleted is not a leaf node but an internal node.

Step 16:

 In case, when an internal node is to be deleted, we replace the key with it’s

inorder predecessor or inorder successor.

 We can select either of the child nodes if they have sufficient number of keys.

 But as we can see in this case the target internal node can only borrow from

it’s right child, i.e, inorder predecessor.

 The key 85 moves down to the child node; key 87 moves up to the parent

node.
Step 17:

 Now, as the target key is moved to the leaf node, it can be simply deleted

from the leaf node.

 The final tree structure after deletion of various nodes and preserving the b-

tree properties is shown as follows.

Applications of B-Trees
 It is used in large databases to access data stored on the
disk
 Searching for data in a data set can be achieved in
significantly less time using the B-Tree
 With the indexing feature, multilevel indexing can be
achieved.
 Most of the servers also use the B-tree approach.
 B-Trees are used in CAD systems to organize and search
geometric data.
 B-Trees are also used in other areas such as natural
language processing, computer networks, and
cryptography.

You might also like