全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/*最大堆问题,堆数据结构是一颗完全二叉树,用数组来完成其物理实现,逻辑上实际是一种树形结构,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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 铁路标志设置考试题目与答案分享
- 高中物理必修第一册3.1.2
- 2026年音乐学科培训试卷
- 土方现场管理测验题及答案
- 土的冻胀试验施工工艺
- 意大利文化测验题及答案
- 索道运营笔试试题及答案解析
- 单招新能源题目及参考答案
- 全民阅读满意度调查问卷
- 2026年中职(物联网技术应用)物联网设备安装调试试题及答案
- 2026年江苏苏州相城区村(社区)工作者招聘考试面试试题-含参考答案
- 电力设施保护措施培训课件
- 2026年征兵心理测试题及答案
- 2026年云南省中考道德与法治试卷
- 医疗机构医保稽核问题整改台账
- 2026年交通运输工程师考试题库
- 2026湖南益阳市消防救援支队消防文员招聘3人备考题库及答案详解(有一套)
- 田径项目核心训练计划
- 工程测量员保密意识考核试卷含答案
- 2025年齐齐哈尔泰来县人民法院招聘聘用制人员考试真题附答案
- 中核产业基金管理有限公司招聘笔试题库2026
评论
0/150
提交评论