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

上传人:b****4 文档编号:6860273 上传时间:2023-05-07 格式:DOCX 页数:28 大小:71.56KB
下载 相关 举报
数据结构实验报告图的基本运算及飞机换乘次数最少问题Word下载.docx_第1页
第1页 / 共28页
数据结构实验报告图的基本运算及飞机换乘次数最少问题Word下载.docx_第2页
第2页 / 共28页
数据结构实验报告图的基本运算及飞机换乘次数最少问题Word下载.docx_第3页
第3页 / 共28页
数据结构实验报告图的基本运算及飞机换乘次数最少问题Word下载.docx_第4页
第4页 / 共28页
数据结构实验报告图的基本运算及飞机换乘次数最少问题Word下载.docx_第5页
第5页 / 共28页
数据结构实验报告图的基本运算及飞机换乘次数最少问题Word下载.docx_第6页
第6页 / 共28页
数据结构实验报告图的基本运算及飞机换乘次数最少问题Word下载.docx_第7页
第7页 / 共28页
数据结构实验报告图的基本运算及飞机换乘次数最少问题Word下载.docx_第8页
第8页 / 共28页
数据结构实验报告图的基本运算及飞机换乘次数最少问题Word下载.docx_第9页
第9页 / 共28页
数据结构实验报告图的基本运算及飞机换乘次数最少问题Word下载.docx_第10页
第10页 / 共28页
数据结构实验报告图的基本运算及飞机换乘次数最少问题Word下载.docx_第11页
第11页 / 共28页
数据结构实验报告图的基本运算及飞机换乘次数最少问题Word下载.docx_第12页
第12页 / 共28页
数据结构实验报告图的基本运算及飞机换乘次数最少问题Word下载.docx_第13页
第13页 / 共28页
数据结构实验报告图的基本运算及飞机换乘次数最少问题Word下载.docx_第14页
第14页 / 共28页
数据结构实验报告图的基本运算及飞机换乘次数最少问题Word下载.docx_第15页
第15页 / 共28页
数据结构实验报告图的基本运算及飞机换乘次数最少问题Word下载.docx_第16页
第16页 / 共28页
数据结构实验报告图的基本运算及飞机换乘次数最少问题Word下载.docx_第17页
第17页 / 共28页
数据结构实验报告图的基本运算及飞机换乘次数最少问题Word下载.docx_第18页
第18页 / 共28页
数据结构实验报告图的基本运算及飞机换乘次数最少问题Word下载.docx_第19页
第19页 / 共28页
数据结构实验报告图的基本运算及飞机换乘次数最少问题Word下载.docx_第20页
第20页 / 共28页
亲,该文档总共28页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

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

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

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

T'

------Tracethetree:

PreOrderandInOrdertheBTree."

E'

------Generatecode:

Accordingtothebuildeduphfmtree,generatethecodesforallcharacter."

C'

------Encode:

Inputarbitarystringformedbycharacterswhichhavebeengeneratedcode,utilizethecodestoecodeandprinttheresultofencoding.(endwith'

#'

)"

D'

------Translate:

readcodefile.txt,utilizetheexisitinghfmtreetotranslatecodeandrestorecodeIntohardwarefileresult.txt."

P'

------Print:

Printthecontentsoffile:

textfile.txt,codefile.txt,result.txtonthescreen."

X'

------Exit:

Exitthissystem."

-'

------Delete:

Deletecharacterset,characterfrequencies,codesandHfmTreeiftheyareexist."

intw[100];

chardata[100];

intn;

charchoice;

inti,j;

HfmTree<

int>

hfm;

HfmNode*ht;

HfmCode*hc;

chars[1000];

repeat1:

Pleaseinputyourchioce:

"

;

cin>

choice;

if(choice=='

b'

||choice=='

{

Pleaseinputthenumberofelementarycode:

n;

cout<

---------------------------------------------------------------"

Allocatingthememory..."

Allocatingthecomplete."

Pleaseinputalltheelementarycodes:

for(inti=0;

i<

i++)cin>

data[i];

PleaseinputalltheFrenquencies:

for(i=0;

w[i];

hfm=CreateHfmTree(w,data,4);

gotorepeat1;

}

t'

hfm"

hfm.PreOrder(Visit);

hfm.InOrder(Visit);

e'

Generatingcode..."

ht=newHfmNode[2*n-1];

hc=newHfmCode[n];

hfm.Grcode(w,data,n,ht,hc);

for(i=0;

i++)

{

ht[i].word<

:

for(j=hc[i].start+1;

j<

j++)

hc[i].bit[j];

}

CodeGeneratecomplete."

gotorepeat1;

c'

Pleaseinputthearticlethatyouwanttocode:

charch;

inti=0;

while

(1)

{

cin>

ch;

s[i++]=ch;

if(ch=='

break;

}

intlength=i;

ofstreamoutf("

testfile.txt"

);

if(!

outf)

Can'

tOpenthefile!

return;

i=0;

while(s[i]!

='

outf.put(s[i]);

i++;

outf.close();

Resultofencoding:

hfm.Encode(ht,hc,n,"

"

codefile.txt"

Encodingcomplete."

gotorepeat1;

d'

Startingtranslatingcode..."

Result:

hfm.Decode(ht,n,"

resultfile.txt"

p'

----------------------testfile.txt----------------------"

hfm.Print("

--------------------------------------------------------"

----------------------codefile.txt----------------------"

----------------------resultfile.txt----------------------"

x'

return;

hfm.Clear();

DeleteAll..."

二叉树类:

BTNode.h"

seqqueue.h"

template<

classT>

classBinaryTree

public:

BinaryTree(){root=NULL;

}

boolIsEmpty()const;

voidClear();

boolRoot(T&

x)const;

voidMakeTree(constT&

x,BinaryTree<

T>

&

left,BinaryTree<

right);

x,constT&

y,BinaryTree<

voidBreakTree(T&

voidPreOrder(void(*Visit)(T&

x));

voidInOrder(void(*Visit)(T&

voidPostOrder(void(*Visit)(T&

voidLevalOrder();

intSize();

protected:

BTNode<

*root;

private:

voidClear(BTNode<

*t);

x),BTNode<

*T);

voidLevalOrder(BTNode<

intSize(BTNode<

};

boolBinaryTree<

IsEmpty()const

returnroot==NULL;

}

voidBinaryTree<

Clear(BTNode<

*t)

if(t)

if(t->

lChild)

Clear(t->

lChild);

rChild)

rChild);

deletet;

t=NULL;

Clear()

Clear(root);

template<

Root(T&

x)const

if(root)

x=root->

element;

returntrue;

else

returnfalse;

MakeTree(constT&

x,BinaryTree<

left,BinaryTree<

right)

hereinBinaryTree<

MakeTree."

if(root||&

left==&

right)return;

root=newBTNode<

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

left.root=right.root=NULL;

MakeTree(constT&

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

BreakTree(T&

x,BinaryTree<

left,BinaryTree<

right)

BreakTree."

root||&

left==&

right||left.root||right.root)return;

x=root->

left.root=root->

lChild;

right.root=root->

rChild;

deleteroot;

root=NULL;

voidVisit(T&

x)

x<

PreOrder(void(*Visit)(T&

x))

{

先序遍历为:

PreOrder(Visit,root);

PreOrder(void(*Visit)(T&

*t)

if(t)

Visit(t->

element);

PreOrder(Visit,t->

InOrder(void(*Visit)(T&

中序遍历为:

InOrder(Visit,root);

InOrder(void(*Visit)(T&

if(t)

InOrder(Visit,t->

PostOrder(void(*Visit)(T&

后序遍历为:

PostOrder(Visit,root);

PostOrder(Visit,t->

PostOrder(Visit,t->

LevalOrder(BTNode<

SeqQueue<

BTNode<

*>

q(100);

t)return;

q.EnQueue(t);

while(!

q.IsEmpty())

t=q.DeQueue();

t->

element<

if(t->

lChild)q.EnQueue(t->

rChild)q.EnQueue(t->

}

LevalOrder()

LevalOrder(root);

intBinaryTree<

Size(BTNode<

t)return0;

else

returnSize(t->

lChild)+Size(t->

lChild)+1;

Size()

returnSize(root);

二叉树结点类:

iostream.h>

structBTNode

BTNode(){lChild=rChild=NULL;

BTNode(constT&

element=x;

lChild=rChild=NULL;

x,BTNode<

*l,BTNode<

*r)

lChild=l;

rChild=r;

BTNode(constT&

y,BTNode<

word=y;

Telement;

Tword;

BTNode<

*lChild,*rChild;

哈夫曼树类:

BinaryTree.h"

structHfmNode

charword;

intweight;

intparent;

intlchild;

intrchild;

structHfmCode

intbit[10000];

intstart;

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;

voidHfmTree<

Grcode(Tw[],charc[],intn,HfmNodeht[],HfmCodehc[])

intm1,m2,x1,x2;

chard1,d2;

2*n-1;

if(i<

n)

ht[i].weight=w[i];

ht[i].word=c[i];

ht[i].weight=0;

ht[i].word='

ht[i].parent=0;

ht[i].lchild=-1;

ht[i].rchild=-1;

n-1;

m1=m2=1000;

x1=x2=0;

d1=d2='

for(j=0;

n+i;

if(ht[j].weight<

m1&

ht[j].parent==0)

m2=m1;

m1=ht[j].weight;

d2=d1;

d1=ht[j].word;

x2=x1;

x1=j;

elseif(ht[j].weight<

m2&

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-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2