Data Structures · Visualization
B-Tree: Search
B-Tree
Searching a B-Tree scans each node's sorted keys to find either a match or the correct child range, descending level by level — far fewer levels than a binary tree because each node branches many ways. Time: O(log n). Space: O(1) iterative / O(log n) recursive.
Big-O: Search / insert / delete O(log n); each node holds many keys so the tree stays very shallow.
B-Tree: Search
B-TreeSearching a B-Tree scans each node's sorted keys to find either a match or the correct child range, descending level by level — far fewer levels than a binary tree because each node branches many ways.
How Search Works
- Start at the root node.
- Within the current node, scan keys left to right to find the target or the first key greater than it.
- If found, return the match.
- Otherwise follow the child pointer immediately before that key and repeat.
- If a leaf is reached without a match, the key is not present.
Search Worked Example
Input: Search for 45 in root [20,40] with right child [50,60]
- Root: 45 > 40, so go to the child right of 40
- Child [50,60]: 45 < 50, so it would be the child left of 50 — but this is a leaf, so 45 is absent
Output: 45 not found after 2 node visits
Press ▶ Run to watch it step by step.
def search(node, key, t=2):
i = 0
while i < len(node.keys) and key > node.keys[i]:
i += 1
if i < len(node.keys) and key == node.keys[i]:
return node, i # found inside this node
if not node.children:
return None # leaf, not present
return search(node.children[i], key)
# root search → 1 node visit per level: O(log_t n)
# example: search 45Behind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Search Time & Space Complexity
When to Use Search
- Range lookups in database query engines
- Filesystem path resolution
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.