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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

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

1、二叉树的基本操作及哈夫曼编码译码系统的实现实 验 报 告(2014 / 2015 学年 第 二 学期)课程名称数据结构A实验名称二叉树的基本操作实验时间2015年4月23日指导单位计算机学院计算机科学与技术系指导教师学生姓名班级学号学院(系)专 业 实 验 报 告实验名称二叉树的基本操作指导教师实验类型设计实验学时2实验时间一、 实验目的和要求在二叉链表上设计和实现下列二叉树运算的算法设计递归算法,实现:删除二叉树,求二叉树的高度,求二叉树中叶子结点数,复制二叉树,交换二叉树的左右子树。设计算法,按自上到下,自左向右的次序,即按层次遍历一棵二叉树。设计main函数,测试上述每个运算。提示:二叉

2、树的按层次遍历需要利用队列作为辅助的数据结构,队列的元素类型是指向二叉树中结点的指针类型。二、实验环境(实验设备)硬件:微型计算机软件:Windows 操作系统、Microsoft Visual C+6.0三、实验原理及内容实验源代码:#include #includetemplate class Queue public: Queue(); Queue(); virtual void EnQueue(const T& x)=0; virtual void DeQueue()=0; virtual T Front()=0; virtual bool IsEmpty() const=0; vir

3、tual bool IsFull() const=0; ;template class SeqQueue:public Queue public: SeqQueue(int MaxQueSize); SeqQueue() delete q; void EnQueue(const T& x); void DeQueue(); T Front(); bool IsEmpty() const; bool IsFull() const; private: int front,rear; int MaxSize; T *q;template SeqQueue:SeqQueue(int MaxQueSiz

4、e) MaxSize=MaxQueSize; q=new TMaxSize; front=rear=0;template bool SeqQueue:IsEmpty() const return front=rear;template bool SeqQueue:IsFull() const return (rear+1) % MaxSize=front; template void SeqQueue:EnQueue(const T &x) assert(!IsFull(); q(rear=(rear+1) % MaxSize)=x;template void SeqQueue:DeQueue

5、() assert(!IsEmpty(); front=(front+1) % MaxSize;template T SeqQueue:Front() assert(!IsEmpty(); return q(front+1) % MaxSize;template class BTree;template class BTNode public: BTNode() lchild=rchild=0; BTNode( const T& e ) element=e; lchild=rchild=0; BTNode(const T& e, BTNode *l, BTNode*r) element=e;

6、lchild=l; rchild=r; private: T element; BTNode *lchild, *rchild; friend class BTree; friend void Visit(BTNode*); ;templatevoid Visit(BTNode* p) coutelement ;templateclass BTree public: BTree()root=NULL; BTree() bool IsEmpty()const; bool Root(T &x)const; void MakeTree(const T &e ,BTree& left, BTree&

7、right); void BreakTree(T &e ,BTree& left,BTree& right); void PreOrder(void (*Visit)(BTNode* u) PreOrder(Visit,root); void InOrder(void (*Visit)(BTNode* u) InOrder(Visit,root); void PostOrder(void (*Visit)(BTNode* u) PostOrder(Visit,root); void Exchange(); void LayerOrder(); int High(); int Leaves();

8、 void Deltree(); BTree CopyBTree(); private: BTNode* root; void PreOrder(void (*Visit)(BTNode*u), BTNode*t); void InOrder(void (*Visit)(BTNode* u), BTNode*t); void PostOrder(void (*Visit)(BTNode* u), BTNode*t); void Exch(BTNode *); void Leaf(BTNode*,int &); int Highs(BTNode*); ;templatebool BTree:Is

9、Empty() const return root=NULL;template /返回根节点bool BTree:Root(T &x) const if (root) x=root-element; return true; else return false;template /构造二叉树void BTree:MakeTree(const T &e, BTree& left, BTree& right) BTNode* p; p=new BTNode(e, left.root, right.root); left.root=right.root=0; root=p;template /删除二

10、叉树的所有节点void BTree:BreakTree(T &e,BTree&left, BTree& right) BTNode* p; p=root; if(p) e=p-element; left.root=p-lchild; right.root=p-rchild ; template /线序遍历二叉树void BTree:PreOrder(void (*Visit)(BTNode* u),BTNode* t) if(t) Visit(t); if(t-lchild) PreOrder(Visit,t-lchild); if(t-rchild) PreOrder(Visit,t-rch

11、ild); template / 中序遍历二叉树void BTree:InOrder (void (*Visit)(BTNode* u),BTNode* t) if (t) if(t-lchild) InOrder(Visit,t-lchild); Visit(t); if(t-rchild) InOrder(Visit,t-rchild); template /后序遍历二叉树void BTree:PostOrder(void (*Visit)(BTNode* u),BTNode* t) if (t) if(t-lchild) PostOrder(Visit,t-lchild); if(t-r

12、child) PostOrder(Visit,t-rchild); Visit(t); template /层次遍历void BTree:LayerOrder() if (root=0) coutTree is Empty!endl; return ; BTNode *p=root; BTNode x; SeqQueue BTNode sq(10); sq.EnQueue(*p); while (!sq.IsEmpty() x=sq.Front(); coutx.element ; if (x.lchild) sq.EnQueue(*x.lchild); if (x.rchild) sq.En

13、Queue(*x.rchild); sq.DeQueue(); template /求二叉树树高int BTree:High() int h=Highs(root); return h;template int BTree:Highs(BTNode* u) if (u=0) return 0; int lch,rch; lch=Highs(u-lchild); rch=Highs(u-rchild); if (lchrch) return lch+1; else return rch+1; template /求二叉树叶子节点数int BTree:Leaves() int count=0; L

14、eaf(root,count); return count;template void BTree:Leaf(BTNode* t,int& count) if (t) if (t-lchild=0)&(t-rchild=0) Visit(t); count+; Leaf(t-lchild,count); Leaf(t-rchild,count); template /删除二叉树void BTree:Deltree() if (root=NULL) return; T x; BTree left,right; BreakTree(x,left,right); left.Deltree(); ri

15、ght.Deltree(); delete(root); root=NULL;template /二叉树的复制BTree BTree:CopyBTree() if (root=NULL) BTree zero; return zero; T x; BTree chleft,chright; BTree change,changeleft,changeright; BreakTree(x,chleft,chright); changeleft=chleft.CopyBTree(); changeright=chright.CopyBTree(); change.MakeTree(x,change

16、left,changeright); return change;template /二叉树左右子树的交换void BTree:Exchange() Exch(root);template void BTree:Exch(BTNode *p) if (p!=NULL) BTNode *temp; temp=p-lchild; p-lchild=p-rchild; p-rchild=temp; Exch(p-lchild); Exch(p-rchild); void main() BTree a,b,c,d,e,f,g,h,j,k,left,right; j.MakeTree(J,left,ri

17、ght); g.MakeTree(G,left,right); f.MakeTree(F,j,g); h.MakeTree(H,left,right); e.MakeTree(E,h,f); d.MakeTree(D,e,right); k.MakeTree(K,left,right); c.MakeTree(C,k,right); b.MakeTree(B,left,c); a.MakeTree(A,d,b); coutLayerOrder:; a.LayerOrder(); coutendl; cout树A高度:a.High()endl; cout树A叶子:; coutendl树A叶子结点

18、数:a.Leaves()endl; coutendlCopy A to B:endl; coutCheck Tree B:endl; b=a.CopyBTree(); coutThe PreOrder of Tree B is:; b.PreOrder(Visit); coutendl; coutThe InOrder of Tree B is:; b.InOrder(Visit); coutendlendl; coutDelete Tree A:; a.Deltree(); coutendl; coutCheck Tree A:endl; cout树A高度:a.High()endl; cout树A叶子结点数:a.Leaves()endl; coutendlendlExchange Tree B, Successful!endl; b.Exchange(); coutCheck the result of Tree B :endl; coutLayerOrder:; b.LayerOrder(); coutendl; 实 验 报 告四、实验小结(包括问题和解决方法、心得体会、意见与建议等)五、指导教师评语成 绩批阅人日 期

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

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