哈夫曼树编码 (c++)
实验题目(共6题, 第1题)
实验题目(共6题, 第1题)
| 标题: | Huffman树 |
| 时 限: | 1000 ms |
| 内存限制: | 10000 K |
| 总时限: | 3000 ms |
| 描述: |
Huffman树
对输入的英文大写字母进行统计概率 然后构建哈夫曼树,输出是按照概率降序排序输出Huffman编码。 |
| 输入: |
大写字母个数 n
第一个字母 第二个字母 第三个字母 ... 第n个字母 |
| 输出: |
字母1 出现次数 Huffman编码
字母2 出现次数 Huffman编码
字母3 出现次数 Huffman编码
…
字母n 出现次数 Huffman编码
|
| 输入样例: |
10
I I U U U I U N U U
|
| 输出样例: |
U 6 1
I 3 01
N 1 00
|
| 提示: | 参见教材144页 |
View Code
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5
6 #define MAX 27
7 #define MAX_INT 99999
8
9
10 //哈夫曼树和哈夫曼编码的存储表示
11 typedef struct
12 {
13 int weight;
14 int parent,lchild,rchild;
15 } HTNode,*HuffmanTree; // 动态分配数组存储哈夫曼树
16
17
18 typedef char **HuffmanCode;
19
20
21 typedef struct Charnode
22 {
23 char c;
24 int weight;
25 }CharNode,*CharNodePtr;//不要定义为Char
26
27
28 CharNode *b;
29
30
31 int Chat_get()
32 {
33 char c;
34 int j=0;
35 int m;
36 int i;
37 scanf("%d",&m);
38 getchar();
39 b=(CharNodePtr)malloc(sizeof(CharNode)*MAX);
40 int a[MAX];
41 for(i=0;i<MAX;i++)
42 {
43 a[i]=0;
44 }
45 for(i=0;i<m;i++)
46 {
47 scanf("%c",&c);
48 getchar();
49 a[c-'A']++;
50 }
51 for(i=0;i<26;i++)
52 {
53 if(a[i]!=0)
54 {
55 b[j].c=char(i+'A');
56 b[j].weight=a[i];
57 j++;
58 }
59 }
60 return j;
61 }//得到不同字符的个数和数组
62
63
64 int min(HuffmanTree t,int i)
65 {
66 int j,flag;
67 int k=MAX_INT; // 取k为不小于可能的值
68 for(j=1; j<=i; j++)
69 if(t[j].weight<k&&t[j].parent==0)
70 k=t[j].weight,flag=j;
71 t[flag].parent=1;
72 return flag;
73 }
74
75
76 //本实习题中右子树是最小值对应序号,左子树是次小值对应序号
77 void select(HuffmanTree t,int i,int &s1,int &s2)
78 {
79 s2=min(t,i);
80 s1=min(t,i);
81 }
82
83
84 void PrintHuffmanTree(HuffmanTree &HT,HuffmanCode &HC, int n)
85 {
86 int i, c, cdlen;
87 char *cd;
88 HC=(HuffmanCode)malloc((n+1)*sizeof(char*));
89 // 分配n个字符编码的头指针向量([0]不用)
90 cd=(char*)malloc(n*sizeof(char)); // 分配求编码的工作空间
91 c=2*n-1;
92 cdlen=0;
93 for(i=0; i<=c; ++i) HT[i].weight=0; // 遍历赫夫曼树时用作结点状态标志
94 while(c)
95 {
96 if(HT[c].weight==0) // 向左
97 {
98 HT[c].weight=1;
99 if(HT[c].lchild==0 && HT[c].rchild==0) // 登记叶子结点字符编码
100 {
101 HC[c]=(char *)malloc((cdlen+1)*sizeof(char));
102 cd[cdlen]='\0';
103 strcpy(HC[c],cd); // 复制编码(串)
104 }
105 if(HT[c].lchild!=0)
106 {
107 c=HT[c].lchild;
108 cd[cdlen++]='1';
109 }
110 }
111 else if(HT[c].weight==1) // 向右
112 {
113 HT[c].weight=2;
114 if(HT[c].rchild!=0)
115 {
116 c=HT[c].rchild;
117 cd[cdlen++]='0';
118 }
119 }
120 else
121 {
122 HT[c].weight=0;
123 c=HT[c].parent;
124 --cdlen; // 退到父结点,编码长度减1
125 }
126 }
127 free(cd);
128 }
129
130
131 // w存放n个字符的权值(均>0),构造哈夫曼树HT,并求出n个字符的哈夫曼编码HC
132 void HuffmanCoding(HuffmanTree &HT,HuffmanCode &HC,int *w,int n)
133 {
134 int m,i,s1,s2;
135
136
137 HuffmanTree p;
138
139
140 if(n<=1) exit(0);
141 m=2*n-1;
142 HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode)); // 0号单元未用
143 //因为0号单元未用,处理数据时候从1号单元开始
144 for(p=HT+1,i=1; i<=n; ++i,++p,++w)
145 {
146 (*p).weight=*w;
147 (*p).parent=0;
148 (*p).lchild=0;
149 (*p).rchild=0;
150 }
151 for(; i<=m; ++i,++p) (*p).parent=0;
152 // 在HT[1~i-1]中选择parent为0且weight最小的两个结点,其序号分别为s1和s2
153 for(i=n+1; i<=m; ++i) // 建哈夫曼树
154 {
155 select(HT,i-1,s1,s2);
156 HT[s1].parent=HT[s2].parent=i;
157 HT[i].lchild=s1;
158 HT[i].rchild=s2;
159 HT[i].weight=HT[s1].weight+HT[s2].weight;
160 }
161 //顺序输出哈夫曼树
162 PrintHuffmanTree(HT, HC, n);
163 }
164
165
166
167
168 void sort_b(int k)
169 {
170 int n;
171 char c;
172 int i,j;
173 for(i=0;i<k;i++)
174 {
175 for(j=k-1;j>=i;j--)
176 {
177 if(b[j].weight>b[j-1].weight)
178 {
179 n=b[j].weight; b[j].weight=b[j-1].weight;b[j-1].weight=n;
180 c=b[j].c; b[j].c=b[j-1].c; b[j-1].c=c;
181 }
182 }
183 }
184 }
185
186
187 int main()
188 {
189 int k;
190 int j;
191 int i;
192 HuffmanTree HT;
193 HuffmanCode HC;
194 k=Chat_get();
195 int *w;
196 w=(int *)malloc(sizeof(int)*k);
197 sort_b(k);
198 for(i=0;i<k;i++)
199 {
200 w[i]=b[i].weight;
201 }
202 HuffmanCoding(HT,HC,w,k);
203 for(i=0,j=1;i<k;j++,i++)
204 {
205 printf("%c %d %s\n",b[i].c,b[i].weight,HC[j]);
206 }
207 return 0;
208 }


浙公网安备 33010602011771号