B+ Tree in DBMS


A B+ tree in DBMS is a specialized version of a balanced tree, a type of tree data structure used in databases to store and retrieve data efficiently. Balanced trees are designed to maintain a roughly equal number of keys at each level, which helps to keep search times as low as possible. B+ trees are a popular choice for use in database management systems(DBMS) because they offer a number of benefits over other types of balanced trees, including faster search times and better space utilization.

What are B+ Trees?

A B+ tree is a self-balancing, ordered tree data structure that stores data in a sorted fashion. Each node in a B+ tree can have a variable number of keys and child pointers, with the exception of the leaf nodes, which only have keys and no child pointers. The keys in a B+ tree are arranged in a specific order, with all keys in a given node being less than any of the keys in its right child and greater than any of the keys in its left child.

B+ trees are characterized by having a large number of keys per node, which helps to keep the height of the tree small and the search times fast. Additionally, B+ trees use a "pointer-based" structure, meaning that each node contains a set of pointers that point to its child nodes rather than the child nodes being stored within the parent node. This helps to reduce the size of each node and allows for better space utilization.

How to Implement a B+ Tree in C++?

Implementing a B+ tree in C++ requires defining a node class that contains the keys and pointers for each node in the tree. The node class should also include a function for inserting a new key into the tree and a function for searching for a specific key in the tree.

Example

Here is an example of how the node class for a B+ tree might be implemented in C++ −

class BPlusTreeNode { public: int *keys; // Array of keys int t; // Minimum degree (defines the range for number of keys) BPlusTreeNode **C; // An array of child pointers int n; // Current number of keys bool leaf; // Is true when node is leaf. Otherwise false BPlusTreeNode(int _t, bool _leaf); // Constructor // A function to traverse all nodes in a subtree rooted with this node void traverse(); // A function to search a key in subtree rooted with this node. BPlusTreeNode *search(int k); // returns NULL if k is not present. // A function to traverse all nodes in a subtree rooted with this node void traverse(); // A function to search a key in subtree rooted with this node. BPlusTreeNode *search(int k); // returns NULL if k is not present. // A function that returns the index of the first key that is greater // or equal to k int findKey(int k); // A utility function to insert a new key in the subtree rooted with // this node. The assumption is, the node must be non-full when this // function is called void insertNonFull(int k); // A utility function to split the child y of this node. i is index of y in // child array C[]. The Child y must be full when this function is called void splitChild(int i, BPlusTreeNode *y); // Make BPlusTree friend of this so that we can access private members of // this class in BPlusTree functions friend class BPlusTree; };

Next, the B+ Tree class can be defined, which will contain functions for inserting and searching for keys in the tree. The B+ Tree class should also include a pointer to the root node of the tree and a function for creating a new root node if one does not already exist.

Example

Here is an example of how the B+ Tree class might be implemented in C++ −

class BPlusTree { BPlusTreeNode *root; // Pointer to root node int t; // Minimum degree public: // Constructor (Initializes tree as empty) BPlusTree(int _t) { root = NULL; t = _t; } // function to traverse the tree void traverse() { if (root != NULL) root->traverse(); } // function to search a key in this tree BPlusTreeNode* search(int k) { return (root == NULL) ? NULL : root->search(k); } // The main function that inserts a new key in this B+ tree void insert(int k); };

The insert function for the B+ Tree class will handle the creation of new nodes and the splitting of nodes when necessary to maintain the balance of the tree. Here is an example of

how the insert function might be implemented −

void BPlusTree::insert(int k) { // If tree is empty if (root == NULL) { // Allocate memory for root root = new BPlusTreeNode(t, true); root->keys[0] = k; // Insert key root->n = 1; // Update number of keys in root } else // If tree is not empty { // If root is full, then tree grows in height if (root->n == 2*t-1) { // Allocate memory for new root BPlusTreeNode *s = new BPlusTreeNode(t, false); // Make old root as child of new root s->C[0] = root; // Split the old root and move 1 key to the new root s->splitChild(0, root); // New root has two children now. Decide which of the // two children is going to have new key int i = 0; if (s->keys[0] < k) i++; s->C[i]->insertNonFull(k); // Change root root = s; } else // If root is not full, call insertNonFull for root root->insertNonFull(k); } }

Advantages of B+ Trees Over B Trees

One of the main advantages of B+ trees over B trees is that B+ trees have better space utilization. Because B+ trees use a pointer-based structure, each node is able to store more keys and use less space than a B tree node. This can be especially beneficial in large databases where space is at a premium.

Additionally, B+ trees have faster search times than B trees because they have a smaller height, thanks to their larger number of keys per node. This means that fewer nodes need to be traversed to find a specific key, which can significantly reduce search times in large databases.

Conclusion

In summary, B+ trees are a specialized type of balanced tree data structure that are used in databases to store and retrieve data efficiently. They offer faster search times and better space utilization compared to other types of balanced trees, making them a popular choice for use in database management systems.

Implementing a B+ tree in C++ involves defining a node class and a B+ Tree class, both of which contain functions for inserting and searching for keys in the tree. B+ trees have a number of advantages over B trees, including better space utilization and faster search times, making them a valuable tool for managing large databases.

Updated on: 16-Jan-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements