B-trees for databases in Data Structures Theory - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using B-trees in databases, it is important to understand how the time to find, insert, or delete data grows as the database gets bigger.
We want to know how the number of steps changes when the amount of data increases.
Analyze the time complexity of searching for a key in a B-tree.
function searchBTree(node, key) {
if (node == null) return null;
let i = 0;
while (i < node.keys.length && key > node.keys[i]) {
i++;
}
if (i < node.keys.length && key === node.keys[i]) {
return node.values[i];
} else if (node.isLeaf) {
return null;
} else {
return searchBTree(node.children[i], key);
}
}
This code searches for a key by checking keys in nodes and moving down the tree until it finds the key or reaches a leaf.
- Primary operation: Comparing the key with keys in a node (loop inside each node).
- How many times: This happens once per tree level, moving down from root to leaf.
As the database grows, the B-tree grows taller slowly because each node holds many keys.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 2-3 node checks |
| 100 | About 3-4 node checks |
| 1000 | About 4-5 node checks |
Pattern observation: The number of steps grows slowly, roughly with the height of the tree, which increases very slowly as data grows.
Time Complexity: O(log n)
This means the time to find or insert data grows slowly, roughly proportional to the logarithm of the data size.
[X] Wrong: "Searching a B-tree takes time proportional to the number of keys because it checks all keys."
[OK] Correct: Each node holds many keys, but the search only checks a few keys per node and moves down the tree levels, so it does not check all keys in the whole tree.
Understanding B-tree time complexity shows you can reason about how databases keep searches fast even with lots of data, a useful skill in many real-world systems.
"What if each B-tree node could hold twice as many keys? How would the time complexity change?"
Practice
Solution
Step 1: Understand B-tree structure
B-trees organize data in a sorted and balanced tree structure.Step 2: Identify the purpose in databases
This structure allows fast searching, insertion, and deletion by minimizing tree height and disk reads.Final Answer:
To keep data sorted and balanced for fast searching and updating -> Option BQuick Check:
B-tree purpose = fast, balanced data access [OK]
- Confusing B-trees with simple lists
- Thinking B-trees encrypt data
- Assuming B-trees compress data
Solution
Step 1: Recall B-tree node structure
B-tree nodes hold multiple keys to reduce tree height.Step 2: Understand children count
Each node has one more child than the number of keys it holds.Final Answer:
Each node can contain multiple keys and multiple children -> Option AQuick Check:
Multiple keys and children per node = C [OK]
- Thinking nodes have only one key
- Assuming nodes have no children
- Confusing B-trees with binary trees
Solution
Step 1: Check node capacity for order 3 B-tree
Max keys per node = 2. Current keys are [10, 20]. Inserting 15 adds a third key.Step 2: Understand insertion rules
When a node exceeds max keys, it splits and promotes the middle key to the parent.Final Answer:
The node will split because it exceeds the max keys, promoting a key up -> Option CQuick Check:
Node over capacity causes split and promotion [OK]
- Thinking node can hold 3 keys without splitting
- Assuming duplicates are discarded here
- Believing tree becomes unbalanced without immediate fix
Solution
Step 1: Understand node splitting in B-trees
When a node splits, the middle key must be promoted to keep the tree balanced.Step 2: Identify the error impact
If the middle key is not promoted, the tree structure breaks and becomes unbalanced.Final Answer:
Not promoting the middle key to the parent node after splitting -> Option AQuick Check:
Missing promotion causes imbalance [OK]
- Skipping promotion step after split
- Thinking sorted insertion causes imbalance
- Confusing node structure rules
Solution
Step 1: Attempt to insert 12 into leaf node
The leaf node has max 3 keys [5, 10, 15]. Inserting 12 adds a 4th key, exceeding capacity.Step 2: Split the node and promote middle key
The node splits into two nodes, and the middle key (10) is promoted to the parent to maintain balance.Final Answer:
Insert 12 into the leaf node, then split the node and promote the middle key to the parent -> Option DQuick Check:
Insert, split, promote middle key = balanced B-tree [OK]
- Discarding keys when node is full
- Merging instead of splitting on insertion
- Temporarily increasing node capacity
