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+ TreePoint 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
- Descend from the root to the leaf holding the start key.
- If the key is there, report it (point search done).
- For ranges: keep walking right along the leaf links.
- Stop at the first key past the end bound.
Search & Range Scan Worked Example
Input: Range 20–35 over [10] → [20] → [25, 30] → [40]
- Descend to 20 (2 hops).
- Walk chain: 20 ✓, 25 ✓, 30 ✓.
- 40 > 35 → stop.
- Total: 1 descent + 3 collected, root touched once.
Output: Range [20, 35] → 20, 25, 30
Press ▶ Run to watch it step by step.
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]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
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
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.