Data Structures · Visualization
B-Tree: Introduction
B-Tree
A B-Tree is a self-balancing multi-way search tree where each node holds multiple sorted keys and up to (order) children, keeping the tree shallow and minimizing disk/page reads — the standard index structure for databases and filesystems. Time: Search/Insert/Delete: O(log n). Space: O(n).
Big-O: Search / insert / delete O(log n); each node holds many keys so the tree stays very shallow.
B-Tree: Introduction
B-TreeA B-Tree is a self-balancing multi-way search tree where each node holds multiple sorted keys and up to (order) children, keeping the tree shallow and minimizing disk/page reads — the standard index structure for databases and filesystems.
How B-Tree: Introduction Works
- Choose an order (minimum degree) t; each node holds between t-1 and 2t-1 keys (except the root).
- Keys within a node are kept sorted; children between two keys hold values strictly between them.
- All leaves sit at the same depth, keeping the tree perfectly height-balanced.
- Search compares the target against a node's keys to pick the correct child range, recursing downward.
B-Tree: Introduction Worked Example
Input: B-Tree of order 3 (min degree t=2) containing [10,20,30,40,50]
- Root splits when it would exceed 2t-1=3 keys: root becomes [20,40]
- Left child holds [10], middle child holds [30], right child holds [50]
- Searching for 30: compare to root [20,40] → 20<30<40 → go to middle child → found
Output: A shallow 2-level tree holding 5 keys with at most 2 comparisons per level
Press ▶ Run to watch it step by step.
# B-Tree of order t=2 ("2-3-4 tree"): each node holds
# up to 3 keys and up to 4 children.
class BNode:
def __init__(self):
self.keys = []
self.children = []
def make_root(keys):
node = BNode()
node.keys = keys
return node
root = make_root([7]) # 1 key, 2 children max
# every leaf sits at the SAME depth — the tree stays flat
# example: view on [10, 20, 30, 40, 50]B-Tree: Introduction Time & Space Complexity
When to Use B-Tree: Introduction
- Database indexes (MySQL InnoDB, PostgreSQL)
- Filesystem structures (NTFS, HFS+)
- Any system needing minimal disk reads for large sorted data
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.