Data Structures · Visualization

B+ Tree: Search & Range Scan

B+ Tree

Point search follows signposts down a single path. Time: O(log n) point; O(log n + k) range. Space: O(1) extra.

Big-O: Point ops O(log n); range scan O(log n + k).

B+ Tree: Search & Range Scan

B+ Tree

Point search follows signposts down a single path. Range search does the same descent once for the start key, then strolls the leaf chain collecting keys until the end bound — k results cost one descent plus k steps, which is why databases love this layout for BETWEEN queries.

How Search & Range Scan Works

  1. Descend from the root to the leaf holding the start key.
  2. If the key is there, report it (point search done).
  3. For ranges: keep walking right along the leaf links.
  4. Stop at the first key past the end bound.

Search & Range Scan Worked Example

Input: Range 20–35 over [10] → [20] → [25, 30] → [40]

  1. Descend to 20 (2 hops).
  2. Walk chain: 20 ✓, 25 ✓, 30 ✓.
  3. 40 > 35 → stop.
  4. Total: 1 descent + 3 collected, root touched once.

Output: Range [20, 35] → 20, 25, 30

Try your own input
B+ Tree: Introduction
Signposts inside, values in chained leaves — the database index layout.
root signposts: [30]
10
20
leaf 0
30
40
leaf 1
Active leaf path Found key
B+ Tree: Introduction
Press ▶ Run to watch it step by step.
Python · B+ Tree in Code
def search(root, key):
    leaf = descend(root, key)  # one root-to-leaf path
    return key in leaf.keys    # O(log n)

def range_scan(root, lo, hi):
    leaf = descend(root, lo)
    while leaf and leaf.keys[0] <= hi:
        yield from [k for k in leaf.keys if lo <= k <= hi]
        leaf = leaf.next       # stroll the chain
# example: search 20 on [10]
Input: Range 20–35 over [10] → [20] → [25, 30] → [40]

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 & Range Scan Time & Space Complexity

TimeO(log n) point; O(log n + k) range
SpaceO(1) extra

When to Use Search & Range Scan

  • SQL BETWEEN and ORDER BY … LIMIT over indexed columns
  • Log viewers paging through timestamp windows

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.