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-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.

How Search Works

  1. Start at the root node.
  2. Within the current node, scan keys left to right to find the target or the first key greater than it.
  3. If found, return the match.
  4. Otherwise follow the child pointer immediately before that key and repeat.
  5. 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]

  1. Root: 45 > 40, so go to the child right of 40
  2. 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

Try your own input
SearchO(log n)
Scans a node’s sorted keys to find the value or the right child range, then descends one level and repeats.
2040
child 0
1015
child 1
2530
child 2
5060
Node being scanned Inserted / promoted key Key
Search
Press ▶ Run to watch it step by step.
Python · B-Trees in Code
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 45
Input: Search for 45 in root [20,40] with right child [50,60]

Behind 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

TimeO(log n)
SpaceO(1) iterative / O(log n) recursive

When to Use Search

  • Range lookups in database query engines
  • Filesystem path resolution

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.