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 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. Because the color rules guarantee the tree's height stays within about 2x the ideal minimum, search remains reliably fast.

How Search Works

  1. Start at the root.
  2. If the current node is null (an empty black leaf), the value is not present.
  3. If it matches the target, return it.
  4. If the target is smaller, go left; if larger, go right.
  5. 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)

  1. Compare 15 with root(20): 15 < 20, go left
  2. Compare 15 with node(15): match found!

Output: Found

Try your own input
Red-Black: Introduction
A self-balancing BST that colors each node red or black and follows a few coloring rules — this keeps the tree "balanced enough" without needing exact height checks like AVL.
50
30
20
40
70
60
80
Red Black Active Result
Red-Black: Introduction
Press ▶ Run to watch it step by step.
Python · Red-Black Trees in Code
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 15
Input: Search for 15 in a red-black tree with root 20, left child 15 (red)

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) guaranteed
SpaceO(log n) recursive, O(1) iterative

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

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.