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-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.

How B-Tree: Introduction Works

  1. Choose an order (minimum degree) t; each node holds between t-1 and 2t-1 keys (except the root).
  2. Keys within a node are kept sorted; children between two keys hold values strictly between them.
  3. All leaves sit at the same depth, keeping the tree perfectly height-balanced.
  4. 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]

  1. Root splits when it would exceed 2t-1=3 keys: root becomes [20,40]
  2. Left child holds [10], middle child holds [30], right child holds [50]
  3. 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

📖 Introduction: this is an overview of the topic, so there's nothing to run here. Pick an operation from the side menu to see a live, step-by-step animation.
B-Tree: Introduction
A multi-way self-balancing search tree: nodes hold several sorted keys and branch many ways, so height stays tiny — the basis of database indexes.
30
child 0
1020
child 1
4050
Node being scanned Inserted / promoted key Key
B-Tree: Introduction
Press ▶ Run to watch it step by step.
Python · B-Trees in Code
# 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

TimeSearch/Insert/Delete: O(log n)
SpaceO(n)

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

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.