Interview: B-Tree vs B+ Tree

Interview: B-Tree vs B+ Tree

Understanding B Trees vs. B+ Trees

Introduction to B Trees and B+ Trees

  • The discussion begins with the difference between a B tree and a B plus tree, using nine users as examples: Ava, Ben, Leo, Maya, Nina, Omar, Raj, Sarah, and Tom.

Structure of a B Tree

  • In a B tree, every node contains actual names along with their corresponding row numbers; even the root node holds data.
  • The top box in the B tree contains Leo (row 2) and Raj (row 8), while names are organized based on their alphabetical order into three blocks.

Structure of a B+ Tree

  • In contrast, the top box of a B plus tree does not store any names or row numbers; it only contains signposts (M and R).
  • All user names with their row numbers are stored in the bottom blocks of the B plus tree which are linked together like a chain.

Searching for Data

  • When searching for Leo in both trees:
  • In the B tree, Leo is found directly in one step at the top box.
  • In the B plus tree, it requires two steps since Leo is located in the first block after following signposts.

Performance Comparison

  • Although initially it seems that searching in a B tree is faster due to direct access to data at higher levels, this advantage diminishes because most data resides in lower blocks.
  • For retrieving ranges of data (e.g., from Ben to Maya), the efficiency of traversal differs significantly:
  • The database can move sideways through linked blocks in a B plus tree.
  • Conversely, accessing data across different levels in a B tree involves more upward and downward movements.

Advantages of Using a B+ Tree

  • A key advantage of the B plus tree is that its nodes only contain signposts rather than actual data. This allows each node to point to more blocks.
  • Consequently, this structure keeps trees shorter and reduces disk reads during searches.

Conclusion on Database Indexing

  • It’s noted that when databases like Postgres or MySQL refer to using a "B-tree index," they essentially implement what functions similarly to a B+ tree.

Turn any video into a summary like this

YouTube links, meetings, lectures. With transcripts, search, and chat.

Video description

In a standard B-tree, keys and row pointers are stored at every level, including the root and middle boxes. While this occasionally allows a lookup to stop early at the top, almost all records in massive databases sit at the bottom anyway. A B+ tree optimizes this architecture: upper nodes hold only navigation signposts to direct traffic, while all actual records and row pointers are pushed strictly down into the leaf nodes at the bottom. The true superpower of the B+ tree lies in range queries and disk efficiency. Because its bottom leaves are linked together in a continuous chain, reading a range of values means dropping down once and simply sliding sideways across the blocks, whereas a B-tree forces the database to hop repeatedly up and down between different levels. Furthermore, because upper nodes store only lightweight signposts without payload pointers, each block fits far more keys. This keeps the tree much flatter, requiring fewer disk reads per lookup. That is why when engines like PostgreSQL and MySQL offer a B-tree index, they are almost always implementing a B+ tree under the hood. #Database #SystemDesign #SoftwareEngineering #DataStructures #SQL #CodingForBeginners