博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【堆】这是要搞事情啊——取出
阅读量:4676 次
发布时间:2019-06-09

本文共 866 字,大约阅读时间需要 2 分钟。

堆是什么?请

取出

取出加删除元素(小根堆)算法(简单粗略加通俗):

1.取出根结点。

2.最后一个节点将根结点覆盖,len--。

3.循环:把根结点和它儿子中小的一个交换,直到没有儿子。

硬模拟:

int get(){	int now,next,res;	res=heap[0];	heap[0]=heap[--len];	now=0;	while(now*2
以上代码编译过,未测试,呵呵,应该没问题。

当然,直接库函数,懒癌福利(需添加algorithm、iostream及using namespace std头文件)

int get(){	//pop_heap(heap,heap+len);//大根堆	pop_heap(heap,heap+len,greater
());//小根堆 return heap[--len];}

至于堆吧,排序是可以的,其实很容易发现,建立堆过后每次get出来的就是最小(大根堆就是最大)的。

以下是小根堆排序程序:

//小根堆排序#include
#include
#include
using namespace std;int heap[100];int n,len;void put(int k){ heap[len++]=k; push_heap(heap,heap+len,greater
());}int get(){ pop_heap(heap,heap+len,greater
()); return heap[--len];}int main(){ int x; scanf("%d",&n); for(int i=0;i
                                                                By WZY

转载于:https://www.cnblogs.com/LinqiongTaoist/p/7203762.html

你可能感兴趣的文章
Linux进程间通信---共享内存
查看>>
Computer Information
查看>>
交换机/路由器上的 S口 F口 E口
查看>>
P1298(矩阵切割)DP
查看>>
wzplayer for delphi demo截图
查看>>
团队第二周:SRS文档
查看>>
Zookeeper的安装与使用:
查看>>
密码策略限制最大与最小长度
查看>>
正则表达式模式
查看>>
使用iframe实现同域跨站提交数据
查看>>
Mouse点击之后,复制GridView控件的数据行
查看>>
ASP.NET开发,从二层至三层,至面向对象 (2)
查看>>
如何查看自己电脑支持OpenGL core版本
查看>>
Halcon使用的3D相机模型-camera_calibration
查看>>
页面元素定位 XPath 简介
查看>>
[转]loadrunner:系统的平均并发用户数和并发数峰值如何估算
查看>>
Linux下Tomcat重新启动
查看>>
HTML Table to Json
查看>>
Theano 学习笔记(一)
查看>>
1.7 节点进行排序显示
查看>>