ImageVerifierCode 换一换
格式:DOCX , 页数:18 ,大小:27.57KB ,
资源ID:2076765      下载积分:12 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/2076765.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(山东大学数据结构实验报告六.docx)为本站会员(b****1)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

山东大学数据结构实验报告六.docx

1、山东大学数据结构实验报告六数据结构实验报告实验六实验题目:排序算法 学号: 201411130001日期:2015.12.11班级:计机14.1姓名:刘方铮 Email:liu191150932实验目的:堆和搜索树任务要求:一、实验目的 1、掌握堆和搜索树的基本概念,插入、删除方法。二、实验内容 创建最大堆类。最大堆的存储结构使用链表。提供操作:堆的插入、堆的删除。堆的初始化。Huffman树的构造。二叉搜索树的构造。接收键盘录入的一系列整数,输出其对应的最大堆、Huffman编码以及二叉搜索树。堆排序。软件环境:Win7 操作系统开发工具:visual C+ 6.0实验代码:#include

2、 #include #include#includeusing namespace std;class BinaryTreeNode public:BinaryTreeNode() LeftChild=RightChild=0;BinaryTreeNode(const int & e) data=e;LeftChild=RightChild=0;BinaryTreeNode(const int& e,BinaryTreeNode *l,BinaryTreeNode *r) data=e;LeftChild=l;RightChild=r;int data;BinaryTreeNode *Left

3、Child,*RightChild; void Travel(BinaryTreeNode* roots) queue q; while(roots) coutdataLeftChild)q.push(roots-LeftChild); if(roots-RightChild)q.push(roots-RightChild); try roots=q.front(); q.pop (); catch(.) return; void PrOrder(BinaryTreeNode* roots)if(roots) coutdataLeftChild); PrOrder(roots-RightChi

4、ld);void InOrder(BinaryTreeNode* roots)if(roots) InOrder(roots-LeftChild); coutdataRightChild);class MaxHeap public:MaxHeap() root = 0; state = 0; void MakeHeap(int element, MaxHeap& left, MaxHeap& right); int Max() if (!root) return 0; return root-data; MaxHeap& Insert(const int& x); MaxHeap& Delet

5、eMax(int& x); MaxHeap& Initialize(int a, int n); void Deactivate()heap=0; void HeapSort(int a,int n); BinaryTreeNode *root, *last,*p_last; int state; void ConditionOrder(BinaryTreeNode *u, int k, int i,BinaryTreeNode *temp); void Adjust(BinaryTreeNode *u); BinaryTreeNode* LocateLast(BinaryTreeNode *

6、u,int k,int i);private: MaxHeap *heap; void MaxHeap:MakeHeap(int element, MaxHeap& left, MaxHeap& right) root = new BinaryTreeNode(element, left.root, right.root); left.root = right.root = 0; last=p_last=root; state+; BinaryTreeNode* MaxHeap:LocateLast(BinaryTreeNode *u,int k,int i) if(k=1) return u

7、; else int n=(int)pow(2.0,k-1); int s=n/2; if(iLeftChild,k-1,i); else return LocateLast(u-RightChild,k-1,i-s); void MaxHeap:ConditionOrder(BinaryTreeNode *u, int k, int i,BinaryTreeNode *temp) int half = (int) pow(2.0, k - 2); if (u-data data) swap(u-data, temp-data); if (!u-LeftChild | !u-RightChil

8、d) if (!u-LeftChild) u-LeftChild = temp; p_last=u; state+; else u-RightChild = temp; p_last=u; state+; else if (i LeftChild, k - 1, i, temp); else ConditionOrder(u-RightChild, k - 1, i - half, temp); MaxHeap& MaxHeap:Insert(const int& x) if(root) int k = (int) (log(double)(state) / log(2.0) + 1; int

9、 index = state - (int) (pow(2.0, k - 1) - 1); int p_index = index / 2 + 1; BinaryTreeNode *temp = new BinaryTreeNode (x); last = temp; if (index = (int) (pow(2.0, k - 1) p_index = 1; ConditionOrder(root, k, p_index, temp); else ConditionOrder(root, k - 1, p_index, temp); else root = new BinaryTreeNo

10、de(x); last=p_last=root; state+; return *this; void MaxHeap:Adjust(BinaryTreeNode *u) if (!u-LeftChild & !u-RightChild) return; else if(u-LeftChild & u-RightChild) if (u-LeftChild-data u-RightChild-data) if (u-data LeftChild-data) swap(u-data, u-LeftChild-data); Adjust(u-LeftChild); else if (u-data

11、RightChild-data) swap(u-data, u-RightChild-data); Adjust(u-RightChild); MaxHeap& MaxHeap:DeleteMax(int& x) if (!root) exit(1) ; else if(!last) x=root-data; state=0;root=0; else x = root-data; root-data = last-data; int k = (int)(log(double)(state)/log(double)(2)+ 1; int index = state - (int)(pow(2.0

12、, k - 1) - 1); Adjust(root); if(index%2) p_last-LeftChild=0; else p_last-RightChild=0; delete last; state-; k = (int)(log(double)(state-1)/log(double)(2)+ 1; index = state - 1 - (int)(pow(2.0, k - 1) - 1); int p_index = index / 2 + 1; if (index = (int) (pow(2.0, k - 1) p_index=1; p_last=LocateLast(root,k,p_index); else p_last=LocateLast(root,k-1,p_index); if(!p_last-RightChild) last=p_last-LeftChild; else last=p_last-RightChild; return *this; MaxHeap& MaxHeap:Initialize(int a, int n) MaxHeap LMaxHeap,RMaxHeap; MakeHeap(a0,LMaxHeap,RMaxHeap); for(int i=1;i=0;i-) max

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1