B+ tree index structure in DBMS Theory - Time & Space Complexity
We want to understand how the time to find or insert data in a B+ tree changes as the data grows.
How does the number of steps grow when the tree holds more records?
Analyze the time complexity of searching for a key in a B+ tree index.
-- Pseudocode for searching a key in a B+ tree
function searchBPlusTree(root, key) {
node = root
while node is not leaf {
find child pointer in node for key
node = child pointer
}
search leaf node for key
return result
}
This code finds a key by moving down from the root to the leaf level, choosing the right child at each step.
Look for repeated steps in the search process.
- Primary operation: Moving down one level in the tree and searching keys in that node.
- How many times: Once per tree level, from root to leaf.
As the number of records grows, the tree grows taller slowly because each node holds many keys.
| Input Size (n) | Approx. Operations (levels) |
|---|---|
| 10 | 2 |
| 100 | 3 |
| 1000 | 4 |
Pattern observation: The number of steps grows very slowly, roughly adding one step when the data grows by a large factor.
Time Complexity: O(log n)
This means the time to search grows slowly, increasing only a little even if the data grows a lot.
[X] Wrong: "Searching a B+ tree takes time proportional to the number of records because it checks every record."
[OK] Correct: The B+ tree groups many keys in each node, so it jumps down levels instead of checking all records one by one.
Knowing how B+ trees keep search times low helps you explain why databases are fast even with lots of data. This skill shows you understand how indexes work under the hood.
"What if each node could hold only one key instead of many? How would the time complexity change?"