Data Structures · Visualization
Red-Black Tree: Search
Red-Black Tree
Searching a red-black tree is identical to searching a plain BST — the color rules don't affect how you search, only how the tree is kept balanced. Time: O(log n) guaranteed. Space: O(log n) recursive, O(1) iterative.
Big-O: Operations O(log n); height ≤ 2·log₂(n+1).
Red-Black Tree: Search
Red-Black TreeSearching a red-black tree is identical to searching a plain BST — the color rules don't affect how you search, only how the tree is kept balanced. Because the color rules guarantee the tree's height stays within about 2x the ideal minimum, search remains reliably fast.
How Search Works
- Start at the root.
- If the current node is null (an empty black leaf), the value is not present.
- If it matches the target, return it.
- If the target is smaller, go left; if larger, go right.
- Repeat until found or an empty spot is reached.
Search Worked Example
Input: Search for 15 in a red-black tree with root 20, left child 15 (red)
- Compare 15 with root(20): 15 < 20, go left
- Compare 15 with node(15): match found!
Output: Found
Press ▶ Run to watch it step by step.
def search(node, key):
while node and node.key != key:
if key < node.key: node = node.left
else: node = node.right
return node # O(log n) — red-black height <= 2*log2(n+1)
# example: search 15Behind 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
- Fast lookups inside standard library map/set implementations
- Any lookup-heavy workload built on a red-black-tree-backed structure
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.