Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Implement B Tree
In C++, a Binary tree is a generalization of the Binary Search Tree (BST). A B-tree can have more than two children. It is also known as a balanced tree data structure that maintains sorted data and allows searches, sequential access, insertions, and deletions in logarithmic time. It is optimized for a system that reads and writes large blocks of data.
Properties of B-tree
As we discussed in the introduction B-tree is self-balanced tree where a node can have multiple children. It maintains the balance by making sure that all leaf nodes are at the same level. Following is the properties of binary tree:
- In B-Tree every node have at most m children.
- In B-Tree every non-leaf node except the root node has at least [m/2] children.
- The root node contains at least two children.
- All non-leaf node with n children contains n-1 keys.
- All leaf nodes appear on the same level and are empty.
B-Tree in C++
A node structure with an array of keys and an array of child pointers can be used to create a binary tree. The number of keys in a node is always one less than the number of child pointers.
Following are the some common operation on Binary tree:
- Insert Node: Insert a new element into the tree takes O(logn) time complexity.
- Delete Node: Delete a given node from the tree O(logn) time complexity.
- Search: Searches for element in the binary-tree takes O(logn) time complexity.
- Traverse: Traverse the binary tree and print the elements, it takes O(n) time complexity.
- Split Child: Split a full-child node during insertion of a node, it takes O(1) time complexity.
- Merge: When we combine two under-full sibling nodes and a parent key into a single node, it takes O(logn) time complexity.
Algorithm for Insertion and Traversal in B-Tree
Following is the algorithm for insertion and traversal in B-Tree:
-
Initialization
- Create a BTree node structure with:
-
d[]: To store keys. -
child_ptr[]: To store child node pointers. -
l: Boolean to check if node is a leaf. -
n: Number of keys currently stored in the node.
-
- Create a BTree node structure with:
-
Create a Node (
init()function)- Allocate memory for a new node
- Set
l = trueandn = 0 - Initialize all child pointers to
NULL
-
Insert Operation (
insert()function)- If tree is empty:
- Initialize root node using
init() - Set
x = r(current node)
- Initialize root node using
- If tree is not empty:
- Start at root and move down to the correct leaf node
- Compare value with existing keys and follow the correct child pointer
- At the correct leaf node:
- Insert the new value into
d[] - Increase
n(number of keys) - Sort the array so keys remain ordered
- Insert the new value into
- If tree is empty:
-
Sort Operation (
sort()function)- Use simple bubble sort to arrange keys in ascending order within the node
-
Traverse Operation (
traverse()function)- Recursively visit each node in left-to-right order
- Print keys in sorted order by traversing child nodes first if not a leaf
C++ Implementation of B Tree
In the following example, we will construct a binary tree, in which we are inserting the node and traversing it into the sorted order, for other operations of the B-tree, visit here:
#include <iostream>
using namespace std;
struct BTree {
int * d; // Array to store key in the node
BTree ** child_ptr; // Array of child pointer
bool l;
// # key in the node
int n;
}* r = NULL, * np = NULL, * x = NULL;
// function to initialize a new binary tree node
BTree * init() {
np = new BTree;
np -> d = new int[6]; // Order 6
np -> child_ptr = new BTree * [7];
np -> l = true;
np -> n = 0;
for (int i = 0; i < 7; i++) {
np -> child_ptr[i] = NULL;
}
return np;
}
//function to traverse a binary tree and print
//new element in the sorted order
void traverse(BTree * p) {
if (!p) return;
for (int i = 0; i < p -> n; i++) {
if (!p -> l) traverse(p -> child_ptr[i]);
cout << " " << p -> d[i];
}
if (!p -> l) traverse(p -> child_ptr[p -> n]);
}
void sort(int * p, int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (p[i] > p[j]) swap(p[i], p[j]);
}
}
}
// function to insert a key into binary tree
void insert(int a) {
if (!r) {
r = init();
x = r;
} else {
x = r;
while (!x -> l) {
int i;
for (i = 0; i < x -> n; i++) {
if (a < x -> d[i]) break;
}
x = x -> child_ptr[i];
}
}
// insert key into the leaf node
x -> d[x -> n++] = a;
// sort the key within the node
sort(x -> d, x -> n);
}
int main() {
int values[] = {10, 20, 30, 40, 50};
for (int v: values) {
insert(v);
}
cout << "constructed B-tree:\n";
traverse(r);
return 0;
}
Following is the constructed binary tree:
constructed B-tree: 10 20 30 40 50