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 TreeSearching 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
- Start at the root.
- If the current node is null, the value is not present.
- If it matches the target, return it.
- If the target is smaller, go left; if larger, go right.
- 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
- Compare 10 with root(20): 10 < 20, go left
- Compare 10 with node(10): match found!
Output: Found in 2 steps
Press ▶ Run to watch it step by step.
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 10Behind 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, 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
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.