Efficiency of the STL priority_queue

前端 未结 6 1194
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 06:11

I have an application (C++) that I think would be well served by an STL priority_queue. The documentation says:

Priority_queue is a cont

6条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 07:03

    C++ STL priority_queue underlying data structure is Heap data structure(Heap is a non linear ADT which based on complete binary tree and complete binary tree is implemented through vector(or Array) container.

    ex  Input data : 5 9 3 10 12 4.
    
    Heap (Considering Min heap) would be :
    
                       [3]
                 [9]             [4]
             [10]    [12]     [5]
    
    
       NOW , we store this min heap in to vector,             
          [3][9][4][10][12][5].
          Using formula ,
          Parent : ceiling of n-1/2
          Left Child : 2n+1
          Right Child : 2n+2 .
      Hence ,
        Time Complexity for 
                 Top = O(1) , get element from root node.
                 POP()= O(logn) , During deletion of root node ,there  is      chance to violation of  heap order . hence restructure of heap order takes at most O(logn) time (an element might move down to height of tree).
                PUSH()= O(logn) , During insertion also , there might chance to violation of  heap order . hence restructure of heap order takes at most O(logn) time (an element might move up to root from leaf node).
    

提交回复
热议问题