当前位置:网站首页>huffman编码
huffman编码
2022-06-29 11:45:00 【天下一般】
使用优先队列进行优化的haffman编码。
- 统计文章中的词频
- 使用优先队列(堆)创建haffman树
- 遍历Haffman树得到每一个字符的huffman编码, 保存在enc(unordered_map)中
- 文章进行编码
- 使用huffman树和haffman编码进行解码。
创建huffman树的时间复杂度 O ( C l o g C ) O(ClogC) O(ClogC)其中C是单词种类的个数。
编码的时间复杂度 O ( n ) O(n) O(n),只需要遍历一遍字符串就行了。
解码的时间复杂度 O ( m ) O(m) O(m),只需要遍历一遍编码后的字符串就行了。
huffman编码是现代压缩技术的基础。应该写个输入输出测试一下对文件的压缩的情况的。
char是8个bit, 而bit数组中的一位是1个bit。所以肯定会有压缩的效果。
文件最后保存的是haffman树以及对应的huaffman编码。在解码的时候先读取haffmans树,然后对二进制文件进行解码。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <unordered_map>
using namespace std;
// Encodes a URL to a shortened URL.
struct HuffmanTree {
HuffmanTree *l, *r;
int cnt;
char ch; // 词频和单词
HuffmanTree(int cnt, char ch, HuffmanTree *l, HuffmanTree *r):cnt(cnt), ch(ch), l(l), r(r) {}
};
class cmp{
public:
/* Node::priority 大的优先, 重写小于号 */
bool operator () (HuffmanTree* &a, HuffmanTree* &b) const
{
return a->cnt > b->cnt;
}
};
typedef pair<int, char>PR;
priority_queue<HuffmanTree*, vector<HuffmanTree*>, cmp>que, bakcup;
unordered_map<char, int>mp;
HuffmanTree* buildHuffman() {
bakcup = que;
while (!bakcup.empty()) {
printf("%d %c\n", bakcup.top()->cnt, bakcup.top()->ch);
bakcup.pop();
}
while (que.size() >= 2) {
HuffmanTree* t1 = que.top(); que.pop();
HuffmanTree* t2 = que.top(); que.pop();
HuffmanTree *fa = new HuffmanTree(t1->cnt + t2->cnt, '#', t1, t2);
que.push(fa);
}
return que.top();
}
HuffmanTree *root;
unordered_map<char, string>enc;
void travel(HuffmanTree *root, string s) { // 获取每个结点的huffman编码
if (root == NULL) return ;
// 叶子结点, 统计编码
if (root->l == NULL && root->r == NULL) {
cout << root->ch << " " << s << endl;
enc[root->ch] = s;
return ;
}
travel(root->l, s + '0');
travel(root->r, s + '1');
}
string encode(string longUrl) {
for (char ch : longUrl) {
mp[ch]++;
}
for (auto [ch, cnt] : mp) {
que.push(new HuffmanTree(cnt, ch, NULL, NULL)); // 存放的是指针。
}
root = buildHuffman();
travel(root, ""); // 遍历haffman树获取Huffman编码
string ans;
for (char ch : longUrl) {
ans += enc[ch];
}
return ans;
}
string treeDecode(HuffmanTree *root, int &cur, string &s) {
if (root == NULL) return "";
if (root->l == NULL && root->r == NULL) {
string ans = "";
ans += root->ch;
return ans;
}
cur++;
if (s[cur] == '0') return treeDecode(root->l, cur, s);
else return treeDecode(root->r, cur, s);
}
// Decodes a shortened URL to its original URL.
string decode(string shortUrl) {
int cur = -1;
string ans;
int n = shortUrl.size();
for (cur; cur < n - 1;) {
printf("cur = %d\n", cur);
string s = treeDecode(root, cur, shortUrl);
cout << s << endl;
ans += s;
}
return ans;
}
int main() {
string s = "aaabbc";
s = encode(s);
cout << s << endl;
cout << decode(s) << endl;
}
边栏推荐
- nvtmpp
- Gbase8s database select has order by Clause 4
- Pro test! Centos7 deploy PHP + spool
- What are outer chain and inner chain?
- AutoCAD - text display mode and how CAD can directly open Tianzheng drawings
- 解决问题:ModuleNotFoundError: No module named ‘pip‘
- 黑化的蜜雪冰城,凭营销就想抓牢消费者的心?
- Intelligent trash can (IV) -- raspberry pie Pico realizes ultrasonic ranging (hc-sr04)
- How do I open an account now? Is there a faster and safer opening channel
- Imile uses Zadig's multi cloud environment to deploy thousands of times a week to continuously deliver global business across clouds and regions
猜你喜欢

对p值的理解

多项目开发入门-业务场景关联基础入门测试 工资表

Easy express: we use Zadig to realize 10000 times of construction and deployment, smart operation and maintenance, and release development productivity

百度云盘不限速下载大文件(2021-11亲测有效)

How to install oracle19c in Centos8

ERP preparation of BOM basis

Baidu cloud disk downloads large files without speed limit (valid for 2021-11 personal test)

一种可解释的几何深度学习模型,用于基于结构的蛋白质结合位点预测

AutoCAD - text display mode and how CAD can directly open Tianzheng drawings

After class assignment of module 5 of the construction practice camp
随机推荐
智能指标驱动的管理和决策平台 Kyligence Zen 全新上线,限量内测中
Weekly recommended short video: How did Einstein think?
GBase8s数据库FOR READ ONLY 子句
Gbase8s database select has order by Clause 1
Factorization of large numbers ← C language
NvtBack
Set operator of gbase8s database in combined query
Is it safe for Orient Fortune Securities to open an account? Handling of securities account opening
bison使用error死循环的记录
MySQL master-slave synchronous asynchronous replication semi synchronous replication full synchronous replication
Gbase8s database select has order by Clause 6
Pangolin compilation error: 'numeric_ limits’ is not a member of ‘std’
Artbench: the first class balanced, high-quality, clean annotated and standardized artwork generation data set
MySQL数据库主从同步,一致性解决方案
GBase8s数据库FOR UPDATE 子句
Gbase8s database select has a having clause
【LeetCode】14、最长公共前缀
How to create new user for ORACLE 19c (CDB & PDB)
内插散点数据
黑化的蜜雪冰城,凭营销就想抓牢消费者的心?