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

后端 未结 5 1624
失恋的感觉
失恋的感觉 2021-01-31 18:20

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

5条回答
  •  情书的邮戳
    2021-01-31 19:22

    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.

提交回复
热议问题