《哈弗曼编码__压缩任意格式文件.pdf》由会员分享,可在线阅读,更多相关《哈弗曼编码__压缩任意格式文件.pdf(12页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、本程序是本人学习数据结构时自己写的,可以压缩、解压缩任意格式文件。由于本人水平有限,里面会有缺陷,若有兴趣,可以多多指正!Email: 年 5 月 9 日 10:24:19/编译器:VC+6.0/数据组织:参见程序的详细说明/功能:哈弗曼压缩文件(可以压缩并解压任何格式文件),参见程序的详细说明/历时:2 天/*Bug 修复*【2012.5.10 18:13:00 修复了:文件中只有一种英文字符无法压缩和解压缩的Bug。解决办法参见本文件中哈夫曼树的创建函数】【2012.5.10 20:49:00 修复了:不能准确还原文件的Bug,是解压文件与原文件大小等完全相同,另外还可以压缩、解压其他任何
2、类型文件(doc、jpg 等)。具体修复办法参见压缩、解压缩】*/*程序的详细说明*本程序是适用于所有格式文件。/-【注意,以下是对于文本文件的思考。因为是二进制操作文件,故适用于所有格式文件】如何实现压缩文件呢,首先考虑到字符存储问题,英文是一个字节存储的,汉子是两个字节存储的。都属于定长存储(定长编码),可以利用哈夫曼树对其进行编码,使其转换为变长的前缀编码。那么,如何实现编码的转换呢?考虑到汉子也可以将其两个字节分别处理,故这里采用单个字节处理的办法,将每个字节进行编码。首先读取文件,计算出每个字节出现的次数。次数即权值,对其进行构造哈夫曼树,然后求出编码,将编码存储在一个二维数组中。再
3、次打开想要压缩的文件,每读入一个字节,就对其进行编码,并写入压缩文件。由于将编码后的数据写入压缩文件时,需整个字节操作,即编码数据刚好整个字节时(可以是多个字节)写入压缩文件,故此处利用一个整形变量作为缓冲。解压缩时,需再次将与压缩时相同的哈夫曼树构造出来,以便进行译码。因此压缩时存储了,原文件中各个字节出现的次数(也就是权值),这样就能再次构造出哈夫曼树。【注意】这里当然也可以将哈夫曼树存储下来,解压时再次重构。解压时,读取字符出现的次数,重构出哈夫曼树,进行译码。这里也是读取整个字节(可以是多个),按位进行译码。*/#include#include#include using namesp
4、ace std;/HuffmanNode 节点定义struct HuffmanNode unsigned char value;/字符内容double cost;/权值HuffmanNode*leftChild,*rightChild,*parent;HuffmanNode(int weight=0):value(0),cost(weight),leftChild(NULL),rightChild(NULL),parent(NULL);/常量、全局变量定义const double MaxCost=10000000000.0;/最大权值const int MaxCodeLen=300;/原文件、
5、压缩文件、解压缩文件文件名const char strSrcFileName300=压缩前.txt;const char strCompreFileName300=压缩文件.HuffYaSuo;const char strDeComFileName300=解压后.txt;int g_numTimes256;/存储各个字符出现次数HuffmanNode*g_pHuffTree=NULL;/指向最终的哈弗曼树unsigned int code256MaxCodeLen;/存储压缩时使用的编码,有遍历哈夫曼树得到文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档
6、编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL1
7、0B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X
8、9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU
9、1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4
10、J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 Z
11、A3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7
12、I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3unsigned int tempCodeMaxCodeLen;/临时存储编码,用于将编码赋给code/统计出字符出现次数/统计各个字符出现次数void CountNumTimes(void)memset(g_numTimes,0,256*sizeof(d
13、ouble);/注意,以二进制文件打开。若以ASCII 文件打开会出问题!-【即,文件可能读取不完】ifstream fin(strSrcFileName,ios_base:binary);unsigned char cTemp;/不要用 fin cTemp,因为 会对输入进行格式化,导致有的字符读入失败while(fin.read(char*)&cTemp,sizeof(cTemp)g_numTimescTemp+;fin.close();/构造哈夫曼树/找出根节点权值最小的一颗树,被CreateHuffTree()调用HuffmanNode*FindMinCostTree(HuffmanN
14、ode*&pTreeList)double cost=MaxCost;int num=-1;for(int i=0;icost-0cost-0-0.000001)&pTreeListi-cost cost;num=i;if(num!=-1)HuffmanNode*pTree=pTreeListnum;pTreeListnum=NULL;return pTree;文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3
15、Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4
16、T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码
17、:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B
18、4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C
19、1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S
20、10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5
21、Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3 else return NULL;/将两颗树合并,被CreateHuffTree()调用void MergeTwoTree(HuffmanNode*firstMin,HuffmanNode*secondMin,HuffmanNode*&pTreeList)HuffmanNode*pHuffNode=new HuffmanNode(firstMin-cost+secondMin-cost);pHuff
22、Node-leftChild=firstMin;pHuffNode-rightChild=secondMin;firstMin-parent=pHuffNode;secondMin-parent=pHuffNode;for(int i=0;i256;i+)if(pTreeListi=NULL)pTreeListi=pHuffNode;break;/构造哈弗曼树void CreateHuffTree(void)HuffmanNode*pTreeList=new HuffmanNode*256;for(int i=0;ivalue=i;HuffmanNode*firstMin,*secondMin
23、;while(1)/每次循环求的两个根节点权值最小的树,并合并 firstMin=FindMinCostTree(pTreeList);secondMin=FindMinCostTree(pTreeList);if(secondMin=NULL)文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10
24、B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9
25、C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1
26、S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J
27、5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA
28、3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I
29、4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编
30、码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3 g_pHuffTree=firstMin;/-【解决 Bug】-/只有一个节点情况,对应于文件中只有一种字符,为了解决无法编码,需将其作为根节点的左孩子,或右孩子,此处为左孩子if(g_pHuffTree-leftChild=NULL&g_pHuffTree-rightChild=NULL)g_pHuffTree-leftChild=new HuffmanNode;g_pHuffTree-leftChild-value=g_pHuffTree-value;/-break;MergeTwoTree(firstMin,
31、secondMin,pTreeList);delete pTreeList;/-测试-/前序遍历哈夫曼树/void PreOrderTraverse(HuffmanNode*pNode)/static int outNum=0;/if(pNode!=NULL)/cout value :cost ;/+outNum%15=14?(cout endl):(cout leftChild);/PreOrderTraverse(pNode-rightChild);/-/求出压缩时的编码/初始化 code等数组全部成员为-1 void InitCode()for(int i=0;iMaxCodeLen;i
32、+)tempCodei=-1;文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA
33、3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I
34、4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编
35、码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10
36、B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9
37、C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1
38、S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3for(i=0;i256;i+)for(int j=0;jMaxCodeLen;j+)codeij=-1;/将临时的编码复制给code,被
39、 FindCode()调用void CopyCode(int num)int i=0;while(-1!=tempCodei)codenumi=tempCodei;i+;/-测试-/输出编码/void PrintCode()/int num=0,i=0;/for(num=0;num256;num+)/cout 【num 】编码为:;/i=0;/while(-1!=codenumi)/cout codenumi ;/i+;/cout value;/while(pNode!=g_pHuffTree)/文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL
40、10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3
41、X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 H
42、U1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B
43、4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8
44、ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B
45、7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文
46、档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3/pNode-parent-leftChild=pNode?tempCodei+=0:tempCodei+=1;/pNode=pNode-parent;/int j=0;/for(i=i-1;i=0;i-,j+)/codenumj=tempCodei;/求出编码vo
47、id FindCode(HuffmanNode*pNode)static num=0;static codeNum=0;if(pNode!=NULL&pNode-leftChild=NULL&pNode-rightChild=NULL)/是叶节点/CodeBack(pNode);/向上回溯求编码CopyCode(pNode-value);/将临时存储空间的编码复制给编码存储数组。注意,也可以利用上一句进行求的编码 if(pNode!=NULL&pNode-leftChild!=NULL)tempCodecodeNum+=0;/左子树,将编码0写入临时存储空间FindCode(pNode-lef
48、tChild);/递归左子树tempCode-codeNum=-1;/回退,将退回路径上的编码从临时存储空间清除 if(pNode!=NULL&pNode-rightChild!=NULL)tempCodecodeNum+=1;/右子树,将编码1写入临时存储空间FindCode(pNode-rightChild);/递归右子树tempCode-codeNum=-1;/回退,将退回路径上的编码从临时存储空间清除 /压缩/压缩文件void CompressionFile()/注意,以二进制文件打开。若以ASCII 文件打开会出问题!-【即,文件可能读取不完】文档编码:CL10B4G3X9C1 HU
49、1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4
50、J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 ZA3Z9B7I4T3文档编码:CL10B4G3X9C1 HU1S10B4J5Q8 Z