B-trees and B+ trees are both self-balancing, multi-way search trees designed for data stored in blocks, such as database pages or file-system blocks. They share the same balancing rules but differ in where the data lives and how the leaves are organised, which affects range queries and tree height. This page summarises each one and then compares them.
What is a B-tree Data Structure?
A B-tree is a balanced search tree whose nodes hold several sorted keys and have one more child than they have keys. All leaves are at the same depth. For a B-tree of order m, a node has at most m children (m − 1 keys), and every internal node except the root has at least ⌈m/2⌉ children.
B-trees were invented by Rudolf Bayer and Edward M. McCreight at Boeing Scientific Research Labs (published in 1972) to manage indexes for large files.
Key Characteristics of B-Tree Data Structure
Balanced: all leaves are at the same depth.
Many keys and children per node: the height is only O(logm n), so a lookup reads few disk blocks.
Data in every node: a key and its record can sit in an internal node or in a leaf.
Self-balancing updates: full nodes split and push their middle key up; underfull nodes borrow or merge.
What is a B+ tree Data Structure?
A B+ tree is a B-tree variant that stores all data records (or record pointers) only in its leaves and links the leaves together in key order. Internal nodes contain only keys used to route a search, like the index of a book.
Features of B+ Tree
- Balanced: all leaves are on the same level.
- Data only in leaves: every search ends at a leaf.
- Routing keys in internal nodes: a key may be repeated in an internal node and in a leaf.
- Linked leaves: fast range queries and ordered scans.
- High fan-out: internal nodes have room for many keys, so the tree is very short.
- Bounded node size: at most m children per internal node, and at least ⌈m/2⌉ except at the root.
Difference between B Tree and B+ Tree
| Characteristic | B Tree | B+ Tree |
|---|---|---|
| Where data is stored | In every node, internal and leaf. | Only in the leaf nodes; internal nodes hold routing keys. |
| Keys | Each key appears exactly once. | A key may appear twice: as a routing key in an internal node and again in a leaf. |
| Leaf links | Leaves are not linked. | Leaves are linked in key order (a linked list). |
| Search for one key | Can stop early if the key is in an internal node. | Always goes down to a leaf, but the tree is usually shorter. |
| Range queries / sequential scan | Possible with an inorder traversal that moves up and down the tree; slower. | One search to the first key, then a walk along the linked leaves; fast. |
| Fan-out and height | Lower fan-out because internal nodes also carry data; slightly taller. | Higher fan-out because internal nodes carry keys only; usually shorter. |
| Insertion and deletion | Data may have to be moved between internal and leaf nodes. | Changes to data happen only in leaves; splits copy a key up to the parent. |
| Typical uses | File systems and storage engines. | Database indexes (for example MySQL InnoDB, PostgreSQL) and multi-level file indexes. |
Both trees give O(log n) search, insertion and deletion. The B+ tree's linked leaves and higher fan-out are why it is the usual choice for database indexes.