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 a variant of the B-tree that is tuned for disk-based indexes. It makes two changes: all data records (or pointers to them) are stored only in the leaf nodes, and the leaves are linked together in key order. The internal nodes contain only keys that guide the search, much like the index at the front of a book.
Because every leaf points to the next one, a range of values can be read by finding the first key and then walking along the leaves, without climbing back up the tree.
Features of B+ Tree
Balanced: all leaves are on the same level. Insertions and deletions split, borrow or merge nodes so that this stays true.
Data only in the leaves: leaf nodes hold the keys together with the records (or record pointers); every search ends at a leaf.
Internal nodes are pure index: they hold keys and child pointers only. A key can therefore appear twice, once as a routing key in an internal node and once in a leaf.
Sorted and linked leaves: keys are kept in order and the leaves form a linked list, which makes range queries and full ordered scans fast.
Bounded node size: for a B+ tree of order m, an internal node has at most m children, and every internal node except the root has at least ⌈m/2⌉ children, so nodes are always at least about half full.
High fan-out: since internal nodes store no records, many keys fit in one disk page. A node can have hundreds of children, which keeps the tree only a few levels deep.
Disk-oriented: few levels mean few page reads per lookup, which is why most relational databases (for example MySQL InnoDB and PostgreSQL) use B+ trees for their indexes.
The high fan-out is the main reason for the B+ tree's speed: with 100 children per node, three levels already cover about a million keys.
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.