C语言数据结构,小弟急求~

来源:百度知道 编辑:UC知道 时间:2024/06/27 07:20:20
Huffman 编码
若待编码的字符集为C={c1,c2,c3,c4,c5},相应地在电文中出现的频率集W={5,7,10,2,4}。求各字符的Huffman 编码。
不要算法,要具体的程序啊,急求啊谢谢~

#include <cstdio>
#include <queue>
#include <string>
#include <vector>
using namespace std;

struct Node {
string name;
int p;
Node *left, *right;
Node(const char* n, int pp, Node* l, Node* r) : name(n), p(pp), left(l), right(r) { }
~Node() {
delete left;
delete right;
}
bool operator<(const Node& n) const {
return p < n.p;
}
};

struct Cmp {
bool operator()(Node*& a, Node*& b) {
return a->p > b->p;
}
};

void print(const Node* root, char* ans, int depth) {
if (root->left == 0 && root->right == 0) {
ans[depth] = 0;
printf("%s: %s\n", root->name.c_str(), ans);
return;
}
if (root->left != 0) {
ans[depth] = '0';
print(root-&g