
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
How to create a B-Tree in DBMS?
A B-tree is a self-balancing tree in data structures that allows efficient storage of sorted data. Each node can hold multiple keys and have many child nodes. B-trees are versatile data structures that can efficiently handle large amounts of data.
However, traditional binary search trees become inefficient for storing and searching large datasets due to their lower performance and high memory usage. B-trees, balanced trees, are self-balancing trees designed to overcome these limitations.
B-trees are characterized by the large number of keys that can be stored in a single node, often called "large key" trees. Each node in a B-Tree can contain multiple keys, allowing the tree to have larger branching factors and a reduced height. This reduced height leads to fewer desk accesses, resulting in more efficient insertion operations.
Problem
Create a B-Tree of order 4 for the following set of key values ?
1,12,8,2,25,6,14,28,17,7,52,16,48,68,3
Solution
Let us start constructing B-Tree step by step ?
Step 1 ? Given an order of 4, the first four items are placed into the root and arranged in ascending order.
Step 2 ? To insert the fifth element, we must avoid placing it in the root, as this would violate the condition due to the order being 4.
Step 3 ? When 25 arrives, select the middle key to create a new root.
Step 4 ? Now 6, 14, 28 are added to the leaf nodes ?
Step 5 ? Adding 17 to the right of the leaf node would cause it to overflow. Therefore, we take the middle key, promote it to the root, and split the leaf node.
So split the leaf node and promote 17 to the root node.
Step 6 ? 7,52,16,48 get added to the leaf nodes as shown below ?
Step 7 ? Adding 68 results in splitting the rightmost leaf and promoting 48 to the root.
Step 8 ? Adding 3 causes the leftmost leaf to split, as inserting it would exceed the order of 4. Since 3 becomes the middle element, promote it to the root node by splitting the leftmost leaf.
Note: When attempting to insert a new key into a leaf, consider the following two points ?
-
If inserting a new key causes the leaf to become too large, split the leaf into two and promote the middle key to the parent node.
-
If the parent node becomes too large after inserting the key, split it into two and promote the middle key.
Representation of B-Tree in C
B-Tree is a self-balancing ordered structured data that stores the data in a set of pages and also allows searching, insertion, and deletion operations. A B-Tree is organized as a balanced tree with the following properties ?
-
Every node in the data structure can contain multiple keys, along with pointers to their child nodes.
-
Balancing a node is achieved by limiting the minimum and maximum number of keys it can contain.
-
Each leaf is compared to the next node to determine if any queue can be eliminated.
Example
This code creates a binary tree and performs an in-order traversal. It prints the values of the nodes in the order: left child, root, and right child.
#include <stdio.h> #include <stdlib.h> struct TreeNode { int value; struct TreeNode* leftChild; struct TreeNode* rightChild; }; struct TreeNode* createTreeNode(int value) { struct TreeNode* newTreeNode = (struct TreeNode*)malloc(sizeof(struct TreeNode)); newTreeNode->value = value; newTreeNode->leftChild = NULL; newTreeNode->rightChild = NULL; return newTreeNode; } void inorderTraversal(struct TreeNode* root) { if (!root) return; inorderTraversal(root->leftChild); printf("%d ", root->value); inorderTraversal(root->rightChild); } int main() { struct TreeNode* root = createTreeNode(10); root->leftChild = createTreeNode(20); root->rightChild = createTreeNode(30); root->leftChild->leftChild = createTreeNode(40); root->leftChild->rightChild = createTreeNode(50); printf("Inorder Traversal: "); inorderTraversal(root); return 0; }
The result is generated as follows ?
Inorder Traversal: 40 20 50 10 30