- 插入
void put(int d)
{
int now,next;
heap[++heap_size]=d;
now=heap_size;
while(now>1)
{
next=now/2;
if(heap[now]>heap[next]) break;//小根堆
swap(heap[now],heap[next]);
now=next;
}
}
- 删除
int get()
{
int now,next,res;
res=heap[1];
heap[1]=heap[heap_size--];
now=1;
while(now*2<=heap_size)
{
next=now*2;
if(next<heap_size&&heap[next+1]<heap[next]) next++;//找出子节点中权值最小的点
if(heap[now]<=heap[next]) return res;//小根堆
swap(heap[now],heap[next]);
now=next;
}
return res;
}