Big O(h) vs. Big O(logn) in trees

后端 未结 2 1022
梦如初夏
梦如初夏 2021-01-12 14:18

I have a question on time complex in trees operations.
It\'s said that (Data Structures, Horowitz et al) time complexity for insertion, deletion, search, finding mins-ma

2条回答
  •  遥遥无期
    2021-01-12 15:06

    h is the height of the tree. It is always Omega(logn) [not asymptotically smaller then logn]. It can be very close to logn in complete tree (then you really get h=logn+1, but in a tree that decayed to a chain (each node has only one son) it is O(n).

    For balanced trees, h=O(logn) (and in fact it is Theta(logn)), so any O(h) algorithm on those is actually O(logn).

    The idea of self balancing search trees (and AVL is one of them) is to prevent the cases where the tree decays to a chain (or somewhere close to it), and its (the balanced tree) features ensures us O(logn) height.

    EDIT:
    To understand this issue better consider the next two trees (and forgive me for being terrible ascii artist):

              tree 1                                tree 2
                7
               /
              6
             /
            5                                         4
           /                                      /       \
          4                                      2         6
         /                                    /    \     /   \
        3                                    1      3   5     7
       /
      2
     /
    1
    

    Both are valid Binary search trees, and in both searching for an element (say 1) will be O(h). But in the first, O(h) is actually O(n), while in the second it is O(logn)

提交回复
热议问题