夫曼树是带权路径长度最小的二叉树,用途是平均查找信息的代价最小。
普通二叉树的用途也普通,比较通用,就是信息存储和查找。
普通二叉树可能有的只有一个子节点,而哈夫曼树一定有两个。
哈夫曼编码哈夫曼树应用哈夫曼编码应用广泛JPEG应用哈夫曼编码
首先介绍哈夫曼树哈夫曼树称优二叉树种带权路径度短二叉树所谓树带权路径度树所叶结点权值乘其根结点路径度(若根结点0层叶结点根结点路径度叶结点层数)树带权路径度记WPL=(W1*L1+W2*L2+W3*L3+...+Wn*Ln)N权值Wi(i=1,2,...n)构棵N叶结点二叉树相应叶结点路径度Li(i=1,2,...n)证明哈夫曼树WPL
哈夫曼世纪五十代初提种编码根据字符现概率构造平均度短编码种变编码编码若各码字度严格按照码字所应符号现概率逆序排列则编码平均度(注:码字即符号经哈夫曼编码编码其度符号现概率同所说哈夫曼编码变编码)
、给定n权值{W1,W2,W3,...,Wi,...,Wn}构n棵二叉树初始集合F={T1,T2,T3,...,Ti,...,Tn}其每棵二叉树Ti权值Wi根结点左右树均空(便计算机实现算般要求Ti权值Wi升序排列)
二、F选取两棵根结点权值树作新构造二叉树左右树新二叉树根结点权值其左右树根结点权值
三、F删除两棵树并棵新二叉树同升序排列加入集合F
四、重复二三两步直集合F棵二叉树止
用C语言实现述算用静态二叉树或态二叉树若用态二叉树用数据结构:
struct
tree{
float
weight;
/*权值*/
union{
char
leaf;
/*叶结点信息字符*/
struct
tree
*left;
/*树左结点*/
};
struct
tree
*right;
/*树右结点*/
};
struct
forest{
/*F集合链表形式表示*/
struct
tree
*ti;
/*
F树*/
struct
forest
*next;
/*
结点*/
};
例:若字母ABZC现概率:0.75,0.54,0.28,0.43;则相应权值:75542843
构造哈夫曼树根据哈夫曼树进行编码例:面字符根据其现概率作权值构造棵哈夫曼树经哈夫曼编码应码值要使用同棵哈夫曼树编码原原组字符显哈夫曼编码前缀编码即任字符编码都另字符编码前缀否则编码能进行翻译例:a,b,c,d编码:01010111于编码串:1010翻译bb或cab编码c编码前缀刚才进行哈夫曼编码规则根结点叶结点(包含原信息)路径向左孩前进编码0向右孩前进编码1反规定
种编码静态哈夫曼编码需要编码数据进行两遍扫描:第遍统计原数据各字符现频率利用频率值创建哈夫曼树并必须树信息保存起即字符0-255(2^8=256)频率值2-4BYTES度顺序存储起(用4Bytes度存储频率值频率值表示范围0--2^32-1已足够表示文件字符现频率)便解压创建同哈夫曼树进行解压;第二遍则根据第遍扫描哈夫曼树进行编码并编码码字存储起
静态哈夫曼编码些缺点:、于短文件进行编码意义光4BYTES度存储哈夫曼树信息需1024Bytes存储空间;二、进行哈夫曼编码存储编码信息若用与通讯网络引起较延;三、较文件进行编码频繁磁盘读写访问降低数据编码速度
提种态哈夫曼编码态哈夫曼编码使用棵态变化哈夫曼树第t+1字符编码根据原始数据前t字符哈夫曼树进行编码解码使用相同初始哈夫曼树每处理完字符编码解码使用相同修改哈夫曼树所没必要解码保存哈夫曼树信息编码解码字符所需间与该字符编码度比所态哈夫曼编码实进行态哈夫曼编码比静态哈夫曼编码复杂兴趣读者参考关数据结构与算书籍
前面提JPEG用哈夫曼编码并说JPEG用哈夫曼编码幅图片经步骤列数值些数值进行哈夫曼编码便存储或传输哈夫曼编码比较易懂家根据编码自编写哈夫曼编码解码程序
#include
#include
#include
#include
#include
#define MAXVALUE 200 /*权值的最大值*/
#define MAXBIT 30 /*最大的编码位数*/
#define MAXNODE 30 /*初始的最大的结点数*/
struct haffnode
{char data;
int weight;
int flag;
int parent; /*双亲结点的下标*/
int leftchild; /*左孩子下标*/
int rightchild; /*右孩子下标*/
};
struct haffcode
{int bit[MAXNODE];
int start; /*编码的起始下标*/
char data;
int weight; /*字符权值*/
};
/*函数说明*/
/************************************************************************/
void pprintf(struct haffcode haffcode[],int n);
/*输出函数*/
void haffmantree(int weight[],int n,struct haffnode hafftree[],char data[]);
/*建立哈夫曼树*/
void haffmancode(struct haffnode hafftree[],int n,struct haffcode haffcode[]);
/*求哈夫曼编码*/
void test(struct haffcode haffcode[],int n);
/*测试函数*/
void end();
/*结束界面函数*/
/************************************************************************/
void haffmantree(int weight[],int n,struct haffnode hafftree[],char data[])
/*建立叶结点个数为n,权值数组为weight[]的哈夫曼树*/
{int i,j,m1,m2,x1,x2;
/*哈夫曼树hafftree[]初始化,n个叶结点共有2n-1个结点*/
for(i=0;i<2*n-1;i++)
{if(i
}
else {hafftree[i].weight=0; /*非叶结点*/
hafftree[i].data='\0';
}
hafftree[i].parent=0; /*初始化没有双亲结点*/
hafftree[i].flag=0;
hafftree[i].leftchild=-1;
hafftree[i].rightchild=-1;
}
for(i=0;i
x1=x2=0;
for(j=0;j
x2=x1;
m1=hafftree[j].weight;
x1=j;
}
else if(hafftree[j].weight
x2=j;
}
}
hafftree[x1].parent=n+i;
hafftree[x2].parent=n+i;
hafftree[x1].flag=1;
hafftree[x2].flag=1;
hafftree[n+i].weight=hafftree[x1].weight+hafftree[x2].weight;
hafftree[n+i].leftchild=x1;
hafftree[n+i].rightchild=x2;
}
}
void haffmancode(struct haffnode hafftree[],int n,struct haffcode haffcode[])
{/*由n个结点的哈夫曼树hafftree[]构成的哈夫曼编码haffcode[]*/
int i,j,child,parent;
struct haffcode newcode;
struct haffcode *cd;
cd=&newcode;
for(i=0;i
cd->weight=hafftree[i].weight;
cd->data=hafftree[i].data; /*取得编码对应值的字符*/
child=i;
parent=hafftree[child].parent;
while(parent!=0)
{if(hafftree[parent].leftchild==child)
cd->bit[cd->start]=0; /*左孩子编码为0*/
else
cd->bit[cd->start]=1; /*右孩子编码为1*/
cd->start--;
child=parent;
parent=hafftree[child].parent;
}
for(j=cd->start+1;j
haffcode[i].data=cd->data;
haffcode[i].start=cd->start;
haffcode[i].weight=cd->weight;
}
}
void pprintf(struct haffcode myhaffcode[],int n)
{int i,j,count=0;
clrscr();
for(i=0;i
cprintf("字符=%c",myhaffcode[i].data);
printf(" ");
textcolor(YELLOW);
cprintf("weight=%3d",myhaffcode[i].weight);
printf(" ");
textcolor(YELLOW);
cprintf("haffcode=");
for(j=myhaffcode[i].start+1;j
printf("\n");
count++;
if(count==21)
getch();
}
}
void test(struct haffcode haffcode[],int n)
{int i,j,k,s;
char sstring[MAXNODE];
struct haffcode newhaffcode[MAXNODE];
j=0;
clrscr();
textcolor(YELLOW);
cprintf("请输入哈夫曼编码测试数据,在此建议为'this programme is my favorite'");
printf("\n");
cprintf("注意小写,空格由大写字母T代替,并且字符数小于27.\n");
scanf("%s",sstring);
if(strlen(sstring)>=MAXNODE)
{printf("you input the data number >=MAXNODE.");
exit(1);
}
for(i=0;i
for(j=0;j
{
k=j;
break;
}
if(k<0||k>MAXNODE-1)
{printf("在系统中找不到与第个%d字符相匹配的编码\n",i+1);
continue;
}
newhaffcode[i].start=haffcode[k].start;
newhaffcode[i].weight=haffcode[k].weight;
newhaffcode[i].data=haffcode[k].data;
for(s=haffcode[k].start+1;s
}
pprintf(newhaffcode,strlen(sstring));
}
void end()
{int driver,mode;
driver=VGA;
mode=VGAHI;
initgraph(&driver,&mode," ");
setlinestyle(0,0,2);
setfillstyle(1,9);
bar(120,60,520,80);
setfillstyle(1,9);
bar(90,100,550,350);
moveto(121,65);
settextstyle(5,0,6);
setcolor(7);
outtext("This programme is designed by Dou Zheren");
settextstyle(3,0,3);
setcolor(7);
moveto(150,200);
outtext("thank you use this programme.");
moveto(100,300);
settextstyle(3,0,2);
setcolor(7);
outtext("please press anykey to end this programme.");
}
void main()
{
int i,j,n=27;
int driver=VGA,mode=VGAHI;
char ch;
int weight[27]={186,64,13,22,32,103,21,15,47,
57,1,5,32,20,57,63,15,1,48,
51,80,23,8,18,1,16,1};
char data[28]={'T','a','b','c','d','e','f','g','h',
'i','j','k','l','m','n','o','p','q',
'r','s','t','u','v','w','x','y','z'};
struct haffnode newhaffnode[2*MAXNODE-1];
struct haffcode newcode[MAXNODE];
struct haffnode *myhafftree=newhaffnode;
struct haffcode *myhaffcode=newcode;
if(n>MAXNODE)
{printf("you input the haffnode > MAXNODE,so you input the data is wrong");
printf("\n");
exit(1);
}
clrscr();
textcolor(YELLOW);
cprintf("WELCOME!这是一个求哈夫曼编码的问题");
printf("\n");
cprintf("即对所有的字母进行编码后,在根据用户的需要,对用户的要求进行编码。");
printf("\n");
cprintf("注意:本程序只支持小写字母,空格用大写字母T代替! ");
printf("\n");
getch();
textcolor(YELLOW);
cprintf("Ready?Enter,if you want to begin!\n");
printf("\n");
getch();
cprintf("Now,开始演示哈夫曼编码.");
getch();
haffmantree(weight,n,myhafftree,data);
haffmancode(myhafftree,n,myhaffcode);
pprintf(myhaffcode,n);
clrscr();
printf("若执行自定义编译,请输入y继续。否则程序将结束.");
if((ch=getch())=='y'||ch=='Y')
test(myhaffcode,n);
getch();
clrscr();
end();
getch();
exit(1);
}