利用优先队列实现Huffman压缩方法

这里先分享一下我的原码:
#include
#include
#include
#include
#include
#include
#include<stdio.h>
#include<math.h>
#include
using namespace std;
struct huffman_node;
typedef huffman_node* node_ptr;

struct huffman_node
{
char id;
int freq;
string code;
node_ptr left,
right,
parent;
};
class compare//huffman class
{
public:
bool operator()(const node_ptr& c1,
const node_ptr& c2)const
{
return(*c1).freq > (*c2).freq;
}
};

class huffman
{
static const int MAX_SIZE = 256;
node_ptr node_array[MAX_SIZE];
priority_queue <node_ptr,vector<node_ptr>,compare>pq;
public:
string in_file_name;
fstream in_file,
out_file;

   huffman(string in_file_name,string out_file_name);
   void  create_pq();
   void  create_node_array();
   void  create_huffman_tree();
   void  calculate_huffman_code();
   void  save_to_file();

};
huffman::huffman(string in_file_name,string out_file_name)
{
(*this).in_file_name = in_file_name;
in_file.open(in_file_name.c_str(),ios::in);
out_file.open(out_file_name.c_str(),ios::out);

}
void huffman :: create_pq()
{
const string DEFAULT_TOTAL = “whit a fixed number of bits per character, the message size in bits is”;
node_ptr entry;
int sum = 0,
n_chars = 0,
n_bits_per_char;
for(int i = 0; i < MAX_SIZE; i++)
{
node_array[i] = new huffman_node;
(*node_array[i]).freq = 0;
}
create_node_array();
for(int i = 0; i < MAX_SIZE; i++)
{
entry = node_array[i];
if((*entry).freq > 0)
{
pq.push(entry);
sum += (entry).freq;
n_chars++;
}
}
n_bits_per_char = ceil(log(n_chars) / log(2));
cout<<endl << DEFAULT_TOTAL<<(sum
n_bits_per_char)<<endl;

}
void huffman:: create_node_array()
{
node_ptr entry;
string line;
while(getline(in_file,line))
{
for(unsigned j = 0; j < line.length(); j++)
{
entry = node_array[(int)(line[j])];
(*entry).freq++;
if((*entry).freq == 1)
{
(*entry).left = NULL;
(*entry).right = NULL;
(*entry).parent = NULL;
}
}
entry = node_array[(int)’\n’];
(*entry).freq++;
(*entry).left = NULL;
(*entry).right = NULL;
(*entry).parent = NULL;

    }

}
void huffman ::create_huffman_tree()
{
node_ptr left,
right,
sum;
while(pq.size() > 1)
{
left = pq.top();
pq.pop();
(*left).code = string(“0”);
right = pq.top();
pq.pop();
(*right).code = string(“1”);
sum = new huffman_node;
(*sum).parent = NULL;
(*sum).freq = (*left).freq + (*right).freq;
(*sum).left = left;
(*sum).right = right;
(*left).parent = sum;
(*right).parent = sum;
pq.push(sum);
}
}

void huffman :: calculate_huffman_code()
{
const string HUFFMAN_CODES = “Here are the Huffman codes:”;
const string ENCODED_SIZE_MESSAGE = "\n\n The size of the encoded message, in bits, is ";
int total = 0;
string code;
node_ptr entry;
cout<<endl << HUFFMAN_CODES << endl;
for(int i = 0; i <MAX_SIZE; i++)
{
code = “”;
entry = node_array[i];
if((*entry).freq > 0)
{
cout<<(char)i << " ";
do
{
code = (*entry).code + code;
entry = (*entry).parent;
}
while((*entry).parent != NULL);
cout<<code << endl;
(*node_array[i]).code = code;
total += code.length() * (*node_array[i]).freq;

            }
    }
    cout<<ENCODED_SIZE_MESSAGE<<total << endl;

}

void huffman:: save_to_file()
{
node_ptr entry;
string line;
in_file.close();
in_file.open(in_file_name.c_str(),ios::in);
for(int i = 0; i < MAX_SIZE; i++)
{
entry = node_array[i];
if((*entry).freq > 0)
{
out_file << (char)i <<" “<<(*entry).code << endl;
}
}
out_file<<”**"<<endl;
while(getline(in_file,line))
{
for(unsigned j = 0; j < line.length(); j++)
{
entry = node_array[(int)(line[j])];
out_file<<(*entry).code;
}
entry = node_array[(int)’\n’];
out_file<<(*entry).code;
}
out_file.close();
}

int main()
{
huffman huf(“in.txt”,“out.txt”);
huf.create_pq();
huf.create_node_array();
huf.create_huffman_tree();
huf.calculate_huffman_code();
huf.save_to_file();
}
这里面你只需要修改huf里面的txt文件,最好把cpp与txt保持同级
列如你可以在1.txt里面输入more money needed
就会实现huffman压缩

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值