A B-tree is a self-balanced tree data structure that will maintain the sorted data and allow for operations such as insertion, deletion and search operations. B-tree is particularly well-suited for systems that need to perform disk-based operations and it minimizes the number of disk accesses required for searching the data and updating the data. These trees are efficient for large-scale storage systems due to their balanced structure which minimizes disk-accesses and their ability to handle the variable-sized keys.
They are widely used in databases, filesystems and other storage-related applications. It is commonly used in databases and file systems due to its ability to handle large amounts of data and maintain balanced trees with relatively shallow heights.
Organization of B-Tree:
A B-tree is a balanced tree data structure where each node have contains multiple keys and multiple child pointers. The organization of the B-tree is designed to optimize the search, insertion and deletion operation while maintaining balance.
Representation of B-Tree:
Explanation of the Image:
- The root node contains keys [15,30].
- Every internal node contains the keys for routing and pointers to the child nodes.
- Leaf nodes contain the actual data and do not have any children.
- Each leaf node can have up to the 2t-1 keys.
- Internal nodes can have up to the 2t-1 keys and 2t children.
Implementation of B-Tree:
Java
// Java Program for Implementaion B-Tree
class BTreeNode {
// Variables Declared
int[] keys; // Array to store keys
int t; // Minimum degree (defines the range for number of keys)
BTreeNode[] children; // Array to store child pointers
int n; // Current number of keys
boolean leaf; // True when node is leaf, else False
public BTreeNode(int t, boolean leaf) {
this.t = t;
this.leaf = leaf;
keys = new int[2 * t - 1];
children = new BTreeNode[2 * t];
n = 0;
}
// Function to search given key in subtree
// rooted with this node
public BTreeNode search(int key) {
int i = 0;
while (i < n && key > keys[i])
i++;
if (keys[i] == key)
return this;
if (leaf)
return null;
return children[i].search(key);
}
// Function to insert a new key
// in subtree rooted with this node
public void insertNonFull(int key) {
int i = n - 1;
if (leaf) {
while (i >= 0 && key < keys[i]) {
keys[i + 1] = keys[i];
i--;
}
keys[i + 1] = key;
n++;
} else {
while (i >= 0 && key < keys[i])
i--;
i++;
if (children[i].n == 2 * t - 1) {
splitChild(i, children[i]);
if (key > keys[i])
i++;
}
children[i].insertNonFull(key);
}
}
// Function to split the child node
public void splitChild(int i, BTreeNode y) {
BTreeNode z = new BTreeNode(y.t, y.leaf);
z.n = t - 1;
for (int j = 0; j < t - 1; j++)
z.keys[j] = y.keys[j + t];
if (!y.leaf) {
for (int j = 0; j < t; j++)
z.children[j] = y.children[j + t];
}
y.n = t - 1;
for (int j = n; j >= i + 1; j--)
children[j + 1] = children[j];
children[i + 1] = z;
for (int j = n - 1; j >= i; j--)
keys[j + 1] = keys[j];
keys[i] = y.keys[t - 1];
n++;
}
// Function to print all keys in the
// subtree rooted with this node
public void printInOrder() {
int i;
for (i = 0; i < n; i++) {
if (!leaf)
children[i].printInOrder();
System.out.print(keys[i] + " ");
}
if (!leaf)
children[i].printInOrder();
}
}
public class BTree {
// Pointer to root node
private BTreeNode root;
// Minimum degree
private int t;
public BTree(int t) {
this.t = t;
root = null;
}
// Function to search a key in this tree
public BTreeNode search(int key) {
return (root == null) ? null : root.search(key);
}
// Function to insert a key into the B-tree
public void insert(int key) {
if (root == null) {
root = new BTreeNode(t, true);
root.keys[0] = key;
root.n = 1;
} else {
if (root.n == 2 * t - 1) {
BTreeNode newRoot = new BTreeNode(t, false);
newRoot.children[0] = root;
newRoot.splitChild(0, root);
int i = 0;
if (newRoot.keys[0] < key)
i++;
newRoot.children[i].insertNonFull(key);
root = newRoot;
} else {
root.insertNonFull(key);
}
}
}
// Function to print the entire B-tree
public void printBTree() {
if (root != null)
root.printInOrder();
System.out.println();
}
public static void main(String[] args) {
// Create a B-tree with minimum degree 3
BTree bTree = new BTree(3);
bTree.insert(10);
bTree.insert(20);
bTree.insert(5);
bTree.insert(6);
bTree.insert(12);
bTree.insert(30);
System.out.print("B-tree : ");
bTree.printBTree();
int searchKey = 6;
BTreeNode foundNode = bTree.search(searchKey);
if (foundNode != null)
System.out.println("Key " + searchKey + " found in the B-tree.");
else
System.out.println("Key " + searchKey + " not found in the B-tree.");
}
}
OutputB-tree : 5 6 10 12 20 30
Key 6 found in the B-tree.
Complexity of the Above Method:
Operation
| Explanation
| Complexity
|
---|
Search Operation
| Searching for the key in a B-tree involves traversing the tree from the root node to the leaf node, following the child pointers based on a comparison of the keys.
| - The time complexity of the searching operation is O(log n).
- The space complexity of the searching operation is O(1) for iterative, and O(log n) for recursive search.
|
---|
Insertion Operation
| Inserting a key into the B-tree involves finding an appropriate leaf node for the insertion and maintaining B-tree properties by splitting nodes if necessary.
| - The time complexity of insertion operation is O(log n).
- The space complexity of the insertion operation is O(1og n).
|
---|
Deletion Operation
| Deleting a key from the B-tree involves the key, removing it from the appropriate leaf node and ensuring the B-tree remains balanced by merging or redistributing the nodes if necessary.
| - The time complexity of the deletion operation is O(log n).
- The space complexity of the deletion operation is O(log n).
|
---|
Traversal Operation
| Traversing the B-tree involves visiting all the keys in the tree in a specific order like in-order, pre-order or post-order traversals.
| - The time complexity of traversal operations is O(n).
- The space complexity of the traversal operation is O(t).
|
---|
Range Queries
| Range queries involve searching for the keys within the given range in the B-tree.
| - The time complexity of range queries is O(k + log n).
- The space complexity of range queries is O(log n).
|
---|
Application of B-tree Data Structure
B-trees are used in different applications especially those that require the efficient storage, retrieval and management of large datasets. Here are some applications of the B-tree data structure:
- Database Systems
- File Systems
- Data Compression
- Multilevel Indexing
- Database of the Geospatial Information
- Information Retrieval Systems
- Caching
- Key-Value Stores
- Filesystem Journaling
- Operating Systems
Similar Reads
TreeMap in Java TreeMap is a part of the Java Collection Framework. It implements the Map and NavigableMap interface and extends the AbstractMap class. It stores key-value pairs in a sorted order based on the natural ordering of keys or a custom Comparator. It uses a Red-Black Tree for efficient operations (add, re
11 min read
TreeSet in Java TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree(red - black tree) for storage. The ordering of the elements is maintained by a set using their natural ordering whether or not an explicit comparator is provided. This must be consistent with equ
13 min read
TreeSet in Java TreeSet provides an implementation of the SortedSet Interface and SortedSet extends Set Interface. It behaves like simple set with the exception that it stores elements in sorted format. Following are the features of TreeSet. TreeSet uses tree data structure for storage.Objects are stored in sorted,
3 min read
Java JTree The JTree is a type of GUI(Graphic User Interface) that displays information in a hierarchical way. This intricate component part provides a quite elegant substance of representing relationships among elements in a tree-like structure. In this exploration, we'll delve into the essence of the JTree c
2 min read
Segment Tree in Java A Segment Tree is a binary tree used for storing intervals or segments. It is allowed to query sum, minimum, maximum or other associative operations over the range of elements in the array efficiently. Each node in the segment tree represents the interval of an array. Leaves of the segment tree corr
5 min read
AVL Tree program in Java AVL tree stands for Adelson-Velsky and Landis tree. An AVL tree is a type of self-balancing binary search tree. In an AVL tree, the height of two child subtrees of any of the nodes differs by no more than one, ensuring that the tree remains balanced. This property helps in maintaining the tree's hei
5 min read
B Tree in Python A B-tree is a self-balancing tree data structure that maintains sorted data and allows searches, sequential access, insertions, and deletions in logarithmic time. B Tree in Python Table of Content Characteristics of B-TreesTraversal of B-Tree in PythonSearch operation in B Tree in PythonInsert opera
14 min read
B+ Tree in Python In computer science, data structures are crucial in efficiently managing and organizing data. Among these, the B+ tree is a powerful and important data structure, widely used in databases and file systems. In this article, we will discuss the concept of B+ trees, exploring their structure, operation
12 min read
Red Black Tree Java A Red-Black Tree is a self-balancing binary search tree where each node contains an extra bit for the storing colour either red or black. This structure ensures that the tree remains balanced during the insertions and deletion operation, so maintains an O(log n) time complexity for the basic operati
6 min read
TreeSet add() Method in Java The Java.util.TreeSet.add() method in Java TreeSet is used to add a specific element into a TreeSet. The function adds the element only if the specified element is not already present in the set else the function return False if the element is not present in the TreeSet. Syntax: Tree_Set.add(Object
1 min read