全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/*最大堆问题,堆数据结构是一颗完全二叉树,用数组来完成其物理实现,逻辑上实际是一种树形结构,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重庆市綦江区郭扶镇招聘公益性岗位5人参考题库附答案详解(研优卷)
- 2026内蒙古赤峰市红山区第四批“绿色通道”引进教师4人备考题库及参考答案详解【轻巧夺冠】
- 2026新疆可克达拉职业技术学院招聘事业单位工作人员89人参考题库含答案详解【巩固】
- 关于江西吉湖启鹏材料有限公司2026年面向社会公开招聘2名辅助管理岗的补充参考题库及完整答案详解【网校专用】
- 2026重庆市畜牧科学院招聘30人(第二批)参考题库及答案详解【全优】
- 2026年6月浙江温州外国语高级中学教师招聘6人笔试题库(考试直接用)附答案详解
- 内镜医师考试题目及答案
- 新电子税务局测试题库及答案
- 免疫医学试题及答案
- 临床化验试题及答案
- 2025安徽合肥庐江县乡村振兴投资有限公司招聘工作人员(第二批)人员笔试历年典型考点题库附带答案详解
- 腹膜炎诊疗规范课件
- 医院病历档案管理规范标准
- 超市洗化类知识培训课件
- 孔明灯制作课件
- 2026年1月浙江省高考(首考)化学试题(含标准答案)
- 八年级物理上册全册知识点(教科版2026新教材)
- 2026中央广播电视总台招聘备考笔试题库及答案解析
- 广西国控集团招聘笔试题库2026
- 基于AI的材料性能预测模型
- 声音环境的听觉空间感知
评论
0/150
提交评论