Java Program to Implement B+ Tree
Last Updated :
13 Jun, 2024
The B+ tree is a self-balancing tree data structure commonly used in database and file systems applications. It is an extension of B-Tree and maintains sorted data in a manner that allows us for efficient insertion, deletion and search operations. The B+Trees stores all data in a leaf node while internal nodes only store keys that act as separators.
Organization of the B+ Tree
- All leaf nodes are at the same level
- Internal nodes contain keys that direct the search to the appropriate child node
- Leaf nodes contain actual data and are linked together sequentially for easier traversal
- Each node has a defined minimum and maximum number of children or keys, ensuring balance
[20, 40]
/ | \
[10, 15] [25, 30] [45, 50]
Here, the root node has two keys (20, 40), guiding where to look. The leaves contain the actual data and are linked, allowing in-order traversal.
Implementation of B+ Tree
Implementing a B+ Tree in Java involves creating classes for nodes and the tree itself. The node class would contain references to child nodes and, in the case of leaves, links to the next leaf for traversal.
- Create a class B+Tree Node to represent a node in the tree
- Create a class B+Tree to represent the B+ Tree itself, handling insertions, deletions, and searches.
- Implement insertion logic such that when a node overflows it splits
- Implement deletion logic such that when a node underflows, it borrows from siblings or merges nodes
Basic Operations in a B+ Tree:
Operation
| Description
| Time Complexity
|
---|
Insertion
| Find the appropriate leaf node, insert the data, and manage any required splits
| O(log n)
|
---|
Deletion
| Find the node with the data, remove it, and re-balance if necessary
| O(log n)
|
---|
Search
| Traverse internal nodes based on keys until reaching the appropriate leaf node, then search within the leaf.
| O(log n)
|
---|
Java Program to Implement B+ Tree
Here is a simplified Java implementation of a B+ Tree, focusing on insertions, searches, and basic re-balancing:
Java
// Java Program to Implement B+ Tree
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
// B+ Tree Node class to represent
// internal and leaf nodes
class BPlusTreeNode {
// True for leaf nodes, False for internal nodes
boolean isLeaf;
// The keys stored in this node
List<Integer> keys;
// Children nodes (for internal nodes)
List<BPlusTreeNode> children;
// Link to the next leaf node
BPlusTreeNode next;
// Constructor to initialize a node
public BPlusTreeNode(boolean isLeaf) {
this.isLeaf = isLeaf;
this.keys = new ArrayList<>();
this.children = new ArrayList<>();
this.next = null;
}
}
// B+ Tree class with basic operations: insert and search
class BPlusTree {
// Root node of the tree
private BPlusTreeNode root;
// Maximum number of keys per node
private final int order;
// Constructor to initialize the B+ Tree
public BPlusTree(int order) {
if (order < 3) {
throw new IllegalArgumentException("Order must be at least 3");
}
this.root = new BPlusTreeNode(true);
this.order = order;
}
// Find the appropriate leaf node for insertion
private BPlusTreeNode findLeaf(int key) {
BPlusTreeNode node = root;
while (!node.isLeaf) {
int i = 0;
while (i < node.keys.size() && key >= node.keys.get(i)) {
i++;
}
node = node.children.get(i);
}
return node;
}
// Insert a key into the B+ Tree
public void insert(int key) {
BPlusTreeNode leaf = findLeaf(key);
insertIntoLeaf(leaf, key);
// Split the leaf node if it exceeds the order
if (leaf.keys.size() > order - 1) {
splitLeaf(leaf);
}
}
// Insert into the leaf node
private void insertIntoLeaf(BPlusTreeNode leaf, int key) {
int pos = Collections.binarySearch(leaf.keys, key);
if (pos < 0) {
pos = -(pos + 1);
}
leaf.keys.add(pos, key);
}
// Split a leaf node and update parent nodes
private void splitLeaf(BPlusTreeNode leaf) {
int mid = (order + 1) / 2;
BPlusTreeNode newLeaf = new BPlusTreeNode(true);
// Move half the keys to the new leaf node
newLeaf.keys.addAll(leaf.keys.subList(mid, leaf.keys.size()));
leaf.keys.subList(mid, leaf.keys.size()).clear();
newLeaf.next = leaf.next;
leaf.next = newLeaf;
// If the root splits, create a new root
if (leaf == root) {
BPlusTreeNode newRoot = new BPlusTreeNode(false);
newRoot.keys.add(newLeaf.keys.get(0));
newRoot.children.add(leaf);
newRoot.children.add(newLeaf);
root = newRoot;
} else {
insertIntoParent(leaf, newLeaf, newLeaf.keys.get(0));
}
}
// Insert into the parent node after a leaf split
private void insertIntoParent(BPlusTreeNode left, BPlusTreeNode right, int key) {
BPlusTreeNode parent = findParent(root, left);
if (parent == null) {
throw new RuntimeException("Parent node not found for insertion");
}
int pos = Collections.binarySearch(parent.keys, key);
if (pos < 0) {
pos = -(pos + 1);
}
parent.keys.add(pos, key);
parent.children.add(pos + 1, right);
// Split the internal node if it exceeds the order
if (parent.keys.size() > order - 1) {
splitInternal(parent);
}
}
// Split an internal node
private void splitInternal(BPlusTreeNode internal) {
int mid = (order + 1) / 2;
BPlusTreeNode newInternal = new BPlusTreeNode(false);
// Move half the keys to the new internal node
newInternal.keys.addAll(internal.keys.subList(mid + 1, internal.keys.size()));
internal.keys.subList(mid, internal.keys.size()).clear();
// Move half the children to the new internal node
newInternal.children.addAll(internal.children.subList(mid + 1, internal.children.size()));
internal.children.subList(mid + 1, internal.children.size()).clear();
// If the root splits, create a new root
if (internal == root) {
BPlusTreeNode newRoot = new BPlusTreeNode(false);
newRoot.keys.add(internal.keys.get(mid));
newRoot.children.add(internal);
newRoot.children.add(newInternal);
root = newRoot;
} else {
insertIntoParent(internal, newInternal, internal.keys.remove(mid));
}
}
// Find the parent node of a given node
private BPlusTreeNode findParent(BPlusTreeNode current, BPlusTreeNode target) {
if (current.isLeaf || current.children.isEmpty()) {
return null;
}
for (int i = 0; i < current.children.size(); i++) {
BPlusTreeNode child = current.children.get(i);
if (child == target) {
// Parent found
return current;
}
BPlusTreeNode possibleParent = findParent(child, target);
if (possibleParent != null) {
return possibleParent;
}
}
// Parent not found
return null;
}
// Search for a key in the B+ Tree
public boolean search(int key) {
BPlusTreeNode node = findLeaf(key);
int pos = Collections.binarySearch(node.keys, key);
return pos >= 0;
}
// Display the Tree (for debugging purposes)
public void printTree() {
printNode(root, 0);
}
private void printNode(BPlusTreeNode node, int level) {
System.out.println("Level " + level + ": " + node.keys);
if (!node.isLeaf) {
for (BPlusTreeNode child : node.children) {
printNode(child, level + 1);
}
}
}
}
// Main class with example usage
public class Main {
public static void main(String[] args) {
BPlusTree tree = new BPlusTree(3);
// Insert keys into the B+ Tree
tree.insert(10);
tree.insert(20);
tree.insert(30);
tree.insert(40);
tree.insert(50);
tree.insert(60);
// Print the B+ Tree structure
System.out.println("Tree after insertion:");
tree.printTree();
// Search for keys in the B+ Tree
System.out.println("Search for 30: " + tree.search(30)); // True
System.out.println("Search for 25: " + tree.search(25)); // False
}
}
OutputTree after insertion:
Level 0: [30, 50]
Level 1: [10, 20]
Level 1: [30, 40]
Level 1: [50, 60]
Search for 30: true
Search for 25: false
Below is the explanation of the above program:
- Provides a basic structure for B+ Tree with insertion and search operations
- Handles leaf and internal splits with re-balancing logic
- Includes a print Tree method to visualize the tree structure
Applications of B+ Tree
- B+ Trees are widely used in applications that require efficient indexing and fast retrieval, including
- Database indexing for relational databases
- File system structures where efficient search, insertion, and deletion are critical
- Applications requiring ordered data with range queries, like in-memory databases or caches
Similar Reads
C++ Program to Implement B+ Tree
A B+ Tree is an advanced data structure that extends the B-tree by adding a linked list of leaf nodes. This structure is widely used in databases and file systems to allow efficient insertion, deletion, and search operations. In this article, we will learn how to implement the B+ Tree in C++ along w
10 min read
C++ Program to Implement AVL Tree
AVL Tree, named after its inventors Adelson-Velsky and Landis, is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one, which ensures that the tree remains approximately balanced, providing efficient search, insertion, and delet
11 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
Java Program to Implement Stack Data Structure
Stack is the fundamental Data Structure that can follow the Last In, First Out(LIFO) principle. It can work that the last element added to the stack will be the first one to be removed. It can operate like a stack of plates: We can only add or remove the topmost plate at any given time. The simplici
5 min read
C Program to Implement Binary Heap
Binary heaps are the most fundamental data structures which are used in various algorithms. They are mostly used in implementing priority queues and also in heapsort. They are tree-based data structures that satisfy heap order property. In this article, we will study the implementation of Binary hea
6 min read
Implementation of B+ Tree in C
A B+ tree is a self-balancing tree data structure that maintains sorted data and allows searching, inserting, and deletion of data in logarithmic time. In B+ trees, the actual data is stored inside the leaf nodes and the internal nodes act as pointers to the leaf nodes. In this article, we will lear
15+ min read
B-Tree Implementation in C++
In C++, B-trees are balanced tree data structures that maintain sorted data and allow searches, sequential access, insertions, and deletions in logarithmic time. B-trees are generalizations of binary search trees (BST) in that a node can have more than two children. B-trees are optimized for systems
13 min read
Implementation of B-Tree in C
The B-tree is a self-balancing ordered structured data that stores data in a set of pages and also allows efficient searching, insertion, and deletion operations. It has its origin in a generic form of binary search trees developed by Rudolf Bayer and Edward M. McCreight in 1972. It is capable of pr
5 min read
C++ Program to Implement Binomial Heap
A binomial heap is a collection of the binomial trees that are linked together. Each binomial tree in the heap has a node structure where the key of a node is greater than or equal to the key of its parent. It is an extension of Binary Heap that allows us to perform union or merge operation faster m
9 min read
C Program to Implement Binomial Heap
Binomial Heap is a specific type of heap data structure that finds its roots in the Binomial Tree. Itâs a collection of binomial trees, each adhering to the min-heap property, where the parent nodeâs key is less than or equal to its childrenâs keys. This structure is particularly efficient for imple
4 min read