#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
#define MaxSize 1024 // 读入文件的上限
#define OK 1
#define ERROR 0
typedef int Status;
typedef struct wordcnt{ // 统计字符和对应的次数
char ch;
int cnt = 0;
}Count;
typedef struct NumCount{ // 统计次数的外部封装
Count count[MaxSize];
int length = 0;
}NumCount;
typedef struct HTree{ // 哈夫曼树结构
char data;
int weight;
int parent,lchild,rchild;
}HTNode,*HuffmanTree;
typedef struct HCode{ // 编码结构
char data;
char* str;
}*HuffmanCode;
Status ReadData(char *source); // 读入文件
Status WordCount(char *data,NumCount *paraCnt); // 统计次数
Status Show(NumCount *paraCnt); // 展示次数
Status CreateHuffmanTree(HuffmanTree &HT,int length,NumCount cntarray); // 创建哈夫曼树
Status select(HuffmanTree HT,int top,int *s1,int *s2); // 选择权重最小的两个节点
Status CreateHuffmanCode(HuffmanTree HT,HuffmanCode &HC,int length); // 创建哈夫曼编码
Status Encode(char *data,HuffmanCode HC,int length); // 将读入的文
哈夫曼树:(1)输入各字符及其权值(2)构造哈夫曼树(3)进行哈夫曼编码(4)查找HC[i],得到各字符的哈夫曼编码
最新推荐文章于 2023-03-20 14:06:51 发布
本文介绍了一种使用哈夫曼编码进行数据压缩的方法。通过构建哈夫曼树并生成哈夫曼编码,实现了对输入文本文件的有效压缩及解压过程。文章详细展示了从读取文件内容、统计字符频率、构建哈夫曼树到生成编码和解码的具体步骤。

最低0.47元/天 解锁文章
337

被折叠的 条评论
为什么被折叠?



