Finding a value in a BST

Why finding a value in a BST takes 4 steps instead of 15, halving the tree at every comparison.

A binary search tree keeps an ordering promise: everything smaller lives down the left, everything bigger down the right. Searching uses that promise, compare at the root, go left or right, and you've just eliminated half the remaining tree.

Repeat at each node and you reach any value in O(log n) steps: a balanced tree of a million values needs about 20 comparisons. That's the same halving idea as binary search on an array, expressed as a structure you can also insert into cheaply.

Remember this

  • Left subtree = smaller, right subtree = bigger, always
  • Every comparison discards half the remaining tree
  • Balanced BSTs find anything in O(log n)

Got a different question? SeaThru generates a fresh video for any topic where systems talk or data structures move.

Ask your own question →

Keep learning