哈夫曼编码步骤
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{
printf ("%d", HuffCode[i].bit[j]);
}
printf(" start:%d",HuffCode[i].start);
printf ("\n");
}/*
for(i=0;i<n;i++){
for(j=0;j<n;j++)
{
printf ("%d", HuffCode[i].bit[j]);
哈夫曼编码步骤:
一、对给定的n个权值{W1,W2,W3,...,Wi,...,Wn}构成n棵二叉树的初始集合F= {T1,T2,T3,...,Ti,...,Tn},其中每棵二叉树Ti中只有一个权值为Wi的根结点,它的左右子树均为空。(为方便在计算机上实现算法,一般还要求以Ti的权值Wi的升序排列。)
二、在F中选取两棵根结点权值最小的树作为新构造的二叉树的左右子树,新二叉树的根结点的权值为其左右子树的根结点的权值之和。
三、从F中删除这两棵树,并把这棵新的二叉树同样以升序排列加入到集合F中。
四、重复二和三两步,直到集合F中只有一棵二叉树为止。
/*-------------------------------------------------------------------------
return s1;
}
/*再去掉重复出现的字符(即压缩电文),提取哈夫曼树叶结点*/
char * getcode (char *s1)
{ char s2[26],s5[26];
char temp[200]="",*p,*q,*r,*s3="";
int m,e,n=0;
m=strlen(s1);
while(m>0)
{ char temp[128]="",*p,*q;
p=s1;
while ((q=strstr(p,s2))!=NULL)
{ *q='\0';
strcat(temp,p);
strcat(temp,s3);
p=q+strlen(s2); }
strcat(temp,p);
strcpy(s1,temp);
m2=HuffNode[j].weight;
x2=j;
}
} /* end for */
/*设置找到的两个子结点x1、x2的父结点信息*/
HuffNode[x1].parent = n+i;
HuffNode[x2].parent = n+i;
HuffNode[n+i].weight = HuffNode[x1].weight + HuffNode[x2].weight; HuffNode[n+i].lchild = x1;
HCODETYPE huffcode[MAXLEAF],cd;
int sum,i,j,n1,n2,x1,x2,p,k,c;
char s1[26]={'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'};
HuffNode[n+i].rchild = x2;
printf ("x1.weight and x2.weight in round %d: %d, %d\n", i+1, HuffNode[x1].weight, HuffNode[x2].weight);
/*用于测试*/
printf ("\n");
Fra Baidu bibliotekint parent;
int lchild;
int rchild;
int value;} HNodeType; /*结点结构体*/
/*构造一颗哈夫曼树*/
void HuffmanTree (HNodeType HuffNode[MAXNODE], int n){
/* i、j:循环变量,m1、m2:构造哈夫曼树不同过程中两个最小权值结点的权值,x1、x2:构造哈夫曼树不同过程中两个最小权值结点在数组中的序号。*/
{ p=s1;
s2[0]=s1[0];
s2[1]='\0';
r=s2;
e=0;
while((q=strstr(p,r))!=NULL)
{ *q='\0';
strcat(temp,p);
strcat(temp,s3);
p=q+strlen(s2);
e++; }
m-=e;
strcat(temp,p);
strcpy(s1,temp);
/*定义一个编码结构体数组,同时定义一个临时变量来存放求解编码时的信息*/ int i, j, c, p, n;
char pp[100];
printf ("Please input n:\n");
scanf ("%d", &n);
HuffmanTree (HuffNode, n);
for (i=0; i < n; i++)
{
cd.start = n-1;
c = i;
p = HuffNode[c].parent;
while (p != -1)
/*父结点存在*/
{
if (HuffNode[p].lchild == c)
cd.bit[cd.start] = 0;
else
cd.bit[cd.start] = 1;
cd.start--;
*父结点左侧,则置码为0,若在右侧,则置码为1。最后输出生成的编码。*------------------------------------------------------------------------*/
#include <stdio.h>
#include<stdlib.h>
#define MAXBIT 100
char s5[MAXLEAF];
int ww[26]={0},n=0;
strcpy( s5,s2);
sum=strlen(s2);
for(i=0;i<26;i++) /*统计所有字符出现的频度*/
for(j=0;j<sum;j++)
if(s2[j]==s1[i]) ww[i]++;
HuffNode[i].rchild =-1;
HuffNode[i].value=i;
//实际值,可根据情况替换为字母
} /* end for */
/*输入n个叶子结点的权值*/
for (i=0; i<n; i++)
{
printf ("Please input weight of leaf node %d: \n", i);
{
if(*nump==0)
{
tmp=Buf[tmp].lchild ;
}
else tmp=Buf[tmp].rchild;
nump++;
}
printf("%d",Buf[tmp].value);
}
}
int main(void){
HNodeType HuffNode[MAXNODE]; /*定义一个结点结构体数组*/ HCodeType HuffCode[MAXLEAF], cd;
}
*/
//测试
}
/* end HuffmanTree */
//解码
void decodeing(char string[],HNodeType Buf[],int Num){
int i,tmp=0,code[1024];
int m=2*Num-1;
char *nump;
char num[1024]
for(i=0;i<strlen(string);i++)
s5[n]=s2[0];
n++;
strcpy(temp,"");
}
s5[n]='\0';
strcpy(s1,s5);
printf("压缩后的电文(即叶结点): %s\n",s1);
return s1;
}
HNODETYPE huffmantree(char *s2,char s3[])
{ HNODETYPE huffnode[MAXNODE];
scanf ("%d", &HuffNode[i].weight);
} /* end for */
/*循环构造Huffman树*/
for (i=0; i<n-1; i++)
{
m1=m2=MAXVALUE;
/* m1、m2中存放两个无父结点且结点权值最小的两个结点*/
x1=x2=0;
/*找出所有结点中权值最小、无父结点的两个结点,并合并之为一颗二叉树*/ for (j=0; j<n+i; j++)
#define MAXVALUE 1000 /*定义最大权值*/
#define MAXLEAF 30 /*定义哈夫曼树叶结点个数*/
#define MAXNODE MAXLEAF*2-1
#define MAXBIT 30 /*定义哈夫曼编码的最大长度*/
typedef struct
{ int bit[MAXBIT];
/*求编码的低一位*/
c=p;
p=HuffNode[c].parent;
/*设置下一循环条件*
} /* end while */
/*保存求出的每个叶结点的哈夫曼编码和编码的起始位*/
for (j=cd.start+1; j<n; j++)
{
HuffCode[i].bit[j] = cd.bit[j];}
} /* end for */
/*
for(i=0;i<n+2;i++)
{
printf("Parents:%d,lchild:%d,rchild:%d,value:%d,weight:%d\n",HuffNode[i].parent,HuffNode[i].lchild,HuffNode[i].rchild,HuffNode[i].value,HuffNode[i].weight);
int i, j, m1, m2, x1, x2;
/*初始化存放哈夫曼树数组HuffNode[]中的结点*/
for (i=0; i<2*n-1; i++)
{
HuffNode[i].weight = 0;//权值
HuffNode[i].parent =-1;
HuffNode[i].lchild =-1;
}
printf("\n");
}*/
printf("Decoding?Please Enter code:\n");
scanf("%s",&pp);decodeing(pp,HuffNode,n);
getch();
return 0;
}
解码
#include "string.h"
#include "stdio.h"
#define MAXVALUE 10000
#define MAXLEAF 30
#define MAXNODE MAXLEAF*2 -1
typedef struct {
int bit[MAXBIT];
int start;} HCodeType; /*编码结构体*/
typedef struct{
int weight;
* Name:哈夫曼编码源代码。
* Date: 2011.04.16 * Author: Jeffrey Hill+Jezze(解码部分)
*在Win-TC下测试通过
*实现过程:着先通过HuffmanTree()函数构造哈夫曼树,然后在主函数main()中
*自底向上开始(也就是从数组序号为零的结点开始)向上层层判断,若在
int start;
} HCODETYPE;
typedef struct
{ int weight;
int parent;
int lchild;
int rchild;
} HNODETYPE;
char *getcode1(char *s1,char *s2,char *s3) /*首先去掉电文中的空格*/
HuffCode[i].start = cd.start;
} /* end for */
/*输出已保存好的所有存在编码的哈夫曼编码*/
for (i=0; i<n; i++)
{
printf ("%d 's Huffman code is: ", i);
for (j=HuffCode[i].start+1; j < n; j++)
{
if (HuffNode[j].weight < m1 && HuffNode[j].parent==-1) {
m2=m1;
x2=x1;
m1=HuffNode[j].weight;
x1=j;
}
else if (HuffNode[j].weight < m2 && HuffNode[j].parent==-1) {
{
if(string[i]=='0')
num[i]=0;
else num[i]=1;
}
i=0;
nump=&num[0];
while(nump<(&num[strlen(string)]))
{tmp=m-1;
while((Buf[tmp].lchild!=-1)&&(Buf[tmp].rchild!=-1))
printf ("%d", HuffCode[i].bit[j]);
}
printf(" start:%d",HuffCode[i].start);
printf ("\n");
}/*
for(i=0;i<n;i++){
for(j=0;j<n;j++)
{
printf ("%d", HuffCode[i].bit[j]);
哈夫曼编码步骤:
一、对给定的n个权值{W1,W2,W3,...,Wi,...,Wn}构成n棵二叉树的初始集合F= {T1,T2,T3,...,Ti,...,Tn},其中每棵二叉树Ti中只有一个权值为Wi的根结点,它的左右子树均为空。(为方便在计算机上实现算法,一般还要求以Ti的权值Wi的升序排列。)
二、在F中选取两棵根结点权值最小的树作为新构造的二叉树的左右子树,新二叉树的根结点的权值为其左右子树的根结点的权值之和。
三、从F中删除这两棵树,并把这棵新的二叉树同样以升序排列加入到集合F中。
四、重复二和三两步,直到集合F中只有一棵二叉树为止。
/*-------------------------------------------------------------------------
return s1;
}
/*再去掉重复出现的字符(即压缩电文),提取哈夫曼树叶结点*/
char * getcode (char *s1)
{ char s2[26],s5[26];
char temp[200]="",*p,*q,*r,*s3="";
int m,e,n=0;
m=strlen(s1);
while(m>0)
{ char temp[128]="",*p,*q;
p=s1;
while ((q=strstr(p,s2))!=NULL)
{ *q='\0';
strcat(temp,p);
strcat(temp,s3);
p=q+strlen(s2); }
strcat(temp,p);
strcpy(s1,temp);
m2=HuffNode[j].weight;
x2=j;
}
} /* end for */
/*设置找到的两个子结点x1、x2的父结点信息*/
HuffNode[x1].parent = n+i;
HuffNode[x2].parent = n+i;
HuffNode[n+i].weight = HuffNode[x1].weight + HuffNode[x2].weight; HuffNode[n+i].lchild = x1;
HCODETYPE huffcode[MAXLEAF],cd;
int sum,i,j,n1,n2,x1,x2,p,k,c;
char s1[26]={'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'};
HuffNode[n+i].rchild = x2;
printf ("x1.weight and x2.weight in round %d: %d, %d\n", i+1, HuffNode[x1].weight, HuffNode[x2].weight);
/*用于测试*/
printf ("\n");
Fra Baidu bibliotekint parent;
int lchild;
int rchild;
int value;} HNodeType; /*结点结构体*/
/*构造一颗哈夫曼树*/
void HuffmanTree (HNodeType HuffNode[MAXNODE], int n){
/* i、j:循环变量,m1、m2:构造哈夫曼树不同过程中两个最小权值结点的权值,x1、x2:构造哈夫曼树不同过程中两个最小权值结点在数组中的序号。*/
{ p=s1;
s2[0]=s1[0];
s2[1]='\0';
r=s2;
e=0;
while((q=strstr(p,r))!=NULL)
{ *q='\0';
strcat(temp,p);
strcat(temp,s3);
p=q+strlen(s2);
e++; }
m-=e;
strcat(temp,p);
strcpy(s1,temp);
/*定义一个编码结构体数组,同时定义一个临时变量来存放求解编码时的信息*/ int i, j, c, p, n;
char pp[100];
printf ("Please input n:\n");
scanf ("%d", &n);
HuffmanTree (HuffNode, n);
for (i=0; i < n; i++)
{
cd.start = n-1;
c = i;
p = HuffNode[c].parent;
while (p != -1)
/*父结点存在*/
{
if (HuffNode[p].lchild == c)
cd.bit[cd.start] = 0;
else
cd.bit[cd.start] = 1;
cd.start--;
*父结点左侧,则置码为0,若在右侧,则置码为1。最后输出生成的编码。*------------------------------------------------------------------------*/
#include <stdio.h>
#include<stdlib.h>
#define MAXBIT 100
char s5[MAXLEAF];
int ww[26]={0},n=0;
strcpy( s5,s2);
sum=strlen(s2);
for(i=0;i<26;i++) /*统计所有字符出现的频度*/
for(j=0;j<sum;j++)
if(s2[j]==s1[i]) ww[i]++;
HuffNode[i].rchild =-1;
HuffNode[i].value=i;
//实际值,可根据情况替换为字母
} /* end for */
/*输入n个叶子结点的权值*/
for (i=0; i<n; i++)
{
printf ("Please input weight of leaf node %d: \n", i);
{
if(*nump==0)
{
tmp=Buf[tmp].lchild ;
}
else tmp=Buf[tmp].rchild;
nump++;
}
printf("%d",Buf[tmp].value);
}
}
int main(void){
HNodeType HuffNode[MAXNODE]; /*定义一个结点结构体数组*/ HCodeType HuffCode[MAXLEAF], cd;
}
*/
//测试
}
/* end HuffmanTree */
//解码
void decodeing(char string[],HNodeType Buf[],int Num){
int i,tmp=0,code[1024];
int m=2*Num-1;
char *nump;
char num[1024]
for(i=0;i<strlen(string);i++)
s5[n]=s2[0];
n++;
strcpy(temp,"");
}
s5[n]='\0';
strcpy(s1,s5);
printf("压缩后的电文(即叶结点): %s\n",s1);
return s1;
}
HNODETYPE huffmantree(char *s2,char s3[])
{ HNODETYPE huffnode[MAXNODE];
scanf ("%d", &HuffNode[i].weight);
} /* end for */
/*循环构造Huffman树*/
for (i=0; i<n-1; i++)
{
m1=m2=MAXVALUE;
/* m1、m2中存放两个无父结点且结点权值最小的两个结点*/
x1=x2=0;
/*找出所有结点中权值最小、无父结点的两个结点,并合并之为一颗二叉树*/ for (j=0; j<n+i; j++)
#define MAXVALUE 1000 /*定义最大权值*/
#define MAXLEAF 30 /*定义哈夫曼树叶结点个数*/
#define MAXNODE MAXLEAF*2-1
#define MAXBIT 30 /*定义哈夫曼编码的最大长度*/
typedef struct
{ int bit[MAXBIT];
/*求编码的低一位*/
c=p;
p=HuffNode[c].parent;
/*设置下一循环条件*
} /* end while */
/*保存求出的每个叶结点的哈夫曼编码和编码的起始位*/
for (j=cd.start+1; j<n; j++)
{
HuffCode[i].bit[j] = cd.bit[j];}
} /* end for */
/*
for(i=0;i<n+2;i++)
{
printf("Parents:%d,lchild:%d,rchild:%d,value:%d,weight:%d\n",HuffNode[i].parent,HuffNode[i].lchild,HuffNode[i].rchild,HuffNode[i].value,HuffNode[i].weight);
int i, j, m1, m2, x1, x2;
/*初始化存放哈夫曼树数组HuffNode[]中的结点*/
for (i=0; i<2*n-1; i++)
{
HuffNode[i].weight = 0;//权值
HuffNode[i].parent =-1;
HuffNode[i].lchild =-1;
}
printf("\n");
}*/
printf("Decoding?Please Enter code:\n");
scanf("%s",&pp);decodeing(pp,HuffNode,n);
getch();
return 0;
}
解码
#include "string.h"
#include "stdio.h"
#define MAXVALUE 10000
#define MAXLEAF 30
#define MAXNODE MAXLEAF*2 -1
typedef struct {
int bit[MAXBIT];
int start;} HCodeType; /*编码结构体*/
typedef struct{
int weight;
* Name:哈夫曼编码源代码。
* Date: 2011.04.16 * Author: Jeffrey Hill+Jezze(解码部分)
*在Win-TC下测试通过
*实现过程:着先通过HuffmanTree()函数构造哈夫曼树,然后在主函数main()中
*自底向上开始(也就是从数组序号为零的结点开始)向上层层判断,若在
int start;
} HCODETYPE;
typedef struct
{ int weight;
int parent;
int lchild;
int rchild;
} HNODETYPE;
char *getcode1(char *s1,char *s2,char *s3) /*首先去掉电文中的空格*/
HuffCode[i].start = cd.start;
} /* end for */
/*输出已保存好的所有存在编码的哈夫曼编码*/
for (i=0; i<n; i++)
{
printf ("%d 's Huffman code is: ", i);
for (j=HuffCode[i].start+1; j < n; j++)
{
if (HuffNode[j].weight < m1 && HuffNode[j].parent==-1) {
m2=m1;
x2=x1;
m1=HuffNode[j].weight;
x1=j;
}
else if (HuffNode[j].weight < m2 && HuffNode[j].parent==-1) {
{
if(string[i]=='0')
num[i]=0;
else num[i]=1;
}
i=0;
nump=&num[0];
while(nump<(&num[strlen(string)]))
{tmp=m-1;
while((Buf[tmp].lchild!=-1)&&(Buf[tmp].rchild!=-1))