二叉堆
2015-09-20 15:41
218 查看
二叉堆是一种富有特色的数据结构,可用数组简便实现。明显的规律,eg:L_child=parent * 2 , R_child=parent * 2 + 1;
#include <stdio.h> #define N 100 int heap ,size=0; void push(int x) { int i=size++; while(i>0) { int j=(i-1)/2; if(heap[j]<=x) break; heap[i]=heap[j]; i=j; } heap[i]=x; } int pop() { int res=heap[0],t=heap[--size]; int i=0; while(i*2+1<size) { int j=i*2+1; if(j+1<size&&heap[j+1]<heap[j]) j++; if(heap[j]>=t) break; heap[i]=heap[j]; i=j; } heap[i]=t; return res; } int main() { int x; while(1) { scanf("%d",&x); if(x==-1) break; push(x); } while(size) printf("%d%c",pop(),size==1? '\n':' '); return 0; }
相关文章推荐
- Android贴吧系统学习-----悬浮布局出现的bug--android.view.WindowManager$BadTokenException:
- Android(异步任务) AsyncTask
- 使用dxNavBar动态创建应用程序菜单
- 数值
- Android View生命周期
- code::blocks的使用
- easyUI之Combo
- [LeetCode]1. 2Sum 数组中和为特定值的两个数
- iOS代码实践总结
- tchar.h及TCHAR数据类型介绍
- Windows安装Nodejs的模块和引用问题(cannot find moudel "xxx")?
- JSP之response对象使用
- CUDA和OpenCL的区别
- linux 查看网卡流量六种方法
- 在CentOS 6.4中编译安装gcc 4.8.1
- 网卡
- 【UML】概述
- VS2003添加.BMP资源
- 协同过滤入门介绍(转)
- SQL SERVER 存储大全以及常见实例