Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# B+ Tree in Data Structure

Last Updated on September 22, 2023 by Mayank Dham

The stack pointer, a fundamental concept in computer science and programming, plays a pivotal role in managing memory and controlling program execution. As a core component of the stack data structure, the stack pointer serves as a reference point that determines the top of the stack’s current position in memory. Through its dynamic manipulation, the stack pointer facilitates the orderly storage and retrieval of data, function calls, and program state.

Understanding the intricacies of the stack pointer is essential for grasping memory management, function execution, and the flow of data within a program. This discussion delves into the significance of the stack pointer, its interaction with the stack, and its broader implications in various programming paradigms.

## What is the b+ tree in data structure?

• b+ Tree is a b Tree extension that allows for faster insertion, deletion, and search operations. Keys and records in the b Tree can be stored in both internal and leaf nodes. In contrast, records (data) can only be stored on the leaf nodes of a b+ tree, while internal nodes can only store key values.

• The b+-tree is a tree structure in which each node corresponds to a disc block and which meets the following requirements: The tree is balanced, which means that each leaf node has the same depth. A list of keys and a list of pointers are stored in an internal node. The number of pointers exceeds the number of keys by one.

• The concept of b+ trees exists because they are far more convenient, both in terms of operations and efficiency.

## How to insert a node in B+ tree in data structure

The first step is insertion. In this section, we will also learn how to make a b+ tree.

Before looking deeper into the steps of node insertion, consider the following scenarios:

Case 1: The leaf node is not full.
If the leaf is not full then we will insert the key in increasing order into the leaf node.

Case 2: The leaf node is completely full.
Step 1: We will insert the new node in increasing order as a leaf node. Because the leaf node was already full, some balancing is required.
Step 2: After that, we will split the node at the m/2nd position. (m means an order of tree)
Step 3: Then, we will add the m/2nd key into the parent node.
Step 4: If the parent node is already full then we will simply repeat steps 2 and 3.

Let’s understand the below example to learn how b+ trees are created and elements are added.

Example :
We will use the following data to create b+ tree: 2,5,8,11,18,22,32.
We have the order(m) of the tree to be 4. The following facts can be calculated from this:

``````Max Children = 4
Min Children: m/2 = 2
Max Keys: m - 1 = 3
Min Keys: ⌈m/2⌉ - 1 = 1``````
1. We will begin by inserting 1, 4, and 7 into the first node.

1. The next element to be inserted is number 10, but there is no space in the current node (due to the maximum number of keys being three), so we will split it.
Here, we have to decide whether to break the node at 4 or 7. Breaking the node at 4 will bias the tree to the left while breaking the node at 7 will bias the tree to the right. We will choose either left or right bias and stick with it throughout the process.
We will pick the right biassed tree for this example. As a result, we will split the node at 7 and move it up.

1. We have now divided it into two parts:
(i) 1,4 and a blank space (because the maximum number of keys in a node is 3) and
(ii) 7 and 10. We will now insert 17 into node 2, as it follows 10 in ascending order.

1. 21 is the next number to be inserted, and it should ideally come after 17. However, because the node is already full with three keys, we will have to split it. Because we chose the right biassed in step 2, we will split the node as follows:
(i) 7, 10, and a blank space
(ii) 17, 21, and a blank space

1. At the end, we will insert 31. It should go after node 21 in the final leaf node. We will finish our b+ tree.

## How to delete a node in B+ tree in data structure

We understood how to create b+ tree and how to insert a node in b+ tree. Now, we will understand how to delete a node from b+ tree.
We have two deletion cases, just like we have two insertion cases.

Case 1: We have to check that If the value to be deleted is only found in the leaf node and If the value to be deleted is only present in the leaf node then we will simply delete it.

Case 2: If the deleted value is present in the leaf node and has a pointer to it.
We will locate the node to be deleted and remove it from the leaf node; however, we will also remove it from the index that points to this node.
After that we will remove the pointer and insert the right child’s first value into the parent node.

Example:
Here, we will use case 1 for deleting a node from b+ tree.
We will remove the value 21 from the b+ Tree below:

After deletion:
We can simply remove 21 because it is only present in the leaf node and deleting it will not invalidate the minimum number of keys (which is 1, as calculated).

## How to search a node in B+ tree in data structure

We knew how insertion and deletion work in the b+ tree. Searching in a b+ tree is extremely simple and efficient because the leaf nodes that contain the data are linked to each other by pointers, forming a linked list, as we previously discussed. This is why the b+ tree has applications in DBMS as well.
Now, we will learn how to search specific nodes in the b+ tree with the help of the following algorithm.

1. We will run a binary search on the records of the current node.
2. Then we wil return a record that matches the search key if one is found.
3. If the current node is a leaf node and the key is not found then we will report an unsuccessful search.
4. Otherwise, we will restart the process from the beginning.

Example:
Look for the number 10 in the given b+ tree:

First, we must examine the indices. We have two, ages 7 and 17. We can see that 10 lies between them, so we must look for records between 7 and 17, which is leaf node number 2. And there we have our required value, 10.

## B+ tree program in C

Output:

``````8 18 |
2 5 |               8 11 |          18 22 32 |
[8 18] 0 ->
Leaf [2 5] ->
Record at 0x560a2af40300 -- key 5, value 21.``````

## Advantages of the B+ tree

• Redundant search keys can be present.
• Leaf nodes are linked together to improve the efficiency of search operations.
• The height of the tree remains balanced and lower than that of the B tree.
• Because elements are always deleted from the leaf nodes, deletion will never be a complicated process.

## Disadvantages of the B+ tree in data structure

• The difficulty of traversing the keys sequentially is the main disadvantage of B-tree.

## Application of B+ tree in data structure

• Indexing at Several Levels
• It has faster operations on the tree compared to b tree(insertion, deletion, search)
• Database indexing

Conclusion
The B+ tree is a vital data structure for efficient data organization and retrieval. Its balanced hierarchy, logarithmic time complexity for operations, and optimization for range queries make it indispensable in modern databases. With its ability to handle unpredictable data growth and reduce disk I/O, the B+ tree remains a cornerstone in computer science, enabling effective management of large datasets across various applications.

## FAQ on B+ tree in data structure.

1. What is the maximum number of keys in B+ tree?
A B+ tree of order n and height h can have at most nh – 1 keys. Therefore maximum number of keys = 33 -1 = 27 -1 = 26.

2. What are the rules of creating B+ tree?
The level of each leaf is the same.The root has at least two children.Each node, excluding the root, is allowed to have a maximum of m children as well as at least m /2 children.A minimum of m/2 – 1 keys and a maximum of m – 1 keys are allowed in each node.

3. Why do databases use B+ trees?
B+ tree is used to hold the actual records, while B-tree is used for indexing. In addition to binary search, B+tree also offers sequential search capabilities, giving the database more freedom to seek non-index values.