We're sorry this tutorial wasn't helpful. Please tell us what we can do better.
B+ Tree Data Structure
Last updated:
A B+ Tree is another version of the B tree data structure that allows for efficient insertion, deletion, storage, and search operations. In the B+ Tree, all the leaf nodes store the actual data (value), and the internal nodes are used only for indexing (like a table of contents).
All the leaf nodes of a B+ Tree are linked together, which provides find a range of values quickly.
Features of B+ Tree
Balanced Tree: A B+ tree is a Balanced Tree which means all the leaf nodes maintain the same level. If any insertion or deletion happen in the tree, it automatically adjusts itself to maintain a balanced structure.
Data Storage: All the leaf nodes of a B+ tree store the actual data (value).
Role of Internal Nodes: In the B+ Tree, all the internal nodes store only keys and addresses to child nodes.
Ordered: B+ Tree maintains the order of the keys in the tree, making it simple to perform range based queries and other operations that need sorted data.
Minimum Degree: The minimal degree (t) of a B+ tree is ⌈m/2⌉.
Disk-oriented: A B+ Tree is the best choice for disk-based storage systems because it provides efficient storage and retrieval of data from the disk.
Number of child nodes: In a B+ Tree, each internal node can have any number of child nodes. This reduces the height of the tree while improving the efficiency of searching and indexing operations.
In a B+ Tree, all the internal nodes can have any number of child nodes. This reduces the height of the tree and increases the efficiency of searching and indexing operations.
Frequently Asked Questions (FAQ)
Q1. What is a B+ tree?
A B+ tree is a variant of the B-tree in which all data records are stored in the leaf nodes, and the internal nodes hold only keys used to guide the search. The leaf nodes are linked together in sorted order.
Q2. Why are the leaf nodes of a B+ tree linked?
The links let you read a range of keys by finding the first key and then walking along the leaves, without going back up the tree. This makes queries such as "all orders from January to March" very fast.
Q3. Why do databases prefer B+ trees for indexes?
Internal nodes hold only keys, so more keys fit in each disk block, which makes the tree shorter. Range scans are fast because of the linked leaves, and every search takes the same number of steps because all data is at the leaf level. MySQL InnoDB and PostgreSQL indexes use B+ trees.
Q4. What is the time complexity of B+ tree operations?
Search, insert and delete are O(log n). A range query returning k results takes O(log n + k): one search to find the start, then a walk through k leaf entries.
Q5. How does a search work in a B+ tree?
Start at the root and follow the child whose key range contains the target, down to a leaf. The search always ends at a leaf, even if the key also appears in an internal node, because only leaves hold the data.
Q6. How does insertion work in a B+ tree?
Insert the key into the correct leaf. If the leaf overflows, split it into two leaves and copy the first key of the new right leaf up to the parent (in a B-tree the middle key moves up instead). Internal nodes that overflow are split in the usual way.
Q7. Write Java code to walk the linked leaves of a B+ tree for a range query.
RangeScan.java
import java.util.ArrayList;
import java.util.List;
class LeafNode {
int[] keys;
int keyCount;
LeafNode next; // link to the next leaf
}
class RangeScan {
// start is the leaf found by searching for 'from'
static List<Integer> range(LeafNode start, int from, int to) {
List<Integer> result = new ArrayList<>();
for (LeafNode leaf = start; leaf != null; leaf = leaf.next) {
for (int i = 0; i < leaf.keyCount; i++) {
int key = leaf.keys[i];
if (key > to) {
return result; // past the end of the range
}
if (key >= from) {
result.add(key);
}
}
}
return result;
}
}
Q8. Can a key appear twice in a B+ tree?
Yes, as a copy. A key used for routing can appear in an internal node and also in a leaf, where the real record lives. In a B-tree, each key appears only once.
Q9. What is the fan-out of a B+ tree, and why does it matter?
Fan-out is the number of children per internal node. Because internal nodes store only keys and child pointers, a node can have hundreds of children. With a fan-out of 100, a tree of height 3 can index about one million records, so a lookup needs only 3 or 4 disk reads.
Q10. What are the disadvantages of a B+ tree?
Every search must go down to a leaf, even if the key appears in an internal node.
Routing keys are duplicated, which uses some extra space.
Splits and merges make the implementation complex.