优先队列(堆)priority queue

优先队列(堆)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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值