C++实现Huffman的编解码

Huffman编码主要是通过统计各元素出现的频率,进而生成编码最终达到压缩的目的。

这里是Huffman树中节点的结构。

typedef struct Tree
{
    int freq;//频率
    int key;//键值
    struct Tree *left, *right;
    Tree(int fr=0, int k=0,Tree *l=nullptr, Tree *r=nullptr):
        freq(fr),key(k),left(l),right(r){};
}Tree,*pTree;

首先用一个名为freq的hashtable来记录各个元素的频率:

void read(){
    int a;

    std::ios::sync_with_stdio(false);
    while(cin>>a){
        if(freq.find(a)==freq.end()) {freq[a]=1;}
        else {freq[a]++;}
    }

}


Huffman树的构建过程如下代码所示:

void huffman()
{
    int i;
    string c;
    int fr;
    auto it = freq.begin();
    while(it!=freq.end()){
        Tree *pt= new Tree;
        pt->key = it->first;
        pt->freq = it->second;
 
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Huffman Encoding的C++实现示例: ```cpp #include <iostream> #include <queue> #include <unordered_map> #include <vector> using namespace std; // Huffman树节点 struct HuffmanNode { char ch; // 符号 int freq; // 出现频率 HuffmanNode* left; HuffmanNode* right; HuffmanNode(char c, int f) : ch(c), freq(f), left(nullptr), right(nullptr) {} ~HuffmanNode() { delete left; delete right; } }; // 定义比较函数用于优先队列 struct compare { bool operator()(HuffmanNode* a, HuffmanNode* b) { return a->freq > b->freq; } }; // 统计字符串中符号出现的频率 unordered_map<char, int> count_freq(string s) { unordered_map<char, int> freq_map; for (auto c : s) { freq_map[c]++; } return freq_map; } // 构建HuffmanHuffmanNode* build_huffman_tree(unordered_map<char, int>& freq_map) { priority_queue<HuffmanNode*, vector<HuffmanNode*>, compare> min_heap; for (auto& p : freq_map) { min_heap.push(new HuffmanNode(p.first, p.second)); } while (min_heap.size() > 1) { auto left = min_heap.top(); min_heap.pop(); auto right = min_heap.top(); min_heap.pop(); auto parent = new HuffmanNode('\0', left->freq + right->freq); parent->left = left; parent->right = right; min_heap.push(parent); } return min_heap.top(); } // 递归构建Huffman编码表 void build_huffman_code_table(HuffmanNode* node, string& code, unordered_map<char, string>& code_table) { if (!node) { return; } if (node->ch != '\0') { code_table[node->ch] = code; return; } code.push_back('0'); build_huffman_code_table(node->left, code, code_table); code.pop_back(); code.push_back('1'); build_huffman_code_table(node->right, code, code_table); code.pop_back(); } // 对字符串进行Huffman编码 string encode(string s) { unordered_map<char, int> freq_map = count_freq(s); HuffmanNode* root = build_huffman_tree(freq_map); unordered_map<char, string> code_table; string code; build_huffman_code_table(root, code, code_table); string encoded_str; for (auto c : s) { encoded_str += code_table[c]; } delete root; return encoded_str; } // 对Huffman编码后的字符串进行解码 string decode(string encoded_str, HuffmanNode* root) { string decoded_str; HuffmanNode* cur = root; for (auto c : encoded_str) { if (c == '0') { cur = cur->left; } else { cur = cur->right; } if (cur->ch != '\0') { decoded_str += cur->ch; cur = root; } } return decoded_str; } int main() { string s = "hello, world!"; string encoded_str = encode(s); cout << "Encoded string: " << encoded_str << endl; HuffmanNode* root = build_huffman_tree(count_freq(s)); string decoded_str = decode(encoded_str, root); cout << "Decoded string: " << decoded_str << endl; delete root; return 0; } ``` 其中,`count_freq`函数用于统计字符串中符号出现的频率;`build_huffman_tree`函数用于构建Huffman树;`build_huffman_code_table`函数用于递归构建Huffman编码表;`encode`函数用于对字符串进行Huffman编码;`decode`函数用于对Huffman编码后的字符串进行解码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值