实验报告2_二叉树及哈夫曼编码_第1页
实验报告2_二叉树及哈夫曼编码_第2页
实验报告2_二叉树及哈夫曼编码_第3页
实验报告2_二叉树及哈夫曼编码_第4页
实验报告2_二叉树及哈夫曼编码_第5页
已阅读5页,还剩25页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实 验 报 告( 2013/ 2014 学年 第 二 学期)课程名称数据结构A 实验名称实验二 二叉树的基本操作及哈夫曼编码译码系统的实现实验时间2014年4月8日指导单位计算机学院计算机软件教学中心指导教师朱立华学生姓名高怡馨班级学号B12140113学院(系)教育科学与技术学院专 业教育技术学实 验 报 告实验名称 实验二 二叉树的基本操作及哈夫曼编码译码系统的实现指导教师朱立华实验类型 设计实验学时 2实验时间2014.4.8一、 实验目的和要求(1)掌握二叉链表上实现二叉树基本去处的方法。(2)学会设计基于遍历的求解二叉树应用问题的递归算法。(3)理解哈夫曼树的构造算法,学习设计哈夫曼

2、编码和译码系统。二、实验环境(实验设备) 硬件: 微型计算机 软件: Microsoft Visual C+6.0三、实验原理及内容 实验题一:在二叉链表上实现二叉树运算(1)设计递归算法,实现下列二叉树运算:删除一棵二叉树、求一棵二叉树的高度、求一棵二叉树中叶子结点数、复制一棵二叉树、交换一棵二叉树的左右子树。(2)设计算法,按自上到下,从左到右的顺序,按层次遍历一棵二叉树。(3)设计main函数,测试上述每个运算。内容:1、建立头文件BTree.H,在该文件中定义二叉树的链式存储结构,并编写二叉树的各种基本操作实现函数。同时建立一个验证操作实现的主函数文件Test.cpp,编译并调试程序,

3、直到正确运行。注意:需要用到队列的有关操作。实 验 报 告说明:二叉树的基本操作可包括:(1)void Clear(BTreeNode *BT) /清除一棵二叉树,并释放结点空间(2)void MakeTree(BTreeNode *BT) /构造一棵二叉树BT(3)void BreakTree(BTreeNode *BT) /撤销一棵二叉树BT(4)void PreOrder (BTreeNode *BT) /先序遍历二叉树BT(5)void InOrder(BTreeNode *BT) /中序遍历二叉树BT(6)void PostOrder(BTreeNode *BT) /后序遍历二叉树B

4、T (7) void FloorOrder(BTreeNode *BT) /层次遍历二叉树BT (8)int Size(BTreeNode *BT) /求二叉树BT的结点数量(9)void Exchange(BTreeNode *BT) /把二叉树BT的左右子树进行交换(10)int GetHeight(BTreeNode *BT) /求二叉树BT的高度(一)概要设计1.流程图及设计思想用maketree构造一棵二叉树建立二叉树树删除二叉树先删除左子树,再删除右子树,最后删除根节点,再释放结点空间左右子树高度之和进行比较,谁大谁就是树的高度树的高度本质就是交换每个结点的左右子树左右交换用队列实

5、现,将跟结点入队。一:出队并输出节点的值。二: 若存在左右孩子,则将其入队。循环以上两个步骤,直到队列为空。运用后序遍历思想,把树分解为左右子树和跟结点节点数量层次遍历二叉树根据二叉树的定义和遍历算法的定义,和很容易实现递归遍历二叉树实 验 报 告2.本程序包含的模块(1)主程序模块 Void main() 初始化; 构造一棵二叉树; 各种遍历二叉树; 对二叉树进行各种常见运算; 删除二叉树; (2) 二叉树模块实现二叉树的抽象数据类型和基本操作(3) 队列模块实现队列的抽象数据类(4)二叉树运算模块求二叉树的结点,叶子数目(二)详细设计一先序遍历:A自然语言描述:1.判断根节点会否为空,如果

6、为空,返回2.如果根节点不为空3.先输出根节点,再递归调用自身依次输出左孩子和右孩子B代码详细分析template <class T>void BinaryTree<T>:PreOrder(void (*Visit)(T& x)PreOrder(Visit,root);template<class T>void BinaryTree<T>:PreOrder(void (*Visit)(T& x),BTNode<T>* t)if(t)Visit(t->element);PreOrder(Visit,t->lC

7、hild);PreOrder(Visit,t->rChild);实 验 报 告二中序遍历:A自然语言描述:1.判断根节点是否为空,如果为空,返回2.如果根节点不为空3. 递归调用自身输出左孩子,再输出根结点,递归调用输出右孩子B代码详细分析:template <class T>void BinaryTree<T>:InOrder(void (*Visit)(T& x)InOrder(Visit,root);template<class T>void BinaryTree<T>:InOrder(void (*Visit)(T&

8、 x),BTNode<T>* t)if(t)InOrder(Visit,t->lChild);Visit(t->element);InOrder(Visit,t->rChild);三后序遍历:A自然语言描述:1.判断根节点是否为空,如果为空,返回2.如果根节点不为空3. 递归调用自身输出左孩子和右孩子,再输出根结点B代码详细分析:template <class T>void BinaryTree<T>:PostOrder(void (*Visit)(T& x)PostOrder(Visit,root);template <cl

9、ass T>void BinaryTree<T>:PostOrder(void (*Visit)(T& x),BTNode<T>* t)if(t)PostOrder(Visit,t->lChild);PostOrder(Visit,t->rChild);实 验 报 告Visit(t->element);四层次遍历二叉树:A自然语言描述:1定义头指针和尾指针和空指针p2.根节点是否为空,如果是,结束3.如果不是,根节点入队4. 取队首元素,输出队首元素5.将队首的非空左右结点入队列,删除队首元素6.循环下去,直到队列为空B代码详细分析:te

10、mplate <class T>void BinaryTree<T>:FloorOrder(void (*Visit)(T &x) FloorOrder(Visit,root);template <class T>void BinaryTree<T>:;FloorOrder(void(*Visit)(Visit<T>*x),BTNode<T>*t) SeqQueue<BTNode<T>*>se(100); se.EnQueue(t); BTNode<T>*temp; while(

11、!se.IsEmpty() se.Front(temp); Visit(temp); se.DeQueue(); if(temp->lchild) se.EnQueue(temp->lchild); if(temp->child) se.EnQueue(temp->rchild); 五求二叉树的结点数:A. 自然语言描述:1:判断根节点是否为空,如果为空,返回02:如果根节点不为空 3:递归调用求二叉树的结点数的函数,参数改为根节点的左孩子 4:递归调用求二叉树的结点数的函数,参数改为根节点的右孩子实 验 报 告5:返回根节点的左右字数的结点数之和B代码详细分析:tem

12、plate <class T>int BinaryTree<T>:Size()return Size(root);template <class T>int BinaryTree<T>:Size(BTNode<T>* t)if(!t)return 0;elsereturn Size(t->lChild)+Size(t->rChild)+1;六二叉树的左右交换:A自然语言描述:1.判断根节点是否为空,如果为空,返回2.如果不为空,再判断该节点左右孩子是否同时为空,3.如果是,返回4.如果不为空,交换左右孩子5.返回循环,遍历

13、左右子树B代码详细分析:template <class T>void BinaryTree<T>:Exchange()Exchange(root);template <class T>void BinaryTree<T>:Exchange(BTNode<T>* t)if(!t)return;BTNode<T>* temp;temp=t->lChild;t->lChild=t->rChild;t->rChild=temp;Exchange(t->lChild);Exchange(t->rC

14、hild);实 验 报 告七求二叉树的深度:A自然语言描述:1:判断根节点是否为空,如果根节点为空,返回0 2:如果根节点不为空但是根节点的左右孩子同时为空,返回1 3:如果以上两个条件都不成立4:递归调用求二叉树的深度,函数的参数改为根节点的左孩子,并且深度初始化为15:递归调用求二叉树的深度,函数的参数改为跟结点的右孩子,并且深度初始化为0 6:返回4与5步中得出深度较大的那个数作为二叉树的深度数B 代码详细分析:template <class T>int BinaryTree<T>:GetHeight(BTNode<T>* t)int templ;in

15、t tempr;if(!t)return 0;templ=GetHeight(t->lChild);tempr=GetHeight(t->rChild);if(templ+>tempr+)return templ;elsereturn tempr;测试结果实 验 报 告二叉树基本运算源代码:BTree.h:#include <iostream>using namespace std;template <class T>struct BTNodeT element;BTNode<T> *lChild,*rChild;BTNode()lChil

16、d=rChild=NULL;BTNode(const T& x)element=x;lChild=rChild=NULL;BTNode(const T& x,BTNode<T> *l,BTNode<T> *r)element=x;lChild=l;rChild=r;template <class T>class BinaryTreepublic:BinaryTree()root=NULL;BinaryTree()Clear(root);bool IsEmpty()const;void Clear();bool Root(T &x)co

17、nst;int Size();void MakeTree(const T &e,BinaryTree<T>& left,BinaryTree<T>& right);void BreakTree(T &e,BinaryTree<T>& left,BinaryTree<T>& right);void LevelOrder(void (*Visit(T& x);void PreOrder(void (*Visit)(T& x);void InOrder(void (*Visit)(T&am

18、p; x);void PostOrder(void (*Visit)(T& x);实 验 报 告void Exchange();int GetHeight();protected:BTNode<T>* root;private:void Clear(BTNode<T>* t);int Size(BTNode<T>* t);void LevelOrder(void (*Visit)(T& x),BTNode<T>* t);void PreOrder(void (*Visit)(T& x),BTNode<T>* t

19、);void InOrder(void (*Visit)(T& x),BTNode<T>* t);void PostOrder(void (*Visit)(T& x),BTNode<T>* t);void Exchange(BTNode<T>* t);int GetHeight(BTNode<T>* t);template <class T>void BinaryTree<T>:Clear(BTNode<T>* t)if(!t)return;Clear(t->lChild);Clear(t

20、->rChild);delete t;template <class T>bool BinaryTree<T>:Root(T &x)constif(root)x=root->element;return true;elsereturn false;template <class T>void BinaryTree<T>:MakeTree(const T &e,BinaryTree<T>& left,BinaryTree<T>& right)if(root|&left=&a

21、mp;right)return;root=new BTNode<T>(e,left.root,right.root);实 验 报 告left.root=right.root=NULL;template <class T>void BinaryTree<T>:BreakTree(T &x,BinaryTree<T>& left,BinaryTree<T>& right)if(!root|&left=&right|left.root|right.root)return;x=root->elem

22、ent;left.root=root->lChild;right.root=root->rChild;delete root;root=NULL;template <class T>void Visit(T& x)cout<<x<<" "template <class T>void BinaryTree<T>:PreOrder(void (*Visit)(T& x)PreOrder(Visit,root);template<class T>void BinaryTree<

23、;T>:PreOrder(void (*Visit)(T& x),BTNode<T>* t)if(t)Visit(t->element);PreOrder(Visit,t->lChild);PreOrder(Visit,t->rChild);template <class T>void BinaryTree<T>:InOrder(void (*Visit)(T& x)InOrder(Visit,root);emplate<class T>void BinaryTree<T>:InOrder(vo

24、id (*Visit)(T& x),BTNode<T>* t)实 验 报 告if(t)InOrder(Visit,t->lChild);Visit(t->element);InOrder(Visit,t->rChild);template <class T>void BinaryTree<T>:PostOrder(void (*Visit)(T& x)PostOrder(Visit,root);template <class T>void BinaryTree<T>:PostOrder(void (*

25、Visit)(T& x),BTNode<T>* t)if(t)PostOrder(Visit,t->lChild);PostOrder(Visit,t->rChild);Visit(t->element);template <class T>void BinaryTree<T>:FloorOrder(void (*Visit)(T &x) FloorOrder(Visit,root);template <class T>void BinaryTree<T>:FloorOrder(void(*Visit

26、)(T& x),BTNode<T>*t) SeqQueue<BTNode<T>*>se(100); se.EnQueue(t); BTNode<T> *tmp; while(!se.IsEmpty() se.Front(tmp); Visit(tmp); se.DeQueue(); if(tmp->lChild)实 验 报 告 se.EnQueue(tmp->rChild); template <class T>int BinaryTree<T>:Size()return Size(root);temp

27、late <class T>int BinaryTree<T>:Size(BTNode<T>* t)if(!t)return 0;elsereturn Size(t->lChild)+Size(t->rChild)+1;template <class T>void BinaryTree<T>:Exchange()Exchange(root);template <class T>void BinaryTree<T>:Exchange(BTNode<T>* t)if(!t)return;BT

28、Node<T>* temp;temp=t->lChild;t->lChild=t->rChild;t->rChild=temp;Exchange(t->lChild);Exchange(t->rChild);template <class T>int BinaryTree<T>:GetHeight()return GetHeight(root);emplate <class T>int BinaryTree<T>:GetHeight(BTNode<T>* t)实 验 报 告int tem

29、pl;int tempr;if(!t)return 0;templ=GetHeight(t->lChild);tempr=GetHeight(t->rChild);if(templ+>tempr+)return templ;elsereturn tempr;Test.Cpp:#include "BTree.h"int main() BinaryTree<char> a,b,x,y,z;y.MakeTree('E',a,b);z.MakeTree('F',a,b);x.MakeTree('C',y,

30、z);y.MakeTree('D',a,b);z.MakeTree('B',y,x);cout<<"前序遍历"<<endl;z.PreOrder(Visit);cout<<endl;cout<<"中序遍历"<<endl;z.InOrder(Visit);cout<<endl;cout<<"后序遍历"<<endl;z.PostOrder(Visit);cout<<endl;cout<<&

31、quot;层次遍历"<<endl;z.LevelOrder(Visit);cout<<endl;cout<<"结点数量:"cout<<z.Size()<<endl;z.Exchange();cout<<"左右交换后的前序遍历"<<endl;实 验 报 告z.PreOrder(Visit);cout<<"树的高度:"cout<<z.GetHeight()<<endl;cout<<endl;retu

32、rn 0;实 验 报 告实验题二:哈夫曼编码和译码系统(1)所设计的系统重复显示以下菜单项:B建树:读入字符集和各字符频度,建立哈夫曼树。T遍历:先序和中序遍历二叉树。E生成编码:根据已建成的哈夫曼树,产生各字符的哈夫曼编码。C编码:输入由字符集中字符组成的任意字符串,利用已生成的哈夫曼编码进行编码,显示编码结果,并将输入的字符串及其编码结果分别保存在磁盘文件textfile.txt和codefile.txt中。D译码:读入codefile.txt,利用已建成的哈夫曼树进行译码,并将译码结果存入磁盘文件result.txt中。P打印:屏幕显示文件textfile.txt、codefile.tx

33、t和result.txt。X退出。源代码#include <iostream>#include <cstdlib>#include <iostream>#include <queue>#include <string>using namespace std;int *weightArray;string s;string *codeArray;template <class T>struct BTNode T element;BTNode<T> *lChild, *rChild;BTNode() lChild

34、= rChild = NULL; BTNode(const T& x) element = x;lChild = rChild = NULL;BTNode(const T& x, BTNode<T>* l, BTNode<T>* r) element = x;lChild = l;rChild = r;实 验 报 告template<class T>class BinaryTree public:BinaryTree() root = NULL;BinaryTree() bool isEmpty() const return root = N

35、ULL;void clear() postClear(root);bool retRoot(T& x) const;void makeTree(const& x, BinaryTree<T>& left, BinaryTree<T>& right);void breakTree(T& x, BinaryTree<T>& left, BinaryTree<T>& right);void preOrder() preOrder(root);void inOrder() inOrder(root)

36、;void postOrder() postOrder(root);BTNode<T>* copy(BTNode<T> *t);int size() return size(root);void change() change(root);void breathFirst() breathFirst(root);int height() return height(root);void leaf() prePrint(root);实 验 报 告protected:BTNode<T>* root;private:void clear(BTNode<T&g

37、t;* t);void change(BTNode<T>* t);void postClear(BTNode<T>* t);void prePrint(BTNode<T>* t);int size(BTNode<T>* t);int height(BTNode<T>* t);void preOrder(BTNode<T>* t);void inOrder(BTNode<T>* t);void postOrder(BTNode<T>* t);void breathFirst(BTNode<T&g

38、t;* t);void visit(T& x) cout << x << " "template <class T> bool BinaryTree<T>:retRoot(T& x) const if (root) x = root -> element;return true;else return false;template <class T> void BinaryTree<T>:makeTree(const& x, BinaryTree<T>&

39、 left, BinaryTree<T>& right) if (root | &left = &right) return;root = new BTNode<T>(x, left.root, right.root);left.root = right.root = NULL;template <class T> void BinaryTree<T>:breakTree(T& x, BinaryTree<T>& left, BinaryTree<T>& right) if

40、(!root | &left = &right | left.root | right.root) return;x = root -> element;left.root = root -> lChild;right.root = root -> rChild;delete root;root = NULL;实 验 报 告template <class T>void BinaryTree<T>:preOrder(BTNode<T>* t) if (t) visit(t -> element);preOrder(t -

41、> lChild);preOrder(t -> rChild);template <class T>void BinaryTree<T>:inOrder(BTNode<T>* t) if (t) inOrder(t -> lChild);visit(t -> element);inOrder(t -> rChild);template <class T>void BinaryTree<T>:postOrder(BTNode<T>* t) if (t) postOrder(t -> lCh

42、ild);postOrder(t -> rChild);visit(t -> element);template <class T>void BinaryTree<T>:clear(BTNode<T>* t) delete t;t = NULL;template <class T>void BinaryTree<T>:postClear(BTNode<T>* t) if (t) postClear(t -> lChild);postClear(t -> rChild);delete t;实 验 报

43、告template <class T>BTNode<T>* BinaryTree<T>:copy(BTNode<T> *t) if (!t) return NULL;BTNode<T>* q = new BTNode<T>(t ->element);q -> lChild = copy(t -> lChild);q -> rChild = copy(t -> rChild);return q;template <class T>int BinaryTree<T>:siz

44、e(BTNode<T>* t) if (!t) return 0;else return size(t -> lChild) + size(t -> rChild);template <class T>void BinaryTree<T>:change(BTNode<T>* t) if (!t) return;BTNode<T> *q = copy(t -> rChild);clear(t -> rChild);t -> rChild = t -> lChild;t -> lChild = q

45、;change(t -> lChild);change(t -> rChild);template <class T>void BinaryTree<T>:breathFirst(BTNode<T>* t) if (!t) return;queue<BTNode<T>*> q1;q1.push(t);BTNode<T> *node;while (!q1.empty() node = q1.front();visit(node -> element);q1.pop();if (node -> lChi

46、ld) q1.push(node -> lChild);if (node-> rChild) q1.push(node -> rChild);template <class T>int BinaryTree<T>:height(BTNode<T>* t) 实 验 报 告 if(t = NULL) return 0; else int m = height( t -> lChild ); int n = height(t -> rChild); return (m > n) ? (m + 1) : (n + 1); temp

47、late <class T>void BinaryTree<T>:prePrint(BTNode<T>* t) if (t) if (t -> lChild = NULL) && (t -> rChild = NULL) visit(t -> element);return;prePrint(t -> lChild);prePrint(t -> rChild);template<class T>class PrioQueue public:PrioQueue(int mSize=20);PrioQue

48、ue()delete q;bool IsEmpty() constreturn n=0;bool IsFull() constreturn n=maxSize;void Append(const T &x);void Serve(T& x);private:void AdjustDown (int r, int j);void AdjustUp (int j);T* q;int n,maxSize;template <class T>PrioQueue<T>:PrioQueue(int mSize) maxSize=mSize; n=0;实 验 报 告q

49、=new TmaxSize;template <class T>void PrioQueue<T>:AdjustUp (int j) int i=j;T temp=qi;while (i>0 && temp<q(i-1)/2)qi=q(i-1)/2; i=(i-1)/2; qi=temp;template <class T>void PrioQueue<T>:Append(const T &x) if(IsFull() cout<< "Overflow" return; qn+=

50、x; AdjustUp(n-1);template <class T>void PrioQueue<T>:Serve(T& x) if(IsEmpty() cout<< "Underflow" return; x=q0;q0=q-n; AdjustDown (0, n-1);template <class T>void PrioQueue<T>:AdjustDown (int r, int j) int child = 2 * r + 1;T temp = qr;while (child <= j)

51、if (child < j) && (qchild > qchild + 1) child+;if (temp <= qchild) break;q(child - 1) / 2 = qchild;child = 2 * child + 1;实 验 报 告q(child - 1) / 2 = temp;template <class T>class HfmTree: public BinaryTree<T>public:operator T()const return weight;T getW()return weight;void

52、putW(const T& x) weight=x; void SetNull()root=NULL; void code(string& c) code(root, c);void decode(string s);private: T weight; void code(BTNode<T>* t, string& c);template <class T>void HfmTree<T>:decode(string decodeString) if (codeArray = NULL) cout << "尚未编

53、码!" << endl;return;BTNode<T>* searchNode = root;for (int i = 0; i < decodeString.length(); i+) if (decodeStringi != '0' && decodeStringi != '1') cout << "所给码格式不正确!" << endl;return;if (searchNode -> lChild = NULL && searchNo

54、de -> rChild = NULL) T value = searchNode -> element;for (int j = 0; j < s.length(); j+) if (value = weightArrayj) cout << sj;break;实 验 报 告searchNode = root;if (decodeStringi = '0') searchNode = searchNode -> lChild;if (decodeStringi = '1') searchNode = searchNode -

55、> rChild;if (searchNode -> lChild = NULL && searchNode -> rChild = NULL) T value = searchNode -> element;for (int j = 0; j < s.length(); j+) if (value = weightArrayj) cout << sj;break;cout << endl;template <class T>void HfmTree<T>:code(BTNode<T>*

56、t, string& c) if (t) if (t -> lChild = NULL && t -> rChild = NULL) for (int i = 0; i < s.length(); i+) if (t -> element = weightArrayi) codeArrayi = c;/cout << "NO" << i << " "cout << "字符" << si << "的权重是" << weightArrayi << ", 哈弗曼编码是" << codeArrayi << endl; break;if (t -> lChild != NULL) string ls;ls.assign(c);ls.append("0");code(t -> lChild, ls);if (t -> rChild != NULL) 实 验 报 告s

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论