原理就是统计带压缩文件字符频率,构建哈夫曼树,然后求哈夫曼编码,将字符频率(解压的时候通过字符频率建树)和哈夫曼编码写入文件,完成压缩。
压缩代码:
//获取一个文件的每个字符的频率
void get_frequency(string filename, int frequency[256])
{
ifstream fin(filename);
if (!fin.is_open())
{
return ;
}
memset(frequency, 0, sizeof(int) * 256);
while (!fin.eof())
{
unsigned char temp = fin.get();
if (fin.eof())
{
break;
}
frequency[temp]++;
}
fin.close();
}
//哈夫曼树的节点
struct node
{
unsigned char ch;
int w;
node *rch, *lch;
};
//获取一个行自定义属性的节点
node* new_node(unsigned char ch, int w, node* lch = NULL, node* rch = NULL)
{
node* temp = (node*)malloc(sizeof(node));
temp->ch = ch;
temp->w = w;
temp->rch = rch;
temp->lch = lch;
return temp;
}
//优先级队列比较大小的方法
struct cmp
{
bool operator () (node* x, node* y)
{
return x->w > y->w;
}
};
//建树,返回根节点
node* build_haffman(int frequency[256])
{
priority_queue<node*, vector<node*>, cmp> q;
for (int i = 0; i < 256; i++)
{
if (frequenc