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