In a multi-way search tree, many nodes have a left subtree but no right subtree. Similarly, they have a right subtree but no left subtree. The insertion of the keys into such a tree also increases the height of the tree.
As we know access time is totally dependent on the level of the tree so we want to minimize the access time through a balanced tree only.
So we have a need to take all the leaf nodes at the same level and non-leaf nodes should not contain the empty subtree. For balancing the tree each node should contain m/2 keys except the root, where m represents m-way search tree.
So the B-Tree of order m can be defined as:
- Each node has at least m/2 and a maximum of m non-empty children.
- All leaf nodes will be at the same level.
- All leaf nodes can contain a maximum of m-1 keys.
- All non-leaf nodes can contain m-1 keys where m is the number of children for that node.
- Keys in the non-leaf node will divide the left and right sub-tree where the value of the left subtree keys will be less and the value of the right subtree keys will be more than the particular key.
Insertion In B-Tree
The insertion of a key in a B-Tree requires the first traversal in B-Tree. Through traversal, it will find the key to be inserted is already existing or not. Suppose the key does not exist then through traversal it will reach the leaf node. Now we have two cases for inserting the key.
- Node is not full.
- Node is already full.
If the leaf node in which the key is to be inserted is not full, then the insertion is done in the node. A node is said to be full if it contains a maximum of m-1 keys, given the order of the B-Tree to be m.
If the node were to be full then insert the key into the existing set of keys in the node, split the node at its median into two nodes at the same level, push the median element up by one level, and rearrange the elements in each level. The following diagrams show how insertion is done in B-Tree of order 5 (i.e. m = 5).
Deletion In B-Tree
Deletion also requires traversal in B-Tree. After reaching on particular node two cases may occur:
- Node is a leaf node.
- Node is non-leaf.
For the first case suppose the node has more than the minimum number of keys then it can be easily detected. But suppose it has only a minimum number of keys, then first we will see the number of keys in the adjacent node will go to the parent node and the key in the parent node which is partitioning will be combined together in one node. suppose now parent has also less than the minimum number of keys then the same thing will be repeated until it will get the node which has more than the minimum number of keys.
For the second case, the key will be deleted and its predecessor or successor key will come in its place. Suppose both nodes of the predecessor or successor key have a minimum number of keys then the nodes of the predecessor and the successor keys will be combined. Let's see how the deletion is done in B-Tree in the following figure.
We have clearly elaborated the deletion mechanism pictorially below.
The source code of insertion and deletion of B-tree is given below,
Insertion and Deletion operations in B-Tree
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.