question
完整代码
#include <iostream>
#include <vector>
#include <string>
#define MAX_Weight 65535
struct HTNode
{
int weight = 0, parent = 0, LChild = 0, RChlid = 0;
std::string code = "";
char data = '\0';
};
void InitHuffman(std::vector<HTNode> &ht, std::vector<int> weight, std::string st, int n);
void CreatHuffman(std::vector<HTNode> &ht, int n);
void Select(std::vector<HTNode> ht, int n, int &s1, int &s2);
void ShowHuffman(std::vector<HTNode> ht);
void Encoding(std::vector<HTNode> &ht, int n);
std::string Decoding(std::vector<HTNode> ht, std::string code);
int main()
{
int n;
std::cin >> n;
std::string st = "";
char ch;
for (auto i = 0; i < n; ++i)
{
std::cin >> ch;
st += ch;
}
int w;
std::vector<int> weight;
for (auto i = 0; i < n; ++i)
{
std::cin >> w;
weight.emplace_back(w);
}
std::string source;
std::cin >> source;
std::vector<HTNode> ht((n << 1) - 1);
InitHuffman(ht, weight, st, n);
CreatHuffman(ht, n);
Encoding(ht, n);
std::string encode = "";
for (auto i = 0; i < source.size(); ++i)
{
for (auto j = 0; j < n; ++j)
{
if (ht[j].data == source[i])
encode += ht[j].code;
}
}
std::cout << encode << std::endl;
std::string decode = Decoding(ht, encode);
std::cout << Decoding(ht, encode) << std::endl;
system("pause");
return 0;
}
void InitHuffman(std::vector<HTNode> &ht, std::vector<int> weight, std::string st, int n)
{
for (auto i = 0; i < n; ++i)
{
ht[i].data = st[i];
ht[i].weight = weight[i];
ht[i].parent = 0;
ht[i].LChild = 0;
ht[i].RChlid = 0;
}
int m = ht.size();
for (auto i = n; i < m; ++i)
{
ht[i].weight = 0;
ht[i].parent = 0;
ht[i].LChild = 0;
ht[i].RChlid = 0;
ht[i].data = '\0';
}
}
void CreatHuffman(std::vector<HTNode> &ht, int n)
{
int s1, s2;
int m = ht.size();
for (auto i = n; i < m; ++i)
{
Select(ht, i, s1, s2);
ht[i].weight = ht[s1].weight + ht[s2].weight;
ht[i].LChild = s1, ht[i].RChlid = s2;
ht[s1].parent = ht[s2].parent = i;
}
}
void Select(std::vector<HTNode> ht, int n, int &s1, int &s2)
{
int left = MAX_Weight, right = MAX_Weight;
for (auto i = 0; i < n; ++i)
{
if (ht[i].parent == 0)
{
if (ht[i].weight < left)
{
right = left;
s2 = s1;
left = ht[i].weight;
s1 = i;
}
else if (ht[i].weight < right)
{
right = ht[i].weight;
s2 = i;
}
}
}
}
void ShowHuffman(std::vector<HTNode> ht)
{
int m = ht.size();
for (auto i = 0; i < m; ++i)
{
std::cout << ht[i].weight << " " << ht[i].parent << " " << ht[i].LChild << " " << ht[i].RChlid << std::endl;
}
}
void Encoding(std::vector<HTNode> &ht, int n)
{
int child, parent;
std::string code;
for (auto i = 0; i < n; ++i)
{
child = i;
parent = ht[i].parent;
code = "";
while (parent)
{
if (child == ht[parent].LChild)
code += '0';
else
code += '1';
child = parent;
parent = ht[parent].parent;
}
ht[i].code.assign(code.rbegin(), code.rend());
}
}
std::string Decoding(std::vector<HTNode> ht, std::string code)
{
std::string result = "";
int len = code.size();
int m = ht.size();
int idx = m - 1;
int cur = 0;
while (cur < len)
{
if (code[cur] == '0')
idx = ht[idx].LChild;
else
idx = ht[idx].RChlid;
if (ht[idx].LChild == 0 && ht[idx].RChlid == 0)
{
result += ht[idx].data;
idx = m - 1;
}
cur++;
}
return result;
}