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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

本文(二叉树基本操作与哈夫曼编码译码系统实现.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

二叉树基本操作与哈夫曼编码译码系统实现.docx

1、二叉树基本操作与哈夫曼编码译码系统实现实 验 报 告( / 学年 第 一 学期)课程名称数据结构A实验名称二叉树的基本操作及哈夫曼编码译码系统的实现实验时间年月日指导单位指导教师学生姓名班级学号学院(系)专 业实 验 报 告实验名称二叉树的基本操作及哈夫曼编码译码系统的实现指导教师实验类型上机实验学时实验时间一、 实验目的和要求实验目的:1、掌握二叉链表上实现二叉树基本运算的方法。2、学会设计基于遍历的求解二叉树应用问题的递归算法。3、理解哈夫曼树的构造算法,学会设计哈夫曼编码和译码系统。内容和要求:1、在二叉链表上实现二叉树运算设计递归算法,实现下列算法:删除一棵二叉树,求一棵二叉树的高度,

2、求一棵二叉树中叶子结点的个数,复制一棵二叉树,交换一颗二叉树的左右子树。设计算法,按自上到下,自左向右的次序,即按层次遍历一颗二叉树。设计main函数,测试上述每个运算。2、哈夫曼编码和译码系统设计的系统重复显示以下菜单项:建树、遍历、生成编码、编码、译码、打印、退出并且实现这些功能。二、实验环境(实验设备)硬件:PC软件:Code:Blocks (C+)三、实验原理及内容1、线性表的基本运算(1)核心算法删除一颗二叉树:思路:将一颗二叉树拆分成三部分,执行语句“delete root; root=NULL”,将原二叉树的根结点回收。代码:template void BinaryTree:Br

3、eakTree(T &x,BinaryTree &left,BinaryTree &right) if(!root|&left=&right|left.root|right.root) return; x=root-element; left.root=root-lChild; right.root=root-rChild; delete root; root=NULL; 求一棵二叉树的高度:思路:递归搜索二叉树,不断返回高度。代码:templateint BinaryTree:High() int h=High2(root); return h;templateint BinaryTree:

4、High2(BTNode *p) if(!p) return 0; int h1,h2; h1=High2(p-lChild); h2=High2(p-rChild); if(h1h2) return h1+1; else return h2+1; 求一棵二叉树中叶子结点的个数:思路:递归搜索二叉树的叶子结点,不断累加。代码:templateint BinaryTree:Leaves() int number=0; Leaf(root,number); return number;templatevoid BinaryTree:Leaf(BTNode *p,int &a) if(p) if(p

5、-lChild=0&p-rChild=0) a+; Leaf(p-lChild,a); Leaf(p-rChild,a); 复制一棵二叉树:思路:用q指针指向复制的二叉树的根结点,递归调用Copy(),不断复制左子树和右子树。 代码:templatevoid BinaryTree:Copy(BinaryTree &p) BTNode *q=Copy(root); p.root=q;templateBTNode* BinaryTree:Copy(BTNode *t) if(!t) return NULL; BTNode *q=new BTNode(t-element); q-lChild=Cop

6、y(t-lChild); q-rChild=Copy(t-rChild); return q;交换一颗二叉树的左右子树:思路:递归调用Change(),不断交换左、右子树。代码:templatevoid BinaryTree:Exchange() Change(root);templatevoid BinaryTree:Change(BTNode *t) if(t) BTNode *temp=t-lChild; t-lChild=t-rChild; t-rChild=temp; Change(t-lChild); Change(t-rChild); 按自上到下,自左向右的次序的层次遍历:思路:

7、利用队列作为辅助数据结构,队列的元素类型是指向二叉树中结点的指针类型,首先让根结点入队,接着做一个循环:每当一个元素出队,则它的左右子树依次入队。代码:templateclass SeqQueueprivate: int front,rear; int maxSize; T *q;public: SeqQueue(int m); SeqQueue()delete q; bool EnQueue(const T x); bool DeQueue(); bool Front(T &x)const; bool IsEmpty()return front=rear; bool IsFull()retu

8、rn (rear+1)%maxSize=front; void Clear()front=rear=0;templateSeqQueue:SeqQueue(int m) maxSize=m; q=new Tm; front=rear=0;templatebool SeqQueue:Front(T &x)const x=q(front+1)%maxSize; return true;templatebool SeqQueue:EnQueue(const T x) if(IsFull() coutfullendl; return false; q(rear=(rear+1)%maxSize)=x;

9、 return true;templatebool SeqQueue:DeQueue() if(IsEmpty() coutUnderFlowendl; return false; front=(front+1)%maxSize; return true;templatevoid BinaryTree:LayerOrder() if(root=0) coutThis tree is empty!endl; return ; BTNode *p=root; BTNode t; SeqQueue BTNode q(30); q.EnQueue(*p); while(!q.IsEmpty() q.F

10、ront(t); Visit(t.element); if(t.lChild) q.EnQueue(*t.lChild); if(t.rChild) q.EnQueue(*t.rChild); q.DeQueue(); (2)程序流程图(3) 完整代码:#include using namespace std;templateclass SeqQueueprivate: int front,rear; int maxSize; T *q;public: SeqQueue(int m); SeqQueue()delete q; bool EnQueue(const T x); bool DeQu

11、eue(); bool Front(T &x)const; bool IsEmpty()return front=rear; bool IsFull()return (rear+1)%maxSize=front; void Clear()front=rear=0;templateSeqQueue:SeqQueue(int m) maxSize=m; q=new Tm; front=rear=0;templatebool SeqQueue:Front(T &x)const x=q(front+1)%maxSize; return true;templatebool SeqQueue:EnQueu

12、e(const T x) if(IsFull() coutfullendl; return false; q(rear=(rear+1)%maxSize)=x; return true;templatebool SeqQueue:DeQueue() if(IsEmpty() coutUnderFlowendl; return false; front=(front+1)%maxSize; return true;templatestruct BTNode BTNode()lChild=rChild=NULL; BTNode(const T &x) element=x; lChild=rChil

13、d=NULL; BTNode(const T &x,BTNode *l,BTNode *r) element=x; lChild=l; rChild=r; T element; BTNode *lChild,*rChild;templateclass BinaryTreepublic: BinaryTree()root=NULL; BinaryTree(); bool IsEmpty()const;/判断二叉树是否为空 void Clear();/释放二叉链表中的所有节点 bool Root(T &x)const;/获取根的元素 void MakeTree(const T &x,BinaryT

14、ree &left,BinaryTree &right);/创建二叉树 void BreakTree(T &x,BinaryTree &left,BinaryTree &right);/删除二叉树 void PreOrder(void (*Visit)(T &x);/先序遍历 void InOrder(void (*Visit)(T &x);/中序遍历 void PostOrder(void (*Visit)(T &x);/后序遍历 void LayerOrder();/自上到下、从左到右的层次遍历 int High();/求二叉树的高度 int Leaves();/求二叉树中叶子结点的数目

15、void Exchange();/交换二叉树的左右子数 void Copy(BinaryTree &p);/二叉树的复制(复制给p)protected: BTNode *root;private: void Clear(BTNode* &t); void PreOrder(void (*Visit)(T &x),BTNode *t); void InOrder(void (*Visit)(T &x),BTNode *t); void PostOrder(void (*Visit)(T &x),BTNode *t); int High2(BTNode *p); void Leaf(BTNode

16、*p,int &a); BTNode* Copy(BTNode *t); void Change(BTNode *t);templatebool BinaryTree:Root(T &x)const if(root) x=root-element; return true; else return false; templatevoid BinaryTree:MakeTree(const T &x,BinaryTree &left,BinaryTree &right) if(root|&left=&right) return; root=new BTNode(x,left.root,right

17、.root); left.root=right.root=NULL;templatevoid BinaryTree:BreakTree(T &x,BinaryTree &left,BinaryTree &right) if(!root|&left=&right|left.root|right.root) return; x=root-element; left.root=root-lChild; right.root=root-rChild; delete root; root=NULL;templatevoid Visit(T &x) coutx;templatevoid BinaryTre

18、e:PreOrder(void (*Visit)(T &x) PreOrder(Visit,root);templatevoid BinaryTree:PreOrder(void (*Visit)(T &x),BTNode *t) if(t) Visit(t-element); PreOrder(Visit,t-lChild); PreOrder(Visit,t-rChild); templatevoid BinaryTree:InOrder(void (*Visit)(T &x) InOrder(Visit,root);templatevoid BinaryTree:InOrder(void

19、 (*Visit)(T &x),BTNode *t) if(t) InOrder(Visit,t-lChild); Visit(t-element); InOrder(Visit,t-rChild); templatevoid BinaryTree:PostOrder(void (*Visit)(T &x) PostOrder(Visit,root);templatevoid BinaryTree:PostOrder(void (*Visit)(T &x),BTNode *t) if(t) PostOrder(Visit,t-lChild); PostOrder(Visit,t-rChild)

20、; Visit(t-element); templateint BinaryTree:High() int h=High2(root); return h;templateint BinaryTree:High2(BTNode *p) if(!p) return 0; int h1,h2; h1=High2(p-lChild); h2=High2(p-rChild); if(h1h2) return h1+1; else return h2+1; templateint BinaryTree:Leaves() int number=0; Leaf(root,number); return nu

21、mber;templatevoid BinaryTree:Leaf(BTNode *p,int &a) if(p) if(p-lChild=0&p-rChild=0) a+; Leaf(p-lChild,a); Leaf(p-rChild,a); templatevoid BinaryTree:Copy(BinaryTree &p) BTNode *q=Copy(root); p.root=q;templateBTNode* BinaryTree:Copy(BTNode *t) if(!t) return NULL; BTNode *q=new BTNode(t-element); q-lChild=Copy(t-lChild); q-rChild=Copy(t-rChild); return q;templatevoid BinaryTree:Exchange() Change(root);templatevoid BinaryTree:Change(BTNode *t) if(t)

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

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