Why lookup in a Binary Search Tree is O(log(n))?

Hommer Smith picture Hommer Smith · Jan 20, 2013 · Viewed 46.2k times · Source

I can see how, when looking up a value in a BST we leave half the tree everytime we compare a node with the value we are looking for.

However I fail to see why the time complexity is O(log(n)). So, my question is:

If we have a tree of N elements, why the time complexity of looking up the tree and check if a particular value exists is O(log(n)), how do we get that?

Answer

Mike Higginbottom picture Mike Higginbottom · Jan 20, 2013

Your question seems to be well answered here but to summarise in relation to your specific question it might be better to think of it in reverse; "what happens to the BST solution time as the number of nodes goes up"?

Essentially, in a BST every time you double the number of nodes you only increase the number of steps to solution by one. To extend this, four times the nodes gives two extra steps. Eight times the nodes gives three extra steps. Sixteen times the nodes gives four extra steps. And so on.

The base 2 log of the first number in these pairs is the second number in these pairs. It's base 2 log because this is a binary search (you halve the problem space each step).