数据结构实验报告图的基本运算及飞机换乘次数最少问题.docx

上传人:b****8 文档编号:10891293 上传时间:2023-02-23 格式:DOCX 页数:28 大小:71.56KB
下载 相关 举报
数据结构实验报告图的基本运算及飞机换乘次数最少问题.docx_第1页
第1页 / 共28页
数据结构实验报告图的基本运算及飞机换乘次数最少问题.docx_第2页
第2页 / 共28页
数据结构实验报告图的基本运算及飞机换乘次数最少问题.docx_第3页
第3页 / 共28页
数据结构实验报告图的基本运算及飞机换乘次数最少问题.docx_第4页
第4页 / 共28页
数据结构实验报告图的基本运算及飞机换乘次数最少问题.docx_第5页
第5页 / 共28页
点击查看更多>>
下载资源
资源描述

数据结构实验报告图的基本运算及飞机换乘次数最少问题.docx

《数据结构实验报告图的基本运算及飞机换乘次数最少问题.docx》由会员分享,可在线阅读,更多相关《数据结构实验报告图的基本运算及飞机换乘次数最少问题.docx(28页珍藏版)》请在冰豆网上搜索。

数据结构实验报告图的基本运算及飞机换乘次数最少问题.docx

数据结构实验报告图的基本运算及飞机换乘次数最少问题

实验报告

实验名称:

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

一、问题描述

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;

HfmTreehfm;

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>data[i];

cout<<"PleaseinputalltheFrenquencies:

"<

for(i=0;i>w[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&left,BinaryTree&right);

voidMakeTree(constT&x,constT&y,BinaryTree&left,BinaryTree&right);

voidBreakTree(T&x,BinaryTree&left,BinaryTree&right);

voidPreOrder(void(*Visit)(T&x));

voidInOrder(void(*Visit)(T&x));

voidPostOrder(void(*Visit)(T&x));

voidLevalOrder();

intSize();

protected:

BTNode*root;

private:

voidClear(BTNode*t);

voidPreOrder(void(*Visit)(T&x),BTNode*T);

voidInOrder(void(*Visit)(T&x),BTNode*T);

voidPostOrder(void(*Visit)(T&x),BTNode*T);

voidLevalOrder(BTNode*t);

intSize(BTNode*t);

};

template

boolBinaryTree:

:

IsEmpty()const

{

returnroot==NULL;

}

template

voidBinaryTree:

:

Clear(BTNode*t)

{

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&left,BinaryTree&right)

{

cout<<"hereinBinaryTree:

:

MakeTree."<

if(root||&left==&right)return;

root=newBTNode(x,left.root,right.root);

left.root=right.root=NULL;

}

template

voidBinaryTree:

:

MakeTree(constT&x,constT&y,BinaryTree&left,BinaryTree&right)

{

if(root||&left==&right)return;

root=newBTNode(x,y,left.root,right.root);

left.root=right.root=NULL;

}

template

voidBinaryTree:

:

BreakTree(T&x,BinaryTree&left,BinaryTree&right)

{

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*t)

{

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*t)

{

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*t)

{

if(t)

{

PostOrder(Visit,t->lChild);

PostOrder(Visit,t->rChild);

Visit(t->element);

}

}

template

voidBinaryTree:

:

LevalOrder(BTNode*t)

{

SeqQueue*>q(100);

if(!

t)return;

q.EnQueue(t);

while(!

q.IsEmpty())

{

t=q.DeQueue();

cout<element<<"";

if(t->lChild)q.EnQueue(t->lChild);

if(t->rChild)q.EnQueue(t->rChild);

}

cout<

}

template

voidBinaryTree:

:

LevalOrder()

{

LevalOrder(root);

}

template

intBinaryTree:

:

Size(BTNode*t)

{

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*l,BTNode*r)

{

element=x;lChild=l;rChild=r;

}

BTNode(constT&x,constT&y,BTNode*l,BTNode*r)

{

element=x;word=y;lChild=l;rChild=r;

}

Telement;

Tword;

BTNode*lChild,*rChild;

};

哈夫曼树类:

#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;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 财务管理

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

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