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