CSData StructuresDiagramIntermediate
B-Tree / B+Tree
KO: B-트리 (B-Tree / B+Tree)·EN: B-Tree / B+Tree·JA: B木(B-Tree / B+Tree)
A self-balancing multi-way search tree that packs hundreds of keys per page-sized node to keep tree height shallow and minimize disk I/O
Table of Contents
Structure Diagram
B-Tree / B+Tree Multi-Way Balanced Index PagesTap a node
ROOTRoot Page [30 | 70]
3 linksQuick Answer
What is B-Tree / B+Tree?
A self-balancing multi-way search tree that packs hundreds of keys per page-sized node to keep tree height shallow and minimize disk I/O. It is a core concept in Data Structures within CS.
Core Principles
A B-Tree generalizes a Binary Search Tree for block-oriented storage (SSDs, disks, and CPU cache lines) by storing hundreds of keys per node.
- Ultra-Shallow Height: Packing hundreds of pointers into a single 4KB–16KB page keeps tree height around 3–4 levels even for hundreds of millions of rows, limiting disk reads to a handful of page fetches.
- Balanced Splits & Merges: When a node overflows, it splits and promotes a median key upward, keeping all leaves at identical depth.
- B+Tree Variant: Stores payloads only in leaf nodes and links leaves sequentially, making range scans (
BETWEEN,ORDER BY) blazingly fast.
Practical Takeaways
- Avoid random UUIDv4 as clustered primary keys: Random inserts cause frequent page splits, low fill factor, and write amplification. Prefer monotonic IDs or time-ordered UUIDv7 / ULID.
- Leftmost prefix rule: A composite B+Tree index on
(a, b)is ordered byafirst; queries filtering only bybcannot use an index seek. - B+Tree vs LSM-Tree: B+Trees excel at reads and range queries; write-heavy engines (RocksDB, Cassandra) often use LSM-Trees to turn random writes into sequential appends.
Notable Examples
- Relational Databases: Default index engine in PostgreSQL, MySQL InnoDB, SQLite, and Oracle.
- Filesystems: Directory and extent indexing in NTFS, APFS, ext4, and Btrfs.
- Cache-friendly in-memory maps: Rust's
BTreeMapoutperforms Red-Black Trees due to CPU cache-line locality.