实习报告6_哈夫曼编码_第1页
实习报告6_哈夫曼编码_第2页
实习报告6_哈夫曼编码_第3页
实习报告6_哈夫曼编码_第4页
实习报告6_哈夫曼编码_第5页
已阅读5页,还剩7页未读, 继续免费阅读

下载本文档

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

文档简介

1、1.初始化:从文件humanweight.txt( 程序运行时,由用户输入 )读入字符集大小n,以及n个字符和n个权值,建立哈夫曼树,将它存于文件huftree.txt中。2.编码:利用已建好的哈夫曼树(如不在内存,则从文件huftree.txt中读入)对 文件tobebin.txt字符集中的每一个进行编码,将结果放在codefile.txt 中。3.译码:利用已建好的哈夫曼树将 codefile.txt 中的代码进行译码,结果保存在textfile.txt 中。4.印代码文件。将文件 codefile.txt 以紧凑的格式显示在终端上,每行50个代码。同时将结果保存在 codeprint.t

2、xt 中。二.概要设计:1.哈夫曼树的抽象数据类型定义: adt haffman 数据对象:d=ai|ai为charnode型的结点,i=1,2,3,n,n0 数据关系:r=|ai是d上的元素 adt haffman 2.编码集结构体的抽象数据类型的定义: adt code 数据对象:d1=ai| ai是charlink型的结点,i=1,2,n,n0 d2=bi|bi是codelink型的结点,i=1,2,n,n0 数据关系: r1=|ai是d1上的元素 r2=|bi是d2上的元素 adt code3.程序分为四个部分: 1)读入字符集以及相应频度,建立哈夫曼树。 2)根据哈夫曼树得到每一个字

3、符的哈夫曼编码。 3)读入要编码的字符串,根据哈夫曼树和编码集求出字符串的哈夫曼编码。 4)根据哈夫曼编码和哈夫曼树得到字符串。三.详细设计:/zy/ / 赫夫曼编/译码器/ 朱勇 13011090/#include #include #include #include using namespace std;/ 赫夫曼树和赫夫曼编码的存储表示typedef struct unsigned int weight;unsigned int parent , lchild , rchild; htnode , *huffmantree; / 动态分配数组存储赫夫曼树typedef struct c

4、har ch;char* hufch; huffmancode; / 动态分配数组存储赫夫曼编码表/ 权值字符结点类型typedef struct char ch;int wt; welem; / 动态分配数组存储读入字符与权值/ 赫夫曼编码函数void huffmancoding( huffmantree & , huffmancode * , welem * , int );/ 对文件 tobebin.txt 中的正文进行编码/ 将结果存入 codefile.txt void encoding( huffmancode * , const char* );/ 对文件 codefile.tx

5、t 里的代码进行译码/ 将结果存入 textfile.txtvoid decoding( huffmantree , huffmancode * , const char* , const int );/ 对 codefile.txt 里的代码进行编排,显示,打印void printcode( const char* , const char* );/ 选择两个最小的结点void selecttwonode( huffmantree , int , int& , int& ); int main() / 用文件流对象 hufinput 打开外部文件/ humanweight.txt 储存字符集

6、大小 n ,以及 n 个字符和 n 个权值ifstream hufinput( humanweight.txt , ios:in );int hufnum = 0;hufinput hufnum; / 读入字符集大小 n 到 hufnumwelem* hufw = new welem hufnum ; / 分配 n 个字符和 n 个权值的储存空间for( int ii = 0 ; ii hufw ii .ch hufw ii .wt;hufinput.close(); / 关闭 humanweight.txt 文件huffmantree huft; / 空树huffmancode* hufc

7、= ( huffmancode* ) malloc ( ( hufnum + 1 ) * sizeof( huffmancode ) ); / 分配n个字符编码的头指针向量huffmancoding( huft , hufc , hufw , hufnum ); / 编码中./ 用文件流对象 huftreeoutput 把赫夫曼树 ht , hc 输出到文件 huftree.txt 中ofstream huftreeoutput( huftree.txt , ios:out );huftreeoutput - ht - endl setw( 9 ) weight parent lchild r

8、child endl;for( int tout = 1 ; tout = 2*hufnum-1 ; tout+ )huftreeoutput setw( 6 ) tout setw( 8 ) huft tout .weight setw( 8 ) huft tout .parent setw( 8 ) huft tout .lchild setw( 8 ) huft tout .rchild endl;huftreeoutput - end ht - endl endl - hc - endl;for( int cout = 1 ; cout = hufnum ; cout+ )huftre

9、eoutput hufc cout .ch hufc cout .hufch endl;huftreeoutput - convert - ok - endl;huftreeoutput.close(); / 输出完毕,关闭文件delete hufw; / 释放内存encoding( hufc , tobebin.txt ); / 对正文编码decoding( huft , hufc , codefile.txt , hufnum ); / 译码printcode( codefile.txt , codeprint.txt ); / 打印代码cout endl;return 0;/ w 存放

10、n 个字符的权值( 均大与0 )/ 构造赫夫曼树 ht , 并求出 n 个字符的赫夫曼编码 hc/void huffmancoding( huffmantree &ht , huffmancode* hc , welem* w , int n ) if( n = 1 ) return;int m = 2 * n - 1; / 赫夫曼树的结点数目ht = ( huffmantree ) malloc ( ( m + 1 ) * sizeof( htnode ) ); /0 号单元未用huffmantree p = ht;p+; / p 指向 ht 的第 1 号单元int i=0 , ww=0;

11、/ ww 为 welem* w 的下标 for( i = 1 ; i weight = www.wt;p-parent = p-lchild = p-rchild = 0; / / 初始化for( ; i weight = p-parent = p-lchild = p-rchild = 0;/ 建赫夫曼树/ 在 ht 1 . i-1 选择parent 为 0 且 weight 最小的两个结点,其序号分别为 s1 和 s2 /int s1=0 , s2=0;for( i = n + 1 ; i = m ; +i ) selecttwonode( ht , i-1 , s1 , s2 ); ht

12、 s1 .parent = i; / 标记 parent ht s2 .parent = i; / 标记 parent ht i .lchild = s1; / 标记左孩子ht i .rchild = s2; / 标记右孩子ht i .weight = ht s1 .weight + ht s2 .weight; / 新结点的权值 / 从叶子到根逆向求每个字符的赫夫曼编码/char* cd = new char n ; / 分配求编码的工作空间cd n - 1 = 0; / 编码结束符int c = 0;int f = 0;for( i = 1 ; i = n ; +i ) / 逐个字符求赫夫

13、曼编码int start = n - 1; / 编码结束符位置for( c = i , f = ht i .parent ; f != 0 ; c = f , f = ht f .parent ) / 从叶子到根逆向求编码if( ht f .lchild = c ) cd -start = 0; else cd -start = 1; hc i .ch = w i-1 .ch ; / 复制字符hc i .hufch = ( char* ) malloc ( ( n - start ) * sizeof( char ) ); / 为第 i 个字符编码分配空间strcpy( hc i .hufch

14、 , &cd start ); / 从 cd 复制编码到 hcdelete cd; / 释放工作空间 / end huffmancoding()/ 选择两个最小的结点 , 序号分别放在 s1 和 s2 中/void selecttwonode( huffmantree ht , int i , int& s1 , int& s2 )unsigned int sm1 , sm2; / 用于权值的比较s1 = 1; / 从序号为 1 的权值开始 s2 = 1;int m=0; / 最小权值的序号临时变量for( m = 1 ; m = i ; m+ ) / 第一遍中第一个 parent 为 0 的

15、结点if( ht m .parent != 0 ) continue;else sm1 = ht m .weight;s1=m;break;for( int j = m+1 ; j ht j .weight ) sm1 = ht j .weight;s1 = j;for( m = 1 ; m = i ; m+ ) / 第二遍中第一个 parent 为 0 的结点if( ht m .parent != 0 ) continue;else sm2 = ht m .weight;s2=m;if( s2 = s1 ) continue;else break;for( int k = m+1 ; k =

16、 i ; k+ ) / 第二遍选第二个最小的if( ht k .parent != 0 ) continue;elseif( (ht k .weight sm2) & ( k != s1 ) ) / 保证sm1 != sm2sm2 = ht k .weight;s2 = k; / end selecttwonode()/ 要编码的正文存放在 ifile 里/ 通过 fin 的成员函数 get( inbuf ) 从 ifile 里读入每一个字符 ,其中 char inbuf 为字符缓冲区 _/ / 编码后的结果通过 fout( oftream 的对象 ) 输出到文件 codefile.txt/v

17、oid encoding( huffmancode* hc , const char* ifile )ifstream fin( ifile , ios:in ); char inbuf = #;ofstream fout( codefile.txt , ios:out );int sub = 0; / hc 的下标while( fin.get( inbuf ) ) / 读入字符,直至文件结束符if( inbuf = ) sub = 1; / hc 1 .hufch = fout hc sub .hufch;else if( inbuf = n )continue;else / 下标换算/ 因

18、为 a 的 ascii码为 65 ,又a 在 hc 里的下标为 2/ 即 hc 2 .hufch = a. 以下的字符雷同sub = inbuf - 63; fout hc sub .hufch; /fin.close(); / 显式关闭文件fout.close(); / / end encoding()/ / 本应该把赫夫曼编码放进 map( , ) 里的,但本程序没有这样做,显得效率有点低/ 在求子串相应的字符时,用的方法为在 hc 里进行逐字扫描比较 &/ 文件流的使用同 eecoding() 里的一样/ 参数 n 为字符个数,p = 2*n-1 就为 ht 的结点数目. htp就为 h

19、t 的根./void decoding( huffmantree ht , huffmancode * hc , const char* ifile , const int n )ifstream fin( ifile , ios:in );char inbuf = 1;ofstream fout( textfile.txt , ios:out );char* cd = new char n ; / 储存字串的工作空间int start = 0; / 译码开始标记int p = 2 * n - 1; / 根下标int ihc = 1; / 用于扫描 hc 的循环变量while( fin.get

20、( inbuf ) ) / 循环读入 0 或 1 或 nif( inbuf = 0 )if( ht p .lchild != 0 ) / 不是叶子cd start+ = inbuf; / 压 inbuf 进 cd*p = ht p .lchild; / 进入左子树continue;else / 左叶子cd start = 0; / 子串结束符for( ihc = 1 ; ihc = n ; ihc+ ) / 寻找与子串匹配的字符if( strcmp( hc ihc .hufch , cd ) = 0 )fout hc ihc .ch;break; / 找到,跳出 forelse continu

21、e;start = 0; /cd start+ = inbuf; / 为读下一子串做准备p = ht 2*n - 1 .lchild; / 注意,这时 p 已指向 root 的 left childelse if( inbuf = 1 )if( ht p .rchild != 0 ) / 不是叶子cd start+ = inbuf; / 压 inbuf 进 cd*p = ht p .rchild; / 进入左子树continue;else / 右叶子cd start = 0; / 子串结束符for( ihc = 1 ; ihc = n ; ihc+ ) / 寻找与子串匹配的字符if( strc

22、mp( hc ihc .hufch , cd ) = 0 )fout hc ihc .ch;break; / 找到,跳出 forelse continue;start = 0; /cd start+ = 1; / 为读下一子串做准备p = ht 2*n - 1 .rchild; / 注意,这时 p 已指向 root 的 right childelse if( inbuf = n ) / 有必要的话,加了个回车符,哈哈continue;/*cd start = 0; / 子串结束符for( ihc = 1 ; ihc = n ; ihc+ ) / 寻找与子串匹配的字符if( strcmp( hc ihc .hufch , cd ) = 0 )fout hc ihc .ch;break; / 找到,跳出 forelse continue;fout endl;start = 0; / p = 2*n - 1; / p 指向 root*/ else/ 不要忘了最后一个字符。cd start = 0;for( ihc = 1 ; ihc = n ; ihc+ ) / 寻找与子串匹配的字符if(

温馨提示

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

评论

0/150

提交评论