优先队列(堆)priority queue
- 完全二叉树:除了最底层都被元素填满
- 堆序性:除根节点,最小堆每个节点父亲的Key小于等于该节点的Key,最大堆反之
优先队列的申明
struct HeapStruct;
typedef struct HeapStruct* PriorityQueue;
PriorityQueue Initialize (int MaxElements);
void Destroy (PriorityQueue H);
void MakeEmpty (PriorityQueue H);
void Insert (ElementType X, PriorityQueue H);
ElementType DeleteMin (PriorityQueue H);
ElementType FindMin (PriorityQueue H);
int IsEmpty (PriorityQueue H);
int IsFull (PriorityQueue H);
struct HeapStruct
{
int Capacity;
int size;
ElementType* Elements;
}
Insert
插入操作,将新增元素放到最后面,比较其与父节点,进行上滤,O(logN)
DeleteMin
删除操作,删除根节点元素后,进行下滤,O(logN)
FindMin
根元素即为所求,O(1)
应用
求第k大的数,求最大前k个数,klogN