Data Structures · Visualization

AVL Tree: Search

AVL Tree

Searching an AVL tree works exactly like searching a regular BST — compare, then go left or right — but because the tree is always kept balanced, the search path is guaranteed to be short (O(log n)), no matter what order values were inserted or deleted. Time: O(log n) guaranteed. Space: O(log n) recursive, O(1) iterative.

Big-O: Every operation stays O(log n) thanks to rotations.

AVL Tree: Search

AVL Tree

Searching an AVL tree works exactly like searching a regular BST — compare, then go left or right — but because the tree is always kept balanced, the search path is guaranteed to be short (O(log n)), no matter what order values were inserted or deleted.

How Search Works

  1. Start at the root.
  2. If the current node is null, 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 — guaranteed to take at most about log2(n) steps.

Search Worked Example

Input: Search for 10 in balanced tree with root 20, left 10, right 30

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

Output: Found in 2 steps

Try your own input
AVL: Introduction
A self-balancing BST — after every insert or delete, it checks each node’s left/right height difference and rotates automatically if it exceeds 1.
500
300
200
400
700
600
800
Active Result Unbalanced
AVL: Introduction
Press ▶ Run to watch it step by step.
Python · AVL Trees in Code
def search(node, key):
    if not node or node.key == key:
        return node
    if key < node.key:
        return search(node.left, key)
    return search(node.right, key)   # O(log n), tree is balanced
# example: search 10
Input: Search for 10 in balanced tree with root 20, left 10, right 30

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, predictable lookups in performance-critical systems
  • Symbol tables in compilers and interpreters
  • Any dataset requiring reliable worst-case search speed

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.