数据结构实验报告图的基本运算及飞机换乘次数最少问题.docx
- 文档编号:10891293
- 上传时间:2023-02-23
- 格式:DOCX
- 页数:28
- 大小:71.56KB
数据结构实验报告图的基本运算及飞机换乘次数最少问题.docx
《数据结构实验报告图的基本运算及飞机换乘次数最少问题.docx》由会员分享,可在线阅读,更多相关《数据结构实验报告图的基本运算及飞机换乘次数最少问题.docx(28页珍藏版)》请在冰豆网上搜索。
数据结构实验报告图的基本运算及飞机换乘次数最少问题
实验报告
实验名称:
二叉树的基本操作及哈夫曼编码译码系统的实现
一、问题描述
1.实验目的和要求
a.创建一棵二叉树,实现先序、中序和后序遍历一棵二叉树,计算二叉树结点个数等操作。
b.哈夫曼编码/译码系统。
2.实验任务:
能成功演示二叉树的有关运算,运算完毕后能成功释放二叉树所有结点占用的系统内存
3.实验内容:
a.①创建一棵二叉树;②先序、中序和后序遍历这棵二叉树;③计算二叉树结点个数
b.哈夫曼编码译码系统
二程序设计
三.程序代码
主函数:
#include"CreateHfmTree.h"
#include
#include
usingnamespacestd;
voidmain()
{
cout<<"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< cout<<"'M'------Showthismenu."< cout<<"'B'------Builduptree: readcharactersetandfrequencyofeachcharacter,builduphfmtree."< cout<<"'T'------Tracethetree: PreOrderandInOrdertheBTree."< cout<<"'E'------Generatecode: Accordingtothebuildeduphfmtree,generatethecodesforallcharacter."< cout<<"'C'------Encode: Inputarbitarystringformedbycharacterswhichhavebeengeneratedcode,utilizethecodestoecodeandprinttheresultofencoding.(endwith'#')"< cout<<"'D'------Translate: readcodefile.txt,utilizetheexisitinghfmtreetotranslatecodeandrestorecodeIntohardwarefileresult.txt."< cout<<"'P'------Print: Printthecontentsoffile: textfile.txt,codefile.txt,result.txtonthescreen."< cout<<"'X'------Exit: Exitthissystem."< cout<<"'-'------Delete: Deletecharacterset,characterfrequencies,codesandHfmTreeiftheyareexist."< intw[100];chardata[100]; intn;charchoice;inti,j; HfmTree HfmNode*ht; HfmCode*hc; chars[1000]; repeat1: cout< cout<<"Pleaseinputyourchioce: ";cin>>choice; cout< if(choice=='b'||choice=='B') { cout<<"Pleaseinputthenumberofelementarycode: ";cin>>n; cout<<"---------------------------------------------------------------"< cout<<"Allocatingthememory..."< cout<<"Allocatingthecomplete."< cout<<"---------------------------------------------------------------"< cout<<"Pleaseinputalltheelementarycodes: "< for(inti=0;i cout<<"PleaseinputalltheFrenquencies: "< for(i=0;i hfm=CreateHfmTree(w,data,4);gotorepeat1; } if(choice=='t'||choice=='T') { cout<<"hfm";hfm.PreOrder(Visit); cout<<"hfm";hfm.InOrder(Visit); gotorepeat1; } if(choice=='e'||choice=='E') { cout<<"Generatingcode..."< ht=newHfmNode[2*n-1]; hc=newHfmCode[n]; hfm.Grcode(w,data,n,ht,hc); for(i=0;i { cout<<"'"< "; for(j=hc[i].start+1;j cout< cout< } cout<<"CodeGeneratecomplete."< gotorepeat1; } if(choice=='c'||choice=='C') { cout<<"Pleaseinputthearticlethatyouwanttocode: "< charch;inti=0; while (1) { cin>>ch; s[i++]=ch; if(ch=='#') break; } intlength=i; ofstreamoutf("testfile.txt"); if(! outf) { cout<<"Can'tOpenthefile! ";return; } i=0; while(s[i]! ='#') { outf.put(s[i]); i++; } outf.close(); cout< cout<<"Resultofencoding: "; hfm.Encode(ht,hc,n,"testfile.txt","codefile.txt"); cout< cout<<"Encodingcomplete."< gotorepeat1; } if(choice=='d'||choice=='D') { cout<<"Startingtranslatingcode..."< cout<<"Result: "; hfm.Decode(ht,n,"codefile.txt","resultfile.txt"); cout< gotorepeat1; } if(choice=='p'||choice=='P') { cout<<"----------------------testfile.txt----------------------"< hfm.Print("testfile.txt"); cout<<"--------------------------------------------------------"< cout< cout<<"----------------------codefile.txt----------------------"< hfm.Print("codefile.txt"); cout<<"--------------------------------------------------------"< cout< cout<<"----------------------resultfile.txt----------------------"< hfm.Print("resultfile.txt"); cout<<"--------------------------------------------------------"< cout< gotorepeat1; } if(choice=='x'||choice=='X') return; if(choice=='-') { hfm.Clear(); cout<<"DeleteAll..."< gotorepeat1; } 二叉树类: #include"BTNode.h" #include"seqqueue.h" template classBinaryTree { public: BinaryTree(){root=NULL;} boolIsEmpty()const; voidClear(); boolRoot(T&x)const; voidMakeTree(constT&x,BinaryTree voidMakeTree(constT&x,constT&y,BinaryTree voidBreakTree(T&x,BinaryTree voidPreOrder(void(*Visit)(T&x)); voidInOrder(void(*Visit)(T&x)); voidPostOrder(void(*Visit)(T&x)); voidLevalOrder(); intSize(); protected: BTNode private: voidClear(BTNode voidPreOrder(void(*Visit)(T&x),BTNode voidInOrder(void(*Visit)(T&x),BTNode voidPostOrder(void(*Visit)(T&x),BTNode voidLevalOrder(BTNode intSize(BTNode }; template boolBinaryTree : IsEmpty()const { returnroot==NULL; } template voidBinaryTree : Clear(BTNode { if(t) { if(t->lChild) Clear(t->lChild); if(t->rChild) Clear(t->rChild); deletet; t=NULL; } } template voidBinaryTree : Clear() { Clear(root); } template boolBinaryTree : Root(T&x)const { if(root) { x=root->element; returntrue; } else returnfalse; } template voidBinaryTree : MakeTree(constT&x,BinaryTree { cout<<"hereinBinaryTree : MakeTree."< if(root||&left==&right)return; root=newBTNode left.root=right.root=NULL; } template voidBinaryTree : MakeTree(constT&x,constT&y,BinaryTree { if(root||&left==&right)return; root=newBTNode left.root=right.root=NULL; } template voidBinaryTree : BreakTree(T&x,BinaryTree { cout<<"hereinBinaryTree : BreakTree."< if(! root||&left==&right||left.root||right.root)return; x=root->element; left.root=root->lChild;right.root=root->rChild; deleteroot;root=NULL; } template voidVisit(T&x) { cout< } template voidBinaryTree : PreOrder(void(*Visit)(T&x)) { cout<<"先序遍历为: "; PreOrder(Visit,root); cout< } template voidBinaryTree : PreOrder(void(*Visit)(T&x),BTNode { if(t) { Visit(t->element); PreOrder(Visit,t->lChild); PreOrder(Visit,t->rChild); } } template voidBinaryTree : InOrder(void(*Visit)(T&x)) { cout<<"中序遍历为: "; InOrder(Visit,root); cout< } template voidBinaryTree : InOrder(void(*Visit)(T&x),BTNode { if(t) { InOrder(Visit,t->lChild); Visit(t->element); InOrder(Visit,t->rChild); } } template voidBinaryTree : PostOrder(void(*Visit)(T&x)) { cout<<"后序遍历为: "; PostOrder(Visit,root); cout< } template voidBinaryTree : PostOrder(void(*Visit)(T&x),BTNode { if(t) { PostOrder(Visit,t->lChild); PostOrder(Visit,t->rChild); Visit(t->element); } } template voidBinaryTree : LevalOrder(BTNode { SeqQueue if(! t)return; q.EnQueue(t); while(! q.IsEmpty()) { t=q.DeQueue(); cout< if(t->lChild)q.EnQueue(t->lChild); if(t->rChild)q.EnQueue(t->rChild); } cout< } template voidBinaryTree : LevalOrder() { LevalOrder(root); } template intBinaryTree : Size(BTNode { if(! t)return0; else returnSize(t->lChild)+Size(t->lChild)+1; } template intBinaryTree : Size() { returnSize(root); } 二叉树结点类: #include template structBTNode { BTNode(){lChild=rChild=NULL;} BTNode(constT&x) { element=x;lChild=rChild=NULL; } BTNode(constT&x,BTNode { element=x;lChild=l;rChild=r; } BTNode(constT&x,constT&y,BTNode { element=x;word=y;lChild=l;rChild=r; } Telement; Tword; BTNode }; 哈夫曼树类: #include"BinaryTree.h" #include structHfmNode { charword; intweight; intparent; intlchild; intrchild; }; structHfmCode { intbit[10000]; intstart; intweight; }; template classHfmTree: publicBinaryTree { public: operatorT()const{returnweight;} TgetW(){returnweight;} voidputW(constT&x){weight=x;} voidSetNull(){root=NULL;} voidGrcode(Tw[],charc[],intn,HfmNodeht[],HfmCodehc[]); voidEncode(HfmNodeht[],HfmCodehc[],intcount,char*record,char*tranvers); voidDecode(HfmNodeht[],intcount,char*in,char*fout); voidPrint(char*in); private: Tweight; }; template voidHfmTree : Grcode(Tw[],charc[],intn,HfmNodeht[],HfmCodehc[]) { intm1,m2,x1,x2; chard1,d2; inti,j; for(i=0;i<2*n-1;i++) { if(i { ht[i].weight=w[i]; ht[i].word=c[i]; } else { ht[i].weight=0; ht[i].word='#'; } ht[i].parent=0; ht[i].lchild=-1; ht[i].rchild=-1; } for(i=0;i { m1=m2=1000; x1=x2=0; d1=d2='#'; for(j=0;j { if(ht[j].weight { m2=m1;m1=ht[j].weight; d2=d1;d1=ht[j].word; x2=x1;x1=j; } elseif(ht[j].weight { m2=ht[j].weight; d2=ht[j].word; x2=j; } } ht[x1].parent=n+i; ht[x2].parent=n+i; ht[n+i].weight=ht[x1].weight+ht[x2].weight; ht[n+i].lchild=x1; ht[n+i].rchild=x2; } HfmCodecd; intchild,parent;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 实验 报告 基本 运算 飞机 换乘 次数 最少 问题