当前位置:网站首页>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;
}
边栏推荐
- Principle and process of MySQL master-slave replication
- [leetcode] 14. Longest public prefix
- How do I open an account now? Is there a faster and safer opening channel
- Is the table queried by this EMR sparksql node ODPs?
- 《高难度谈话》突破谈话瓶颈,实现完美沟通
- ArtBench:第一个类平衡的、高质量的、干净注释的和标准化的艺术品生成数据集
- Set operator of gbase8s database in combined query
- Zhengda futures liu4 data integration
- Gbase8s database select has order by Clause 1
- How to fix ORA-01017:用户名/口令无效 登录拒绝
猜你喜欢
联想领像 lenovoimage 部分打印机 驱动 PPD 文件

How to create new user for ORACLE 19c (CDB & PDB)

MySQL 主从复制原理以及流程
![[pbootcms template] composition website / document download website source code](/img/6e/51bbb4ce961defa4abd098ff3af21f.jpg)
[pbootcms template] composition website / document download website source code

每周推荐短视频:爱因斯坦是怎样思考问题的?

ArtBench:第一个类平衡的、高质量的、干净注释的和标准化的艺术品生成数据集

ERP preparation of BOM basis

MIT线性代数中文笔记

How can colleges and universities build future oriented smart campus based on cloud native? Full stack cloud native architecture vs traditional IT architecture

大家有没有觉得学机械的人很可怕?
随机推荐
求大数的阶乘 ← C语言
How can colleges and universities build future oriented smart campus based on cloud native? Full stack cloud native architecture vs traditional IT architecture
GBase8s数据库FOR UPDATE 子句
Gbase8s database into external clause
After class assignment of module 5 of the construction practice camp
How to install oracle19c in Centos8
MySQL数据库主从同步,一致性解决方案
& 4 express framework
& 3 view request message and response message in browser
架构实战营第五模块课后作业
智能指标驱动的管理和决策平台 Kyligence Zen 全新上线,限量内测中
Gbase8s database select has order by Clause 5
Jerry's configuration of TWS cross pairing [chapter]
这个EMR-SparkSQL节点,他查询的表是不是ODPS的啊?
torch. Load load model error: can't get attribute 'VAE_ vc‘ on <module ‘__ main__‘ From 'xxxx() run file path‘
GBase8s数据库select有HAVING 子句
Titanium dynamic technology: our Zadig landing Road
Weekly recommended short video: How did Einstein think?
Introduction to multi project development - business scenario Association basic introduction test payroll
Do you think people who learn machinery are terrible?