哈夫曼编码时一种最优码。
在已知传输的字符集合及其出现频度的情况下,得到一个编码长度最小的,这种码就是哈夫曼编码。
这是哈夫曼编码的实现
先找两个权值最小的,然后建立二叉树
以此类推,得到哈夫曼树
这是代码实现,我用的是Code:Blocks软件,经测试,可以运行。
#include <stdio>
#include <limits>
#include <string>
#include <stdlib>
#define N 6
typedef struct huffNode
{
int weight; //权重
int lchild,rchild,parent; //左右子节点和父节点
}HTNode,*HuffTree;
typedef char **HuffCode;
//找出数组中无父节点且权值最小的两个节点下标,分别用s1和s2保存
void select(const HuffTree &HT,int n,int &s1