郝夫曼树的编译码实现.doc_第1页
郝夫曼树的编译码实现.doc_第2页
郝夫曼树的编译码实现.doc_第3页
郝夫曼树的编译码实现.doc_第4页
郝夫曼树的编译码实现.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

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

文档简介

#include #include #define MAX 27/最大码长int nCount = 0;/记录结点总数int nNode = 0;/记录有效结点数/哈夫曼树结点的结构struct _HNodechar data;/数据域int pri;/权重struct _HNode * left;/左孩子指针struct _HNode * right;/右孩子指针struct _HNode * parent;/父节点指针;/编码存储结点的结构struct _HCodestruct _HNode * p;/哈夫曼树结点指针char codeMAX;/编码int flag;/码长;void main()cout * 哈夫曼编/译码的实现 * endlint i;/for 循环的计数器/内容输入cout 正在建立哈夫曼树(26 个大写英文字母和空格)! endl;char chMAX+1;for(i = 0;i MAX+1;i+)if(i = 0)chi = ;elsechi = A+i-1;chi = 0;int priMAX = 186,64,13,22,32,103,21,15,47,57,1,5,32,20,57,63,15,1,48,51,80,23,8,18,1,16,1;/记录结点struct _HCode HT2*MAX;while(nCount data = chnCount;newNode-pri = prinCount;newNode-left = NULL;newNode-right = NULL;newNode-parent = NULL;HTnCount.p = newNode;HTnCount.flag = 0;nCount+;/建立哈夫曼树cout 开始创建哈夫曼树. endl;nNode = nCount;for(i = 1;i nNode;i+)struct _HCode temp1;/第一小的结点位置struct _HCode temp2;/第二小的结点位置int tpri1 = 32767;/第一小的权重(int 的最大界限 32767)int tpri2 = 32767;/第二小的权重(int 的最大界限 32767)for(int j = 0;j pri parent = NULL)tpri2 = tpri1;temp2 = temp1;tpri1 = HTj.p-pri;temp1 = HTj;else if(HTj.p-pri parent = NULL)tpri2 = HTj.p-pri;temp2 = HTj;struct _HNode * newNode = NULL;newNode = new _HNode;if(newNode = NULL)return;newNode-data = ;newNode-pri = tpri1 + tpri2;newNode-left = temp1.p;newNode-right = temp2.p;newNode-parent = NULL;temp1.p-parent = newNode;temp2.p-parent = newNode;HTnCount+.p = newNode;cout 创建完毕! endl;/开始编码(code 中的编码是反序的)cout 开始编码. endl;for(i = 0;i parent)if(temp = temp-parent-left)HTi.codeHTi.flag+ = 0;if(temp = temp-parent-right)HTi.codeHTi.flag+ = 1;temp = temp-parent;HTi.codeHTi.flag = 0;cout 编码完毕! endl;/编码/内容cout 要翻译成二进制编码的文字:THIS PROGRAM IS MY FAVORITE endl;char text = THIS PROGRAM IS MY FAVORITE;/编码过程及结果输出cout 翻译结果: endl;int nnn = 0;/控制换行 50 每行;for(i = 0;texti;i+)for(int j = 0;j data = texti)for(int nflag = HTj.flag-1;nflag = 0;nflag-)cout HTj.codenflag;nnn+;if(nnn = 50)cout endl;nnn = 0;cout endl;/解码/内容cout 要解码的二进制编码:n011011110111100111000000101111000100010100110000100101010110010 endl;char codetext = 011011110111100111000000101111000100010100110000100101010110010;int codetextflag = 0;/编码过程及结果输出cout 翻译结果: left != NULL)/tempDecode-right 与 tempDecode-left 同有同无if(codetextcodetextflag = 0)tempDecode = tempDecode-left;codetextflag+;else if(codetextcodetextflag = 1)tempDecode = tempDecode-right;codetextflag+;cout data;tempDecode = HTnCount-1.p;cout endl;/人工输入文字进行编-译码/编码/内容cout n请输入要编码的文字(仅包括大写英文字母和空格): endl;char InsertText1024;cin.getline(InsertText,1024);/编码过程及结果输出cout 编码结果: = A & InsertTexti = Z) | InsertTexti = )for(int j = 0;j data = InsertTexti)for(int nflag = HTj.flag-1;nflag = 0;nflag-)cout HTj.codenflag;nnn+;if(nnn = 50)cout endl;nnn = 0;elsecout *;cout endl;/解码/内容cout n请输入要解码的二进制数(仅包括 0 和 1 ): endl;char InsertCode1024;cin.getline(InsertCode,1024);InsertCodestrlen(InsertCode) = 0;int InsertCodeFlag = 0;int RemainCodeFlag = 0;char RemainCode10;/编码过程及结果输出cout 翻译结果: left != NULL)/tempDecode-right 与 tempDecode-left 同有同无if(InsertCodeInsertCodeFlag = 0)RemainCodeRemainCodeFlag+ = 0;tempInsertDecode = tempInsertDecode-left;InsertCodeFlag+;else if(InsertCodeInsertCodeFlag = 1)RemainCodeRemainCodeFlag+ = 1;tempInsertDecode = tempInsertDecode-right;InsertCodeFlag+;else if(InsertCodeInsertCodeFlag)cout l

温馨提示

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

评论

0/150

提交评论