What is BST Searching?
Searching a Binary Search Tree is just insertion's walk without the final "attach a node" step — start at the root, compare the target against the current node, and let the BST property tell you exactly which way to go: smaller means left, larger means right. Either you land on the value, or you eventually fall off the tree (hit a null pointer), which means it isn't there.
How Does It Work?
- Search for 6 in a tree rooted at 8
- 8: 6 < 8 → go left
- 3: 6 > 3 → go right
- 6: 6 = 6 → found it!
If the value isn't in the tree, the same walk simply runs out of tree:
- Searching for a value that isn't present follows the same path logic
- The walk continues left/right based on comparisons
- Eventually it reaches a null pointer instead of a matching node
- That null pointer means the value isn't in the tree — search stops there
This is the entire reason a BST is useful in the first place — the sorted structure lets you eliminate an entire subtree with every comparison, the same way binary search eliminates half of a sorted array. That's what gets search down to O(log n) instead of the O(n) you'd need to scan an unsorted structure.
Algorithm Steps
- Start at the root
- Compare the target value with the current node:
- If equal, return the node — found
- If smaller, move to the left child
- If larger, move to the right child
- Repeat until you find a match or hit a null pointer
- A null pointer means the value isn't in the tree
Time Complexity
- Best Case: Target is the root → O(1).
- Average Case: Roughly balanced tree → O(log n).
- Worst Case: Degenerate/skewed tree → O(n).
Time Complexity Analysis
Space Complexity
Search needs O(1) additional space (excluding the recursion call stack), since it never creates or modifies any nodes — it's a read-only walk.
Because search, insertion, and deletion all follow this same root-to-leaf comparison walk, they share the same best/worst-case complexity profile — which is exactly why keeping a BST balanced (see AVL trees) matters for all three operations, not just one.