Inserting an element into a B+ tree consists of three main events: searching the appropriate leaf, inserting the element and balancing/splitting the tree.
Let us understand these events below.
Before inserting an element into a B+ tree, these properties must be kept in mind.
- The root has at least two children.
- Each node except root can have a maximum of
m
children and at leastm/2
children. - Each node can contain a maximum of
m-1
keys and a minimum of⌈m/2⌉ - 1
keys.
The following steps are followed for inserting an element.
- Since every element is inserted into the leaf node, go to the appropriate leaf node.
- Insert the key into the leaf node.
- If the leaf is not full, insert the key into the leaf node in increasing order.
- If the leaf is full, insert the key into the leaf node in increasing order and balance the tree in the following way.
- Break the node at
m/2
-th position. - Add
m/2
-th key to the parent node as well. - If the parent node is already full, follow steps 2 to 3.
Let us understand the insertion operation with the illustrations below.
The elements to be inserted are 5, 15, 25, 35, 45.
Step 1: Insert 5.
Step 2: Insert 15.
Step 3: Insert 25.
Step 4: Insert 35.
Step 5: Insert 45.
Deleting an element on a B+ tree consists of three main events: searching the node where the key to be deleted exists, deleting the key and balancing the tree if required. Underflow is a situation when there is less number of keys in a node than the minimum number of keys it should hold.
Before going through the steps below, one must know these facts about a B+ tree of degree m.
- A node can have a maximum of m children. (i.e. 3)
- A node can contain a maximum of
m-1
keys. (i.e. 2) - A node should have a minimum of
⌈m/2⌉
children. (i.e. 2) - A node (except root node) should contain a minimum of
⌈m/2⌉ - 1
keys. (i.e. 1)
While deleting a key, we have to take care of the keys present in the internal nodes (i.e. indexes) as well because the values are redundant in a B+ tree. Search the key to be deleted then follow the following steps.
The key to be deleted is present only at the leaf node not in the indexes (or internal nodes). There are two cases for it:
- There is more than the minimum number of keys in the node. Simply delete the key.
- There is an exact minimum number of keys in the node. Delete the key and borrow a key from the immediate sibling. Add the median key of the sibling node to the parent.
The key to be deleted is present in the internal nodes as well. Then we have to remove them from the internal nodes as well. There are the following cases for this situation.
- If there is more than the minimum number of keys in the node, simply delete the key from the leaf node and delete the key from the internal node as well. Fill the empty space in the internal node with the inorder successor.
- If there is an exact minimum number of keys in the node, then delete the key and borrow a key from its immediate sibling (through the parent). Fill the empty space created in the index (internal node) with the borrowed key.
- This case is similar to Case II(1) but here, empty space is generated above the immediate parent node. After deleting the key, merge the empty space with its sibling. Fill the empty space in the grandparent node with the inorder successor.
In this case, the height of the tree gets shrinked. It is a little complicated. Deleting 55 from the tree below leads to this condition. It can be understood in the illustrations below.