#include <iostream>
#include <cstdio>
#define inf 1<<30
using namespace std;
//哈夫曼树实现
//哈夫曼树的特征:如果有n个叶子节点的话,则总结点数为2*n-1。越频繁访问的编码越短
struct node
{
int parent, lson, rson;
int val; //访问次数
};
void createTree(node p[], int n, int total) //total=2*n-1
{
for(int t=0; t<total; ++t) //初始化
{
p[t].parent = p[t].lson = p[t].rson = -1;
}
for(int i=n; i<total; ++i)
{
int min1, min2, l, r; //最大值和第二大值
min1 = min2 = inf;
l = r = -1;
for(int t=0; t<i; ++t) //遍历寻找两个最小的点,也就是val值最小的
{
if(p[t].parent == -1)
{
if(p[t].val<min1)
{
r = l;
l = t;
min2 = min1;
min1 = p[t].val;
} else if(p[t].val<min2){
r = t;
min2 = p[t].val;
}
}
}
p[i].val = min1 + min2;
p[l].parent = p[r].parent = i;
p[i].lson = l;
p[i].rson = r;
}
}
struct code
{
int k;
char c[100];
};
void huffmancode(node p[], int n, code c1[])
{
for(int t=0; t<n; ++t)
{
int pre = p[t].parent;
int g = t;
c1[t].k = 0;
while(pre != -1)
{
if(p[pre].lson == g)
{
c1[t].c[c1[t].k++] = '0';
} else {
c1[t].c[c1[t].k++] = '1';
}
g = pre;
pre = p[pre].parent;
}
c1[t].k--;
}
}
int main()
{
node p[4*2-1];
for(int t=0; t<4; ++t)
{
p[t].val = 2*t+1;
}
createTree(p, 4, 2*4-1);
code c[4];
huffmancode(p, 4, c);
for(int t=0; t<4; ++t)
{
int k = c[t].k;
printf("%d:", p[t].val);
for(int j=k; j>=0; --j)
{
printf("%c", c[t].c[j]);
}
printf("\n");
}
return 0;
}
运行结果: