Acm学习总结19
关于二叉树的应用:经常应用于堆里面,作为堆经常用的是两个函数一个get()函数,一个就是堆函数put()
int get() //heap[1]为堆顶
{
int now=1, next, res= heap[1];
heap[1] = heap[heap_size–];
while(now * 2 <= heap_size)
{
next = now * 2;
if (next < heap_size && heap[next + 1] < heap[next]) next++;
if (heap[now] <= heap[next]) break;
swap(heap[now], heap[next]);
now = next;
}
return res;
}
这是读取函数;
void put(int d) //heap[1]为堆顶
{
int now, next;
heap[++heap_size] = d;
now = heap_size;
while(now > 1)
{
next = now >> 1;
if(heap[now] >= heap[next]) break;
swap(heap[now], heap[next]);
now = next;
}
}
这是堆入函数;
解决问题的时候还需要这两个函数就可以作为模板直接使用;
这次的总结也就这些了。
Acm学习总结19
最新推荐文章于 2024-02-25 18:23:11 发布