全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/*最大堆问题,堆数据结构是一颗完全二叉树,用数组来完成其物理实现,逻辑上实际是一种树形结构,r是下标,parent(r)=int(r-1)/2);leftchild(r)=2r+1;rightchild(r)=2r+2;*/以下是头文件maxheap.h的内容templateclass maxheapprivate:Elem* heapArray;int maxsize;int n;/堆的大小public:maxheap(int size = 10)maxsize = size;heapArray = new Elemmaxsize;/注意为中括号n = 0;maxheap() delete heapArray;int heapsize() return n;int parent(int pos) return (pos-1)/2;int leftchild(int pos) return 2*pos+1;int rightchild(int pos) return 2*pos+2;bool insert(Elem& e);bool remove(int pos);bool creatHeap(int ArrLen,Elem* arr);void preorder(int pos);void heapSort(int arrlen);void print();templatebool maxheap:insert(Elem& e)if(n=maxsize-1)return false;heapArrayn = e;/cout=0 & heapArrayparent(temp)heapArraytemp)ha = heapArrayparent(temp);heapArrayparent(temp) = heapArraytemp;heapArraytemp = ha;/coutheapArraytempheapArrayparent(temp);temp = parent(temp);n +=1;return true;templatebool maxheap:creatHeap(int ArrLen, Elem* arr)int n = ArrLen;/heapArray = new ElemArrLen;for(int i = 0; iArrLen-1 & arri; i+)insert(arri);return true;templatebool maxheap:remove(int pos)if(posn)return false;Elem temp;temp = heapArraypos;heapArraypos = heapArrayn-1;heapArrayn-1 = temp;n = n-1;/删除一个节点后,堆节点数减1int j = leftchild(pos);while(j=n-1)if( heapArrayleftchild(pos) heapArrayrightchild(pos) & rightchild(pos) = heapArrayj )break;/如果父节点比左右节点中较大节点大,则循环退出temp = heapArrayj; /父节点与较大节点交换heapArrayj = heapArraypos;heapArraypos = temp; pos = j;j = leftchild(pos);return true;templatevoid maxheap:preorder(int pos) /前序遍历/coutpos ;coutheapArrayposendl;if(leftchild(pos)n)preorder(leftchild(pos);if(rightchild(pos)n)preorder(rightchild(pos);templatevoid maxheap:print()cout物理实现(数组形式):;for(int i = 0; in; i+)coutheapArrayi ;coutendl;cout逻辑结构基于数组的完全二叉树(最大堆);endl;templatevoid maxheap:heapSort(int arrlen)for(int i = 0; iarrlen-1; i+)remove(0);cout堆排序:endl;for(i = 0; iarrlen-1; i+)coutheapArrayiendl;以下是usemaxheap.cpp的内容#include#includemaxheap.husing namespace std;int main()maxheap mymaxheap1(1024);int arr=4,8,25,3,91,54,24,36,74,15,11,8,97,102,114,125,33;mymaxheap1.creatHeap(17,arr);mymaxheap1.print();cout前序遍历:endl;mymaxheap1.preorder(0);mymaxheap1.hea
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026贵州黔南州罗甸县面向社会选聘城市社区工作者8人笔试备考试题及答案解析
- 2026四川大学华西医院医保真实世界研究与评价中心项目制科研助理招聘1人笔试备考试题及答案解析
- 2026年海原县教师招聘笔试备考题库及答案解析
- 2026年信丰县教师招聘考试备考题库及答案解析
- 2026双鸭山市重新遴选市委市政府法律顾问考试备考题库及答案解析
- 2026年多伦县教师招聘考试模拟试题及答案解析
- 2026年地下综合管廊工程建筑行业投资前景报告及未来五至十年战略布局与增长路径
- 2026年闽清县教师招聘笔试参考题库及答案解析
- 2026年临猗县教师招聘考试备考试题及答案解析
- 2026福建福州市江南智慧城市建设运营有限公司社会招聘4人笔试参考题库及答案解析
- 人教PEP四年级英语上册阅读理解专项30篇(含答案)
- 2026年秋季开学中秋诗词赏析课件
- 2026临汾市侯马市招聘乡(街道)消防协管员考试备考试题及答案详解
- 2026秋学期人教版小学数学六年级上册(新教材)教学计划附进度表
- 2026年秋季学期小学四年级上册英语(人教版PEP新教材)教学计划
- 自来水生产工岗前专项能力考核试卷含答案
- 2026教科版六年级科学上册第一单元《健康生活》全部教案
- 2026年山东青岛市中考历史试题(附答案)
- 江西省人才发展集团有限公司2026年春季集中招聘专题【11人】建设笔试备考题库及答案解析
- 2026年重庆市九龙坡区辅警人员招聘考试试卷及答案
- 2026年高考上海卷英语含解析及答案(新课标卷)
评论
0/150
提交评论