堆的建立&堆排序
2017-03-31 23:13
295 查看
2017 / 3 / 31 很久没写博客了 最近除了上课 睡觉 吃饭 其他时间几乎都待在图书馆里飙车 其实也谈不上什么飙车 就是一个跌破低谷的咸鱼在努力尝试着翻身 今天和朋友看了场电影 终于克服掉进电影院的阴影 可能害怕看到一对对情侣想到以前的我们 因为和你待在一起最久地方除了家就是电影院 曲终人散 人走茶凉 我就不要想起你 撒油啦啦 渍渍渍 在这车手如云的论坛上竟然写起了随笔 好尴尬 勿喷 接下来我们说正题 这次介绍数据结构中简单的二叉堆。
堆的建立
二叉堆是一种特殊的堆,二叉堆是完全二元树或者是近似完全二元树。 二叉堆有两种:最大堆和最小堆。 最大堆:父结点的键值总是大于或等于任何一个子节点的键值; 最小堆:父结点的键值总是小于或等于任何一个子节点的键值。 堆一般用数组存放:根节点为1节点,i节点的左子节点为2*i,右子节点为2*i+1。 插入:因为从根节点到叶节点是一个有序序列。将新插入的节点放到最后然后将其放到这个有序序列。 删除:删除某一个节点, 将最后一个节点填充到删除的节点处,然后将此节点看做成根节点构造一个堆。 多BB一句 这里让数组下标从1开始比从0开始方便些 比如1的左右儿子分别就是1*2和1*2+1
#include <iostream> #include <malloc.h> #define MAXSIZE 100 #define ElemType int using namespace std; typedef struct Heap{ int capacity; int size; ElemType elements[MAXSIZE + 1]; }Heap; void CreatHeap(Heap *&H) { H = (Heap *) malloc (sizeof(Heap)); H->capacity = MAXSIZE; H->size = 0; } bool IsFull(Heap *H) { if(H->size == H->capacity) return true; return false; } void Insert(Heap *&H, ElemType x) { if(IsFull(H)){ cout<<"Heap is full"<<endl; return; } for(int i = ++H->size; H->elements[i / 2] > x; i /= 2) H->elements[i] = H->elements[i / 2]; H->elements[i] = x; } bool IsEmpty(Heap *H) { if(H->size) return false; return true; } ElemType DeleteMin(Heap *&H) { if(IsEmpty(H)){ cout<<"The heap is empty"<<endl; exit(-1); } int i, child, MinElement, LastElement; MinElement = H->elements[1]; LastElement = H->elements[H->size--]; for(i = 1; 2 * i <= H->size; i = child){ child = 2 * i; if(H->elements[child] > H->elements[child + 1]) ++child; if(LastElement > H->elements[child]) H->elements[i] = H->elements[child]; else break; } H->elements[i] = LastElement; return MinElement; } int main() { int i, x; Heap *h; CreatHeap(h); for(i = 1; i <= 10; i++){ cin>>x; Insert(h, x); } cout<<"Heap is: "; for(i = 1; i <= h->size; i++) cout<<h->elements[i]<<' '; cout<<endl; cout<<"Now insert an element"<<endl; cin>>x; Insert(h, x); for(i = 1; i <= h->size; i++) cout<<h->elements[i]<<' '; cout<<endl; cout<<"Delete min element: "<<DeleteMin(h)<<endl; for(i = 1; i <= h->size; i++) cout<<h->elements[i]<<' '; cout<<endl; return 0; }
堆排序
堆的排序:因为堆的根节点是最小堆值,所以每次取根节点,然后将其删除,剩余的节点重新构造成堆以此递归完成排序。 使用最小堆得到递增序列,最大堆得到递减序列。 二次操作时间相加还是O(N * logN)。故堆排序的时间复杂度为O(N * logN)。
#include <iostream> #define MAXSIZE 100 using namespace std; void swap(int *a, int *b) { int t; t = *a; *a = *b; *b = t; } void HeapAdjust(int a[], int i, int n) { int child, tmp; for(tmp = a[i]; 2 * i <= n; i = child){ child = 2 * i; if(a[child] < a[child + 1]) ++child; if(child <= n &&a[child] > tmp) a[i] = a[child]; else break; } a[i] = tmp; } void HeapSort(int a[], int n) { int i; for(i = n / 2; i > 0; i--) //Build heap HeapAdjust(a, i, n); for(i = n - 1; i > 0; i--){ //Sort swap(&a[1], &a[i + 1]); //这里切记是a[i + 1] 数组中下标是从1到n的 0号位置相当于废了Orz HeapAdjust(a, 1, i); } } int main() { int i, n, a[MAXSIZE]; cin>>n; for(i = 1; i <= n; i++) cin>>a[i]; cout<<"Before: "; for(i = 1; i <= n; i++) cout<<a[i]<<' '; 9c7f cout<<endl; HeapSort(a, n); cout<<"After: "; for(i = 1; i <= n; i++) cout<<a[i]<<' '; cout<<endl; return 0; }
相关文章推荐
- 在KEIL中建立了工程之后,编译就出现这样的提示can't execute 'C:\Keil\C51\BIN\A51.EXE'
- 堆排序和建立最大堆
- centos ssh 间建立信任关系 (免密码登录&传输)
- C/C++日常学习总结(第八篇)winsock建立连接的步骤&窗口的生成
- win7下Qt 5建立空白项目 无法打开包括文件<QApplication> No such file or directory
- Torque引擎最精简的torque程序建立环境[含源代码下载](Most Simple&Base Torque Mod Framework)
- 解决Error"基础连接已经关闭: 未能为SSL/TLS 安全通道建立信任关系。"
- &高手解析在Win 7上如何建立虚拟wifi设置&【mfxp】
- 建立&修改视图
- 排序&&堆排序
- <<node.js探秘>>(02).建立一个http站点服务.
- 数据结构中堆的建立,建小堆,以及堆排序
- FPGA建立时间(setup time)&保持时间(hold time)&竞争和冒险&毛刺
- 按层次&nbsp;建立二叉树
- 数据库SQL Server 2008&Visual Studio建立管理系统
- PHP基础->文件读写->遍历目录->文件属性->建立、删除文件目录
- Oracle® Database 2 Day DBA 11.2翻译--第二章 Oracle数据库软件安装和数据库建立(上)
- MYSQL建立外键失败几种情况记录Can't create table不能创建表
- Redis2.2.2源码学习——Server&Client链接的建立以及相关Event
- 一份"断舍离"清单,帮你迅速建立精…