-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgo6-2.cpp
51 lines (46 loc) · 1015 Bytes
/
algo6-2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// algo6-2.cpp 实现算法6.13的程序
#include"c1.h"
#include"c6-5.h"
#include"func6-2.cpp"
void HuffmanCoding(HuffmanTree &HT,HuffmanCode &HC,int* w,int n)
// 前半部分为算法6.12的前半部分
{
unsigned cdlen;
#include"func6-3.cpp"
HC=(HuffmanCode)malloc((n+1)*sizeof(char*));
cd=(char*)malloc(n*sizeof(char));
c=m;
cdlen=0;
for(i=1;i<=m;++i)
HT[i].weight=0;
while(c)
{ if(HT[c].weight==0)
{
HT[c].weight=1;
if(HT[c].lchild!=0)
{ c=HT[c].lchild;
cd[cdlen++]='0';
}
else if(HT[c].rchild==0)
{
HC[c]=(char*)malloc((cdlen+1)*sizeof(char));
cd[cdlen]='\0';
strcpy(HC[c],cd);
}
}
else if(HT[c].weight==1)
{
HT[c].weight=2;
if(HT[c].rchild!=0)
{ c=HT[c].rchild;
cd[cdlen++]='1';
}
}
else
{ c=HT[c].parent;
--cdlen;
}
}
free(cd);
}
#include"func6-4.cpp"