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.