Implementation of B+ Tree in C

We will implement the B+ tree with its basic operations. Following are some of the basic operations that are required to work with the B+ Tree data structure:

Operation

Description

Time Complexity

Space Complexity

Insert

Adds a new element into the B+ tree and maintains the tree property.

O(log n)

O(1)

Delete Key

Removes an element from the B+ tree.

O(log n)

O(1)

Search

Searches for an element in the B+ tree using the key.

O(log n)

O(1)

Split Child

Splits a full child node during the insertion.

O(m)

O(1)

Merge Node

Merges two nodes during deletion if necessary.

O(m)

O(1)

Display

Traverses the whole B+ tree and prints all the elements.

O(n)

O(1)

where:

  • n: denotes the number of nodes in the B+ Tree.
  • m: denotes the minium order of the tree.

Algorithm for B+ Tree Insert Implementation

Below is the algorithm for the insert function of the B+ tree:

  1. Check if the root node consists of at least two childrens.
  2. Continue traversing the tree until reaching a leaf node.
  3. If the leaf node is not full , insert the element into the key in increasing order.
  4. If the leaf node is full split the leaf node.
  5. After splitting the leaf node, promote the middle key to the parent node.
    1. If the parent node has space, insert the promoted key in the correct position.
    2. If the parent node is full, recursively split the parent node and promote the middle key to it’s parent.
    3. Keep iterating through this procedure until you reach the root node.
  6. If the root node is full, recursively repeat step 2- 5.

Example

The following diagram explains the workflow of inserting a new node in B+ tree:

STEP 1 – Let us take an example of seven elements :2, 4, 7, 10, 17, 21, 28 to insert into the B+ tree.

STEP 2 – Assume that the order is three, then each node must contain at least three nodes and two keys.

STEP 3 – Since we don’t have space to insert 10, which is an overflow condition in the node with three elements, we will split the middle element and then insert it.

STEP 4 – In the same way, we will insert 17 after 10 in the third node.

STEP 4 – Since we don’t have space after 17 to insert 21 in the above third node, we will split 10 from the last leaf node to insert 21 by placing 10 after 7 in the root node.

STEP 5 – To insert 28, we need to send 17 to the above node but, there is no space to insert 17 so, we need to split 7 to place 17 and finally insert 28.

Final B+ Tree after the insertion of all given elements

Algorithm for B+ Tree DeleteKey Implementation

The following is the algorithm to for the deletekey function of the B+ tree:

  1. Find the leaf node that contains the key value.
  2. If the key exists , remove it from the leaf node.
  3. If the leaf node has fewer than n/2 keys after deletion , check it’s sibling to determine if redistribution or merging is needed.
  4. Redistribute or merge with sibling:
    1. If the left sibling has more than ⌈n/2⌉ keys, move the largest key from the left sibling to the current leaf node.
    2. If the right sibling has more than ⌈n/2⌉ keys, move the smallest key from the right sibling to the current leaf node.
  5. Adjust the parent node:
    1. When merging, remove the key in the parent node that pointed to the merged node.
    2. If the parent node now has fewer than ⌈n/2⌉ keys, recursively perform steps 3 and 4 on the parent node.
    3. If merging involves the root node and the root becomes empty, decrease the height of the tree.

Example

The following diagram explains the workflow of inserting a new node in B+ tree:

Let’s assume we want to delete the key 70 from the tree shown in the image.

STEP 1 – First, we need to find the key 70 in the tree. In the given tree, 70 is located in the leaf node [70, 80].

STEP 2 – Remove the key 70 from the leaf node. After removal, the leaf node will contain [80].

STEP 3 – The leaf node [80] after deletion has 1 key, which is the minimum required, so no underflow occurs.

Algorithm for B+ Tree Search Implementation

The following is the algorithm to search an element into the B+ tree:

  1. Start from the root node.
  2. If the current node is a leaf:
    1. Check each key in the node. If a key matches the search key, return true.
    2. If no key matches, return false.
  3. If the current node is not a leaf node:
    1. Iterate through the keys in the node.
    2. If a key in the node is greater than the search key, recursively search the child node corresponding to the current position.
    3. If no such key is found, recursively search the rightmost child node.

Algorithm for B+ Tree Split Implementation

Below is the algorithm for the Split Child function of the B+ tree:

  1. Create a new node newChild.
  2. Identify the middle key in the child node (child) to split the node around it.
  3. Move the keys and children after the middle key from the child node to the newChild node.
  4. Place the middle key into the parent node at the suitable position.
  5. Update the parent node to point to the newChild.
  6. Retain the keys and children up to the middle key in the original child node.


Algorithm for B+ Tree MergeNode Implementation

Below is the algorithm for the Merge Node function of the B+ tree:

  1. Add the separator key from the parent node (which separates the leftNode and rightNode) to the leftNode.
  2. Move all keys and children from the rightNode to the leftNode.
  3. Remove the separator key from the parent node.
  4. Remove the pointer to the rightNode from the parent node.


Algorithm for B+ Tree Traversal

The following is the algorithm for the Display function of the B+ tree:

  1. Start from the root node.
  2. If the present node is a leaf node:
    1. Print all keys in the node.
  3. If the present node is not a leaf node:
    1. For each key in the node, recursively traverse the corresponding child node.
    2. After traversing a child node, print the current key.
    3. Traverse to the rightmost child node after the last key.

Implementation of B+ Tree in C

A B+ tree is a self-balancing tree data structure that maintains sorted data and allows searching, inserting, and deletion of data in logarithmic time. In B+ trees, the actual data is stored inside the leaf nodes and the internal nodes act as pointers to the leaf nodes. In this article, we will learn the implementation of a B+ tree in C.

Similar Reads

B+ Tree in C

B+ Tree is an extension of the B-tree with some specific characteristics that make it particularly useful for database and filesystem implementations....

Implementation of B+ Tree in C

We will implement the B+ tree with its basic operations. Following are some of the basic operations that are required to work with the B+ Tree data structure:...

C Program to Implement B+ Tree

The following program illustrates how we can implement the B+ Tree in C:...