哈夫曼树
哈夫曼树(Huffman Tree)是一种用于数据压缩的树形结构。它是由David A. Huffman在1952年提出的,被广泛应用于无损数据压缩算法中。
在哈夫曼树中,每个叶子节点都代表一个字符或符号,并且带有一个权重,该权重通常是字符在文本中出现的频率或概率。树的根节点是所有叶子节点的父节点,且每个非叶子节点都有两个子节点。
构造方法
编码设计思路:
在Huffman树中,权值最大的结点离根最近,权值最小的结点离根最远
①根据给定的n个权值(w1, w2, …, wn)构成n棵二叉树的集合F={T1, T2, …, Tn},其中每棵二叉树Ti中只有一个带树为Ti的根结点
②在F中选取两棵根结点的权值最小的树作为左右子树构造一棵新的二叉树,且置其根结点的权值为其左右子树权值之和
③在F中删除这两棵树,同时将新得到的二叉树加入F中
④重复步骤2和3,直到F只含一棵树为止
//编码
void Encoding() {
int m = 2 * l;
int s1, s2;
for (int i = l + 1; i < m; i++) {
select(i - 1, s1, s2);
a[i].right = s2;
a[i].left = s1;
a[i].weight = a[s1].weight + a[s2].weight;
a[s1].parent = a[s2].parent = i;
}
for (int i = 1; i <= l; i++) {
stack<char> st;
for (int c = i, f = a[i].parent; f != 0; c = f, f = a[f].parent) {
if (a[f].left == c)
st.push('0');
else
st.push('1');
}
cout << a[i].weight << ' ' << a[i].ch << '-';
while (!st.empty()) {
cout << st.top();
st.pop();
}
cout << endl;
}
}
解码设计思路:
在哈夫曼树中,编码是通过将字符映射到对应的二进制码来表示的。解码过程是根据给定的哈夫曼树和编码,将编码转换回原始字符的过程。
解码的步骤如下:
- 从哈夫曼树的根节点开始。
- 读取编码的每一位,如果是0,则向左子节点移动;如果是1,则向右子节点移动。
- 一直重复步骤2,直到到达叶子节点。
- 到达叶子节点后,记录该叶子节点对应的字符。
- 回到根节点,继续读取下一个编码位。
- 重复步骤2到步骤5,直到所有编码位都被处理。
- 解码完成后,得到原始的字符序列。
//解码
void Decoding() {
string s;
cin >> s;
int i, k, c;
char *txtstr = new char[l];
char ch;
c = 2 * l - 1;
k = 0;
for (i = 0; i < s.length(); i++) {
ch = s[i];
if (ch == '0') {
c = a[c].left;
} else if (ch == '1') {
c = a[c].right;
} else {
cout<<"error"<<endl;
return;
}
if (a[c].left == 0 && a[c].right == 0) {
txtstr[k++] = a[c].ch;
c = 2 * l - 1;
} else {
ch = '\0';
}
}
if (ch == '\0')
{
cout<<"error"<<endl;
return;
}
else txtstr[k] = '\0';
cout<<txtstr<<endl;
}
哈夫曼树全部实现代码:
#include<iostream>
#include <stack>
using namespace std;
//哈夫曼编码
class node {
public:
int weight;
char ch;
int parent, left, right;
node() {
weight = parent = left = right = 0;
ch = ' ';
}
};
class huffman {
public:
node *a;
int l;
huffman() {
cin >> l;
a = new node[2 * l];
for (int i = 1; i <= l; i++)
cin >> a[i].weight;
for (int i = 1; i <= l; i++)
cin >> a[i].ch;
}
void select(int s, int &s1, int &s2) {
int m = 10000;
for (int i = 1; i <= s; i++) {
if (!a[i].parent) {
if (a[i].weight < m) {
s1 = i;
m = a[i].weight;
}
}
}
m = 10000;
for (int i = 1; i <= s; i++) {
if (!a[i].parent) {
if (a[i].weight < m && i != s1) {
s2 = i;
m = a[i].weight;
}
}
}
}
//编码
void Encoding() {
int m = 2 * l;
int s1, s2;
for (int i = l + 1; i < m; i++) {
select(i - 1, s1, s2);
a[i].right = s2;
a[i].left = s1;
a[i].weight = a[s1].weight + a[s2].weight;
a[s1].parent = a[s2].parent = i;
}
for (int i = 1; i <= l; i++) {
stack<char> st;
for (int c = i, f = a[i].parent; f != 0; c = f, f = a[f].parent) {
if (a[f].left == c)
st.push('0');
else
st.push('1');
}
cout << a[i].weight << ' ' << a[i].ch << '-';
while (!st.empty()) {
cout << st.top();
st.pop();
}
cout << endl;
}
}
//解码
void Decoding() {
string s;
cin >> s;
int i, k, c;
char *txtstr = new char[l];
char ch;
c = 2 * l - 1;
k = 0;
for (i = 0; i < s.length(); i++) {
ch = s[i];
if (ch == '0') {
c = a[c].left;
} else if (ch == '1') {
c = a[c].right;
} else {
cout<<"error"<<endl;
return;
}
if (a[c].left == 0 && a[c].right == 0) {
txtstr[k++] = a[c].ch;
c = 2 * l - 1;
} else {
ch = '\0';
}
}
if (ch == '\0')
{
cout<<"error"<<endl;
return;
}
else txtstr[k] = '\0';
cout<<txtstr<<endl;
}
};