第一范文网 - 专业文章范例文档资料分享平台

数据结构(C语言版)实验报告(哈夫曼树)

来源:用户分享 时间:2025/5/18 4:43:31 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

编码结果被存储在CodeFile.txt文件里

通过打开文件进行译码

译码结果被存储

通过打开encode.txt文件进行编码

编码结果同时被存储

通过自行输入文件进行译码

译码同时被存储 退出

七、 附录(源代码)

#include #include #include using namespace std; ofstream outstuf;

typedef struct{

unsigned int weight;

unsigned int parent,lchild,rchild; }HTNode,*HuffmanTree;

typedef char** HuffmanCode;

HuffmanTree HT=NULL; int n=0;

HuffmanCode HC=NULL; char *ch=NULL;

void initHuffmanTree(); int openfileInit(); int inputInit();

int HuffmanCoding(int *w); void Select(int j,int &s1,int &s2); void encoding(); void openfileEnco(); void inputEnco(); void decode();

void openfileDeco(); void inputDeco();

void dispHT( HuffmanTree nodeRoot, int level );

void initHuffmanTree(){ //选择初始化哈夫曼树 int sel=0; for(;;){

cout<<\ cout<<\ \字符集及权值来源\\t\\t\\t\\t\\t*\

cout<<\使用权值文件data.txt进行编码\\t\\t\\t*\ cout<<\自行输入字符集及权值\\t\\t\\t\\t*\ cout<<\返回上层\\t\\t\\t\\t\\t*\

cout<<\ cout<<\请输入您的选择\ \

cin>>sel;

if(sel==3) break; switch(sel)

{case 1:openfileInit();break; case 2:inputInit();break;

default:cout<<\对不起,您输入的数据有误!请重新输入。\ } }; }

int openfileInit(){ //通过打开的data.txt文件初始化哈夫曼树该文件是为了测试数据2 包涵26个字符 int *w=(int*)malloc(28*sizeof(int)); ch=(char*)malloc(28*sizeof(char)); n=27;

ifstream infile(\ if(!infile){

cerr<<\ exit(1); }

cout<<\ 权值文件中的信息(#代表空格)\ for(int i=1;infile.eof()==0;i++){ infile>>ch[i];

infile>>w[i]; } cout<

cout<<\ 字符:\

for(i=1;i<10;i++) cout<

for(i=1;i<10;i++) cout<

for(i=10;i<19;i++) cout<

for(i=10;i<19;i++) cout<

for(i=19;i<28;i++) cout<

for(i=19;i<28;i++) cout<

HuffmanCoding(w);

cout<<\ 各字符编码如下:\ for(i=1;i<=27;i++) {

数据结构(C语言版)实验报告(哈夫曼树).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c9crjj4ef0d6c4ro7pooy_5.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top