Skip to content
We areTOP 1%

What is the difference between a B tree and a B+ tree?

MediumConceptIndexing & Query Optimization
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews

Answer

Both are balanced search trees in which every node holds many keys, so the tree stays shallow and each node fits in one disk page. The difference is where the data is stored.

  • B tree: keys and their data pointers are stored in every node, internal and leaf, so a search can stop early at an internal node.
  • B+ tree: internal nodes hold only keys for routing; all data pointers (or the rows themselves) are in the leaves, and the leaves are linked in key order.
  • Because B+ tree internal nodes carry no data, more keys fit in each node, so the tree is shorter.
  • The linked leaves make range queries and ordered scans fast, which is why MySQL InnoDB, PostgreSQL, Oracle and SQL Server use B+ trees.

Example: Keys 10 to 50 in a small B+ tree:

               [30]
             /      \
    [10 | 20]  -->  [30 | 40 | 50]

For WHERE key BETWEEN 20 AND 40, the database finds 20 in the left leaf, then follows the leaf link to the right: 20, 30, 40. A B tree would have to move up and down the tree instead.

More in Indexing & Query Optimization

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account