Status CreateHList(HuffmanCode HT,HList &HL)
{
HTNode *P;HCode tmp;
P=HT;P++;
int i;i=0;
while(P->lchild==0&&P->rchild==0)
{
i++;P++;
}//求HT初态的长度.
printf("Length Of Initiation:%d/n",i);
InitialLength=i;
P=HT;P++;
HL=(HList)malloc((i+1)*sizeof(HCode));
HL->data='#';HL->HFC='/0';
int j;int x;
for(j=1;j<=i;j++)
{
TransCode(HT,tmp,j);
(HL+j)->HFC=(char *)malloc(sizeof(char)*tmp.HFCLength);
(HL+j)->data=tmp.data;
strcpy((HL+j)->HFC,tmp.HFC);
(HL+j)->HFCLength=tmp.HFCLength;
}
printf("CreateHList succeed!/n");
return OK;
}//CreateHList构建哈夫曼编码的存储表,哪个字母表示哪个字符串.
{
HTNode *P;HCode tmp;
P=HT;P++;
int i;i=0;
while(P->lchild==0&&P->rchild==0)
{
i++;P++;
}//求HT初态的长度.
printf("Length Of Initiation:%d/n",i);
InitialLength=i;
P=HT;P++;
HL=(HList)malloc((i+1)*sizeof(HCode));
HL->data='#';HL->HFC='/0';
int j;int x;
for(j=1;j<=i;j++)
{
TransCode(HT,tmp,j);
(HL+j)->HFC=(char *)malloc(sizeof(char)*tmp.HFCLength);
(HL+j)->data=tmp.data;
strcpy((HL+j)->HFC,tmp.HFC);
(HL+j)->HFCLength=tmp.HFCLength;
}
printf("CreateHList succeed!/n");
return OK;
}//CreateHList构建哈夫曼编码的存储表,哪个字母表示哪个字符串.
Status PrintHList(HCode *HC)
{
HCode *P;char *tmp;int i;int j;P=HC;char s;
P++;
printf("data ,Code /n");
for(j=0;j<InitialLength;j++)
{
printf("%5c ,",P->data);
for(i=0;i<P->HFCLength;i++)
{
s=*((P->HFC)+i);
printf("%3c",s);
}
printf("/n");
P++;
}
return OK;
}//PrintHList输出哈夫曼储表中的内容
{
HCode *P;char *tmp;int i;int j;P=HC;char s;
P++;
printf("data ,Code /n");
for(j=0;j<InitialLength;j++)
{
printf("%5c ,",P->data);
for(i=0;i<P->HFCLength;i++)
{
s=*((P->HFC)+i);
printf("%3c",s);
}
printf("/n");
P++;
}
return OK;
}//PrintHList输出哈夫曼储表中的内容
Status TransDToCode(char c,HList HL)
{
int i,j;char *d,*t,s;HCode *P;P=HL;P++;
for(i=0;i<InitialLength;i++)
{
if((P->data)==c)
{
for(j=0;j<P->HFCLength;j++)
{
s=*((P->HFC)+j);
printf("%2c ",s);
}
}
if(i<InitialLength)
P++;
}
printf("__");
return OK;
}//TransDToCode把字符变成相对应编码.
{
int i,j;char *d,*t,s;HCode *P;P=HL;P++;
for(i=0;i<InitialLength;i++)
{
if((P->data)==c)
{
for(j=0;j<P->HFCLength;j++)
{
s=*((P->HFC)+j);
printf("%2c ",s);
}
}
if(i<InitialLength)
P++;
}
printf("__");
return OK;
}//TransDToCode把字符变成相对应编码.
Status ReceiveCode(HList HL)
{
char c;char d[100];char *t;t=d;int i;i=0;int j=0;HCode *P;P=HL;P++;
scanf("%s",d);
printf("the char to HuffmanCode is:/n");
while(c!='#')
{
c=d[i];i++;
if(c=='#')
break;
TransDToCode(c,HL);
}
// delete []d;
return OK;
}//接收字符
{
char c;char d[100];char *t;t=d;int i;i=0;int j=0;HCode *P;P=HL;P++;
scanf("%s",d);
printf("the char to HuffmanCode is:/n");
while(c!='#')
{
c=d[i];i++;
if(c=='#')
break;
TransDToCode(c,HL);
}
// delete []d;
return OK;
}//接收字符
Status TransNToD(char *d,int i,HList HL)
{
int j;printf("/n你输入的01字符串是:");
for(j=0;j<i;j++)
{
printf("%c",*(d+j));
}//d[0]是个回车符.
printf("/n");
char *t;int x;int m;int charCount=0;char s;
HList P;P=HL;P++;
t=(char *)malloc(sizeof(char)*P->HFCLength);
strcpy(t,P->HFC);
while(charCount<i-1)
{
for(j=0;j<InitialLength;j++)
{
t=(char *)malloc(sizeof(char)*(P->HFCLength));
strcpy(t,P->HFC);m=0;
for(x=0;x<(P->HFCLength);x++)
{
s=*t;
if(s==(*(d+1)))
{
m++;t++;d++;
}
else
break;
}
if(m==P->HFCLength)
{
charCount=charCount+(P->HFCLength);
printf("%3c,",P->data);
break;
}
else
{
d=d-m;
}
P++;
}
}
return OK;
}//把01字符串解析成字母
{
int j;printf("/n你输入的01字符串是:");
for(j=0;j<i;j++)
{
printf("%c",*(d+j));
}//d[0]是个回车符.
printf("/n");
char *t;int x;int m;int charCount=0;char s;
HList P;P=HL;P++;
t=(char *)malloc(sizeof(char)*P->HFCLength);
strcpy(t,P->HFC);
while(charCount<i-1)
{
for(j=0;j<InitialLength;j++)
{
t=(char *)malloc(sizeof(char)*(P->HFCLength));
strcpy(t,P->HFC);m=0;
for(x=0;x<(P->HFCLength);x++)
{
s=*t;
if(s==(*(d+1)))
{
m++;t++;d++;
}
else
break;
}
if(m==P->HFCLength)
{
charCount=charCount+(P->HFCLength);
printf("%3c,",P->data);
break;
}
else
{
d=d-m;
}
P++;
}
}
return OK;
}//把01字符串解析成字母
Status ReceiveNum(HList HL)
{
char c;char d[100];char *t;t=d;
int i=0;int j=0;
HCode *P;P=HL;P++;
while(c!='#')
{
scanf("%c",&c);
d[i]=c;
i++;
}
TransNToD(d,i-1,HL);
return OK;
}//接收01组成的字符串
{
char c;char d[100];char *t;t=d;
int i=0;int j=0;
HCode *P;P=HL;P++;
while(c!='#')
{
scanf("%c",&c);
d[i]=c;
i++;
}
TransNToD(d,i-1,HL);
return OK;
}//接收01组成的字符串
int main()
{
HTNode *HN;HList HL1;
HuffmanCode HF;
CreateHTreeList(HF);
PrintHTree(HF);
CreateHTree(HF);
PrintHTree(HF);
HList HL;
CreateHList(HF,HL);
HL1=HL;
PrintHList(HL1);
{
HTNode *HN;HList HL1;
HuffmanCode HF;
CreateHTreeList(HF);
PrintHTree(HF);
CreateHTree(HF);
PrintHTree(HF);
HList HL;
CreateHList(HF,HL);
HL1=HL;
PrintHList(HL1);
printf("请输入要转换的字符,以#结尾:");
ReceiveCode(HL);
printf("请输入要转换的01字符串,以#结尾:");
ReceiveNum(HL);
printf("/n为输出的对应字符串");
return 1;
}
ReceiveCode(HL);
printf("请输入要转换的01字符串,以#结尾:");
ReceiveNum(HL);
printf("/n为输出的对应字符串");
return 1;
}
/*输入输出结果,按照严慰敏书P149页上的赫夫曼编码表具体算法为147页算法6.12
哈夫曼编码表的字母个数:8
0, *, 0, 0, 0, 0
输入字母和权值:
a 5
输入字母和权值:
b 29
输入字母和权值:
c 7
输入字母和权值:
d 8
输入字母和权值:
e 14
输入字母和权值:
f 23
输入字母和权值:
g 3
输入字母和权值:
h 11
1, a, 5, 0, 0, 0
2, b, 29, 0, 0, 0
3, c, 7, 0, 0, 0
4, d, 8, 0, 0, 0
5, e, 14, 0, 0, 0
6, f, 23, 0, 0, 0
7, g, 3, 0, 0, 0
8, h, 11, 0, 0, 0
num ,data ,weight ,parent ,lchild ,rchild
1 , a , 5 , 0 , 0 , 0
2 , b , 29 , 0 , 0 , 0
3 , c , 7 , 0 , 0 , 0
4 , d , 8 , 0 , 0 , 0
5 , e , 14 , 0 , 0 , 0
6 , f , 23 , 0 , 0 , 0
7 , g , 3 , 0 , 0 , 0
8 , h , 11 , 0 , 0 , 0
i=1
0, *, 0, 0, 0, 0
输入字母和权值:
a 5
输入字母和权值:
b 29
输入字母和权值:
c 7
输入字母和权值:
d 8
输入字母和权值:
e 14
输入字母和权值:
f 23
输入字母和权值:
g 3
输入字母和权值:
h 11
1, a, 5, 0, 0, 0
2, b, 29, 0, 0, 0
3, c, 7, 0, 0, 0
4, d, 8, 0, 0, 0
5, e, 14, 0, 0, 0
6, f, 23, 0, 0, 0
7, g, 3, 0, 0, 0
8, h, 11, 0, 0, 0
num ,data ,weight ,parent ,lchild ,rchild
1 , a , 5 , 0 , 0 , 0
2 , b , 29 , 0 , 0 , 0
3 , c , 7 , 0 , 0 , 0
4 , d , 8 , 0 , 0 , 0
5 , e , 14 , 0 , 0 , 0
6 , f , 23 , 0 , 0 , 0
7 , g , 3 , 0 , 0 , 0
8 , h , 11 , 0 , 0 , 0
i=1
Select Succeednow ListLength=8
num ,data ,weight ,parent ,lchild ,rchild
1 , a , 5 , 9 , 0 , 0
2 , b , 29 , 0 , 0 , 0
3 , c , 7 , 0 , 0 , 0
4 , d , 8 , 0 , 0 , 0
5 , e , 14 , 0 , 0 , 0
6 , f , 23 , 0 , 0 , 0
7 , g , 3 , 9 , 0 , 0
8 , h , 11 , 0 , 0 , 0
9 , * , 8 , 0 , 1 , 7
i=2
num ,data ,weight ,parent ,lchild ,rchild
1 , a , 5 , 9 , 0 , 0
2 , b , 29 , 0 , 0 , 0
3 , c , 7 , 0 , 0 , 0
4 , d , 8 , 0 , 0 , 0
5 , e , 14 , 0 , 0 , 0
6 , f , 23 , 0 , 0 , 0
7 , g , 3 , 9 , 0 , 0
8 , h , 11 , 0 , 0 , 0
9 , * , 8 , 0 , 1 , 7
i=2
Select Succeednow ListLength=9
num ,data ,weight ,parent ,lchild ,rchild
1 , a , 5 , 9 , 0 , 0
2 , b , 29 , 0 , 0 , 0
3 , c , 7 , 10 , 0 , 0
4 , d , 8 , 10 , 0 , 0
5 , e , 14 , 0 , 0 , 0
6 , f , 23 , 0 , 0 , 0
7 , g , 3 , 9 , 0 , 0
8 , h , 11 , 0 , 0 , 0
9 , * , 8 , 0 , 1 , 7
10 , * , 15 , 0 , 4 , 3
i=3
num ,data ,weight ,parent ,lchild ,rchild
1 , a , 5 , 9 , 0 , 0
2 , b , 29 , 0 , 0 , 0
3 , c , 7 , 10 , 0 , 0
4 , d , 8 , 10 , 0 , 0
5 , e , 14 , 0 , 0 , 0
6 , f , 23 , 0 , 0 , 0
7 , g , 3 , 9 , 0 , 0
8 , h , 11 , 0 , 0 , 0
9 , * , 8 , 0 , 1 , 7
10 , * , 15 , 0 , 4 , 3
i=3
Select Succeednow ListLength=10
num ,data ,weight ,parent ,lchild ,rchild
1 , a , 5 , 9 , 0 , 0
2 , b , 29 , 0 , 0 , 0
3 , c , 7 , 10 , 0 , 0
4 , d , 8 , 10 , 0 , 0
5 , e , 14 , 0 , 0 , 0
6 , f , 23 , 0 , 0 , 0
7 , g , 3 , 9 , 0 , 0
8 , h , 11 , 11 , 0 , 0
9 , * , 8 , 11 , 1 , 7
10 , * , 15 , 0 , 4 , 3
11 , * , 19 , 0 , 8 , 9
i=4
num ,data ,weight ,parent ,lchild ,rchild
1 , a , 5 , 9 , 0 , 0
2 , b , 29 , 0 , 0 , 0
3 , c , 7 , 10 , 0 , 0
4 , d , 8 , 10 , 0 , 0
5 , e , 14 , 0 , 0 , 0
6 , f , 23 , 0 , 0 , 0
7 , g , 3 , 9 , 0 , 0
8 , h , 11 , 11 , 0 , 0
9 , * , 8 , 11 , 1 , 7
10 , * , 15 , 0 , 4 , 3
11 , * , 19 , 0 , 8 , 9
i=4
Select Succeednow ListLength=11
num ,data ,weight ,parent ,lchild ,rchild
1 , a , 5 , 9 , 0 , 0
2 , b , 29 , 0 , 0 , 0
3 , c , 7 , 10 , 0 , 0
4 , d , 8 , 10 , 0 , 0
5 , e , 14 , 12 , 0 , 0
6 , f , 23 , 0 , 0 , 0
7 , g , 3 , 9 , 0 , 0
8 , h , 11 , 11 , 0 , 0
9 , * , 8 , 11 , 1 , 7
10 , * , 15 , 12 , 4 , 3
11 , * , 19 , 0 , 8 , 9
12 , * , 29 , 0 , 5 , 10
i=5
num ,data ,weight ,parent ,lchild ,rchild
1 , a , 5 , 9 , 0 , 0
2 , b , 29 , 0 , 0 , 0
3 , c , 7 , 10 , 0 , 0
4 , d , 8 , 10 , 0 , 0
5 , e , 14 , 12 , 0 , 0
6 , f , 23 , 0 , 0 , 0
7 , g , 3 , 9 , 0 , 0
8 , h , 11 , 11 , 0 , 0
9 , * , 8 , 11 , 1 , 7
10 , * , 15 , 12 , 4 , 3
11 , * , 19 , 0 , 8 , 9
12 , * , 29 , 0 , 5 , 10
i=5
Select Succeednow ListLength=12
num ,data ,weight ,parent ,lchild ,rchild
1 , a , 5 , 9 , 0 , 0
2 , b , 29 , 0 , 0 , 0
3 , c , 7 , 10 , 0 , 0
4 , d , 8 , 10 , 0 , 0
5 , e , 14 , 12 , 0 , 0
6 , f , 23 , 13 , 0 , 0
7 , g , 3 , 9 , 0 , 0
8 , h , 11 , 11 , 0 , 0
9 , * , 8 , 11 , 1 , 7
10 , * , 15 , 12 , 4 , 3
11 , * , 19 , 13 , 8 , 9
12 , * , 29 , 0 , 5 , 10
13 , * , 42 , 0 , 6 , 11
i=6
num ,data ,weight ,parent ,lchild ,rchild
1 , a , 5 , 9 , 0 , 0
2 , b , 29 , 0 , 0 , 0
3 , c , 7 , 10 , 0 , 0
4 , d , 8 , 10 , 0 , 0
5 , e , 14 , 12 , 0 , 0
6 , f , 23 , 13 , 0 , 0
7 , g , 3 , 9 , 0 , 0
8 , h , 11 , 11 , 0 , 0
9 , * , 8 , 11 , 1 , 7
10 , * , 15 , 12 , 4 , 3
11 , * , 19 , 13 , 8 , 9
12 , * , 29 , 0 , 5 , 10
13 , * , 42 , 0 , 6 , 11
i=6
Select Succeednow ListLength=13
num ,data ,weight ,parent ,lchild ,rchild
1 , a , 5 , 9 , 0 , 0
2 , b , 29 , 14 , 0 , 0
3 , c , 7 , 10 , 0 , 0
4 , d , 8 , 10 , 0 , 0
5 , e , 14 , 12 , 0 , 0
6 , f , 23 , 13 , 0 , 0
7 , g , 3 , 9 , 0 , 0
8 , h , 11 , 11 , 0 , 0
9 , * , 8 , 11 , 1 , 7
10 , * , 15 , 12 , 4 , 3
11 , * , 19 , 13 , 8 , 9
12 , * , 29 , 14 , 5 , 10
13 , * , 42 , 0 , 6 , 11
14 , * , 58 , 0 , 2 , 12
i=7
num ,data ,weight ,parent ,lchild ,rchild
1 , a , 5 , 9 , 0 , 0
2 , b , 29 , 14 , 0 , 0
3 , c , 7 , 10 , 0 , 0
4 , d , 8 , 10 , 0 , 0
5 , e , 14 , 12 , 0 , 0
6 , f , 23 , 13 , 0 , 0
7 , g , 3 , 9 , 0 , 0
8 , h , 11 , 11 , 0 , 0
9 , * , 8 , 11 , 1 , 7
10 , * , 15 , 12 , 4 , 3
11 , * , 19 , 13 , 8 , 9
12 , * , 29 , 14 , 5 , 10
13 , * , 42 , 0 , 6 , 11
14 , * , 58 , 0 , 2 , 12
i=7
Select Succeednow ListLength=14
num ,data ,weight ,parent ,lchild ,rchild
1 , a , 5 , 9 , 0 , 0
2 , b , 29 , 14 , 0 , 0
3 , c , 7 , 10 , 0 , 0
4 , d , 8 , 10 , 0 , 0
5 , e , 14 , 12 , 0 , 0
6 , f , 23 , 13 , 0 , 0
7 , g , 3 , 9 , 0 , 0
8 , h , 11 , 11 , 0 , 0
9 , * , 8 , 11 , 1 , 7
10 , * , 15 , 12 , 4 , 3
11 , * , 19 , 13 , 8 , 9
12 , * , 29 , 14 , 5 , 10
13 , * , 42 , 15 , 6 , 11
14 , * , 58 , 15 , 2 , 12
15 , * , 100 , 0 , 14 , 13
CreateHTreeSucceed
ListLength=15
num ,data ,weight ,parent ,lchild ,rchild
1 , a , 5 , 9 , 0 , 0
2 , b , 29 , 14 , 0 , 0
3 , c , 7 , 10 , 0 , 0
4 , d , 8 , 10 , 0 , 0
5 , e , 14 , 12 , 0 , 0
6 , f , 23 , 13 , 0 , 0
7 , g , 3 , 9 , 0 , 0
8 , h , 11 , 11 , 0 , 0
9 , * , 8 , 11 , 1 , 7
10 , * , 15 , 12 , 4 , 3
11 , * , 19 , 13 , 8 , 9
12 , * , 29 , 14 , 5 , 10
13 , * , 42 , 15 , 6 , 11
14 , * , 58 , 15 , 2 , 12
15 , * , 100 , 0 , 14 , 13
Length Of Initiation:8
0 1 1 1 (P+i)->data=a
0 0 (P+i)->data=b
1 1 1 0 (P+i)->data=c
0 1 1 0 (P+i)->data=d
0 1 0 (P+i)->data=e
0 1 (P+i)->data=f
1 1 1 1 (P+i)->data=g
0 1 1 (P+i)->data=h
CreateHList succeed!
data ,Code
a , 1 1 1 0
b , 0 0
c , 0 1 1 1
d , 0 1 1 0
e , 0 1 0
f , 1 0
g , 1 1 1 1
h , 1 1 0
请输入要转换的字符,以#结尾:aaacccdddfffhhh#
the char to HuffmanCode is:
1 1 1 0 __ 1 1 1 0 __ 1 1 1 0 __ 0 1 1 1 __ 0 1 1 1 __ 0 1 1
1 __ 0 1 1 0 __ 0 1 1 0 __ 0 1 1 0 __ 1 0 __ 1 0 __ 1 0 __ 1 1 0 _
_ 1 1 0 __ 1 1 0 __请输入要转换的01字符串,以#结尾:1110000001110110010#
num ,data ,weight ,parent ,lchild ,rchild
1 , a , 5 , 9 , 0 , 0
2 , b , 29 , 14 , 0 , 0
3 , c , 7 , 10 , 0 , 0
4 , d , 8 , 10 , 0 , 0
5 , e , 14 , 12 , 0 , 0
6 , f , 23 , 13 , 0 , 0
7 , g , 3 , 9 , 0 , 0
8 , h , 11 , 11 , 0 , 0
9 , * , 8 , 11 , 1 , 7
10 , * , 15 , 12 , 4 , 3
11 , * , 19 , 13 , 8 , 9
12 , * , 29 , 14 , 5 , 10
13 , * , 42 , 15 , 6 , 11
14 , * , 58 , 15 , 2 , 12
15 , * , 100 , 0 , 14 , 13
CreateHTreeSucceed
ListLength=15
num ,data ,weight ,parent ,lchild ,rchild
1 , a , 5 , 9 , 0 , 0
2 , b , 29 , 14 , 0 , 0
3 , c , 7 , 10 , 0 , 0
4 , d , 8 , 10 , 0 , 0
5 , e , 14 , 12 , 0 , 0
6 , f , 23 , 13 , 0 , 0
7 , g , 3 , 9 , 0 , 0
8 , h , 11 , 11 , 0 , 0
9 , * , 8 , 11 , 1 , 7
10 , * , 15 , 12 , 4 , 3
11 , * , 19 , 13 , 8 , 9
12 , * , 29 , 14 , 5 , 10
13 , * , 42 , 15 , 6 , 11
14 , * , 58 , 15 , 2 , 12
15 , * , 100 , 0 , 14 , 13
Length Of Initiation:8
0 1 1 1 (P+i)->data=a
0 0 (P+i)->data=b
1 1 1 0 (P+i)->data=c
0 1 1 0 (P+i)->data=d
0 1 0 (P+i)->data=e
0 1 (P+i)->data=f
1 1 1 1 (P+i)->data=g
0 1 1 (P+i)->data=h
CreateHList succeed!
data ,Code
a , 1 1 1 0
b , 0 0
c , 0 1 1 1
d , 0 1 1 0
e , 0 1 0
f , 1 0
g , 1 1 1 1
h , 1 1 0
请输入要转换的字符,以#结尾:aaacccdddfffhhh#
the char to HuffmanCode is:
1 1 1 0 __ 1 1 1 0 __ 1 1 1 0 __ 0 1 1 1 __ 0 1 1 1 __ 0 1 1
1 __ 0 1 1 0 __ 0 1 1 0 __ 0 1 1 0 __ 1 0 __ 1 0 __ 1 0 __ 1 1 0 _
_ 1 1 0 __ 1 1 0 __请输入要转换的01字符串,以#结尾:1110000001110110010#
你输入的01字符串是:
1110000001110110010
a, b, b, c, d, e,
为输出的对应字符串Press any key to continue*/
1110000001110110010
a, b, b, c, d, e,
为输出的对应字符串Press any key to continue*/