这一节介绍哈夫曼树和哈弗曼编码,其中构造哈弗曼树的步骤如下
1.根据给定的n个权值{ Wl,响,…,Wn }构成n棵二叉树的集合F={Tl,T2,…Tn},其中每棵二叉树Ti 中只有一个带权为Wi 根结点,其左右子树均为空。
2. 在F中选取两棵根结点的权值最小的树作为左右子树构造一棵新的二叉树,且置新的二叉树的根结点的权值为其左右子树上根结点的权值之和。
3. 在F中删除这两棵树,同时将新得到的二叉树加入F中。
4. 重复2和3步骤,直到F只含一棵树为止。这棵树便是哈弗曼树。
对于哈弗曼编码是一种最基本的压缩编码方式,就是根据哈弗曼树,然后前缀编码,左子树为0,右子树为1
实现的代码是参考http://www.cnblogs.com/Jezze/archive/2011/12/23/2299884.html,感觉代码读起来易懂
我知识调整了下代码的结构,让代码更加清晰,还有删去了源代码的解码函数,但是核心代码都没有变化,其中关于如何求权值可以使用优先队列,参考九度题目1172:哈夫曼树
代码中用的是实现二叉树的另外一种存储结构,双亲表示法,其实感觉这个双亲表示法的思想和静态链表的思想类似
struct haTree{
int parent; //存储该结点的双亲在数组中的下标
int lchild;
int rchild;
int weight; //权重
};
struct haCode{
int bit[MAXN]; //编码数组
int start; //编码起始长度,作用就是知道当前结点的编码长度
};
struct haTree h[MAXN];
创建哈弗曼树的代码如下:
void creatHuffmanTree(struct haTree *h,int n){
int i,j;
int m=2*n-1;
for(i=0;i<m;i++){
h[i].parent=-1;
h[i].lchild=-1;
h[i].rchild=-1;
h[i].weight=-1;
}
for(i=0;i<n;i++) scanf("%d",&h[i].weight);
int x1,x2;
int m1,m2;
for(i=0;i<n-1;i++){
x1=x2=0; //x1,x2分别为最小的两个结点的下标
m1=m2=MAX;
for(j=0;j<n+i;j++){
if(h[j].weight<m1&&h[j].parent==-1){ //查找当前最小的权值的结点
m2=m1;
x2=x1;
x1=j;
m1=h[j].weight;
}
else if(h[j].weight<m2&&h[j].parent==-1){ //查找当前倒数第二小的权值的结点
x2=j;
m2=h[j].weight;
}
}
h[x1].parent=n+i;
h[x2].parent=n+i;
h[n+i].weight=h[x1].weight+h[x2].weight;
h[n+i].lchild=x1;
h[n+i].rchild=x2;
printf ("x1.weight and x2.weight in round %d: %d, %d\n", i+1, h[x1].weight, h[x2].weight);
printf ("\n");
}
for(i=0;i<n+2;i++)
{
printf("Parents:%d,lchild:%d,rchild:%d,weight:%d\n",h[i].parent,h[i].lchild,h[i].rchild,h[i].weight);
}
}
创建哈弗曼编码的代码如下
void HuffmanCode(struct haCode *huffCode,int n){
int i,p,j,c;
struct haCode tmp;
for(i=0;i<n;i++){
tmp.start=n-1;
c=i;
p=h[c].parent;
while(p!=-1){ //对每个结点编码,n-(start+1)为编码位的数目,如n=4,编码是011(编码最多是n-1位),则start=0
if(h[p].lchild==c) tmp.bit[tmp.start]=0;
else tmp.bit[tmp.start]=1;
tmp.start--;
c=p;
p=h[c].parent;
}
for(j=tmp.start+1;j<n;j++) huffCode[i].bit[j]=tmp.bit[j];
huffCode[i].start=tmp.start;
}
}
总的代码如下:
#include <stdio.h>
#define MAXN 100
#define MAX 1000000
struct haTree{
int parent;
int lchild;
int rchild;
int weight;
};
struct haCode{
int bit[MAXN];
int start;
};
struct haTree h[MAXN];
void creatHuffmanTree(struct haTree *h,int n){
int i,j;
int m=2*n-1;
for(i=0;i<m;i++){
h[i].parent=-1;
h[i].lchild=-1;
h[i].rchild=-1;
h[i].weight=-1;
}
for(i=0;i<n;i++) scanf("%d",&h[i].weight);
int x1,x2;
int m1,m2;
for(i=0;i<n-1;i++){
x1=x2=0; //x1,x2分别为最小的两个结点的下标
m1=m2=MAX;
for(j=0;j<n+i;j++){
if(h[j].weight<m1&&h[j].parent==-1){ //查找当前最小的权值的结点
m2=m1;
x2=x1;
x1=j;
m1=h[j].weight;
}
else if(h[j].weight<m2&&h[j].parent==-1){ //查找当前倒数第二小的权值的结点
x2=j;
m2=h[j].weight;
}
}
h[x1].parent=n+i;
h[x2].parent=n+i;
h[n+i].weight=h[x1].weight+h[x2].weight;
h[n+i].lchild=x1;
h[n+i].rchild=x2;
printf ("x1.weight and x2.weight in round %d: %d, %d\n", i+1, h[x1].weight, h[x2].weight);
printf ("\n");
}
for(i=0;i<n+2;i++)
{
printf("Parents:%d,lchild:%d,rchild:%d,weight:%d\n",h[i].parent,h[i].lchild,h[i].rchild,h[i].weight);
}
}
void HuffmanCode(struct haCode *huffCode,int n){
int i,p,j,c;
struct haCode tmp;
for(i=0;i<n;i++){
tmp.start=n-1;
c=i;
p=h[c].parent;
while(p!=-1){ //对每个结点编码,n-(start+1)为编码位的数目,如n=4,编码是011(编码最多是n-1位),则start=0
if(h[p].lchild==c) tmp.bit[tmp.start]=0;
else tmp.bit[tmp.start]=1;
tmp.start--;
c=p;
p=h[c].parent;
}
for(j=tmp.start+1;j<n;j++) huffCode[i].bit[j]=tmp.bit[j];
huffCode[i].start=tmp.start;
}
}
void display(struct haCode *huffCode,int n){
int i,j;
for(i=0;i<n;i++){
printf ("%d 's Huffman code is: ", i);
for(j=huffCode[i].start+1;j<n;j++){
printf("%d",huffCode[i].bit[j]);
}
printf(" start:%d\n",huffCode[i].start);
}
}
int main(){
int n;
scanf("%d",&n);
creatHuffmanTree(h,n);
struct haCode huffCode[MAXN];
HuffmanCode(huffCode,n);
display(huffCode,n);
return 0;
}