Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 450
Hommer Smith Avatar asked Jan 20 '13 16:01

Hommer Smith


People also ask

Why the binary search on is array O log n?

The main reason why binary search (which requires sorted data in a data structure with O(1) random access reads) is O(log N) is that for any given data set, we start by looking at the middle-most element.

Why is binary search logarithmic?

In other words, the number of times binary search runs increases logarithmically with the number of elements it has to search. This is the opposite of an exponential increase. For example, the algorithm must run a maximum of 2 times to search 4 items, 3 times to search 8 items, and only 5 times to search 32 items.

Is binary search log n or log2 n?

The definition of the logarithm says that k is about log2(n), so binary search has that complexity. So basically, in this case log2(𝑛) is simplified as log n in the lecture.

Why is the Big O of inserting into a BST O n and not O lg n )?

In the absolute worst case, a binary tree with N elements would be like a linked list. Hence, there would be N levels, and a search would take N traversals. That's why it's O(N) in the worst case.


2 Answers

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).

like image 111
Mike Higginbottom Avatar answered Nov 27 '22 18:11

Mike Higginbottom


For me the easiest way was to look at a graph of log2(n), where n is the number of nodes in the binary tree. As a table this looks like:

          log2(n) = d

          log2(1) = 0
          log2(2) = 1 
          log2(4) = 2
          log2(8) = 3
          log2(16)= 4 
          log2(32)= 5
          log2(64)= 6             

and then I draw a little binary tree, this one goes from depth d=0 to d=3:

            d=0          O
                        / \
            d=1        R   B
                      /\   /\
            d=2      R  B R  B
                    /\ /\ /\ /\
            d=3    R B RB RB R B

So as the number of nodes, n, in the tree effectively doubles (e.g. n increases by 8 as it goes from 7 to 15 (which is almost a doubling) when the depth d goes from d=2 to d=3, increasing by 1.) So the additional amount of processing required (or time required) increases by only 1 additional computation (or iteration), because the amount of processing is related to d.

We can see that we go down only 1 additional level of depth d, from d=2 to d=3, to find the node we want out of all the nodes n, after doubling the number of nodes. This is true because we've now searched the whole tree, well, the half of it that we needed to search to find the node we wanted.

We can write this as d = log2(n), where d tells us how much computation (how many iterations) we need to do (on average) to reach any node in the tree, when there are n nodes in the tree.

like image 28
Will Avatar answered Nov 27 '22 16:11

Will