-
Notifications
You must be signed in to change notification settings - Fork 0
/
HuffmanTree.cpp
279 lines (247 loc) · 7.15 KB
/
HuffmanTree.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define MAX 1000
#define MIN -1000
using namespace std;
typedef struct Bean{
int num;
char c;
}Bean;
typedef struct HuffmanTree{
int weight;
char c;
HuffmanTree *left , *right;
}HuffmanTree;
typedef struct MinHeap{
int *num;
char *c;
int Size;
int Capacity;
}MinHeap;
void InitHeap(MinHeap **heap){
*heap = new MinHeap;
int n;
cout <<"请输入最大容量:" <<endl;
cin >>n;
(*heap)->num = (int *)malloc(n * sizeof(heap));
(*heap)->num[0] = MIN;
(*heap)->Capacity = n;
(*heap)->Size = 0;
(*heap)->c = (char *)malloc(n * sizeof(heap));
(*heap)->c[0] = '#';
}
bool isFull(MinHeap *heap){
return heap->Size == heap->Capacity;
}
bool isEmpty(MinHeap *heap){
return heap->Size == 0;
}
void CreateOriginHeap(MinHeap *heap){
int n;
char c;
cout << "请输入字符及权值(以\"# 0\"结束):" <<endl;
cin >> c >> n;
while(c != '#' && !isFull(heap)){
heap->num[++ heap->Size] = n;
heap->c[heap->Size] = c;
cin >> c >>n;
}
}
void PrintHeap(MinHeap *heap){
for(int i = 1 ;i <= heap->Size ;i ++){
cout << heap->num[i] <<heap->c[i]<< " ";
}
cout << ""<<endl;
}
void PercDown(MinHeap *heap , int p){
int Parent , Child;
int X = heap->num[p];
char c = heap->c[p];
for(Parent = p; Parent*2 <= heap->Size;Parent = Child){
Child = Parent *2;
if(heap->num[Child] > heap->num[Child+ 1])
Child ++;
if(heap->num[Child] < X){
heap->num[Parent] = heap->num[Child];
heap->c[Parent] = heap->c[Child];
}else{
break;
}
}
heap->num[Parent] = X;
heap->c[Parent] = c;
}
Bean *DeleteNode(MinHeap *heap){
int minNode = heap->num[1];
char minc = heap->c[1];
Bean *bean = new Bean;
bean->c = minc;
bean->num = minNode;
cout << "未排序"<<endl;
PrintHeap(heap);
heap->num[1] = heap->num[heap->Size];
heap->c[1] = heap->c[heap->Size];
cout << "删除节点"<<endl;
PrintHeap(heap);
heap->Size --;
PercDown(heap , 1);
return bean;
}
void PreOrderTraversal(HuffmanTree *huffmanTree){
if(huffmanTree != NULL){
cout << huffmanTree->c << huffmanTree->weight << endl;
PreOrderTraversal(huffmanTree->left);
PreOrderTraversal(huffmanTree->right);
}
}
MinHeap *InsertHeap(MinHeap *heap , int num , char c){
if(!isFull(heap)){
heap->num[++ heap->Size] = num;
heap->c[heap->Size] = c;
PercDown(heap , 1);
}
return heap;
}
HuffmanTree *CreateHuffmanTreeNode(Bean *num , HuffmanTree *huffmanTree ,MinHeap *heap , bool isleft){
HuffmanTree *T = new HuffmanTree;
T->c = '#';
HuffmanTree *t = new HuffmanTree;
if(isleft){
t->weight = num->num;
t->c = num->c;
t->left = t->right = NULL;
T->left = huffmanTree->left;
T->right = t;
T->weight = huffmanTree->left->weight + t->weight;
huffmanTree->weight += num->num;
huffmanTree->left = T;
InsertHeap(heap , T->weight , T->c);
}else{
t->weight = num->num;
t->c = num->c;
t->left = t->right = NULL;
T->right = huffmanTree->right;
T->left = t;
T->weight = huffmanTree->right->weight + t->weight;
huffmanTree->weight += num->num;
huffmanTree->right = T;
InsertHeap(heap , T->weight ,T->c);
}
return huffmanTree;
}
HuffmanTree *InitHuffmanTree(MinHeap *heap){
Bean *a , *b;
a = DeleteNode(heap);
b = DeleteNode(heap);
HuffmanTree *huffmanTree = new HuffmanTree;
HuffmanTree *left = new HuffmanTree;
HuffmanTree *right = new HuffmanTree;
huffmanTree->left = left;
huffmanTree->right = right;
huffmanTree->c = '#';
HuffmanTree *t1 = new HuffmanTree;
t1->left = t1->right = NULL;
t1->weight = a->num;
t1->c = a->c;
HuffmanTree *t2 = new HuffmanTree;
t2->left = t2->right = NULL;
t2->weight = b->num;
t2->c = b->c;
left->weight = a->num + b->num;
left->c = '#';
left->left = t1;
left->right = t2;
right->left = right->right = NULL;
right->weight = 0;
right->c = '#';
huffmanTree->weight = a->num + b->num;
InsertHeap(heap , a->num + b->num , '#');
return huffmanTree;
}
HuffmanTree *CreateHuffmanTree(HuffmanTree *huffmanTree ,MinHeap *heap){
while(heap->Size > 2){
Bean *a , *b;
a = DeleteNode(heap);
b = DeleteNode(heap);
cout << "删除数" <<a->c<<a->num << b->c<<b->num<<endl;
if(a->num > b->num){
int t = a->num;
char c = a->c;
a->num = b->num;
a->c = b->c;
b->c = a->c;
b->num = c;
}
if(a->num != huffmanTree->left->weight && b->num != huffmanTree->right->weight){
HuffmanTree *t1 = new HuffmanTree;
HuffmanTree *t2 = new HuffmanTree;
t1->weight = a->num;
t1->c = a->c;
t1->left= t1->right = NULL;
t2->weight = b->num;
t2->c = b->c;
t2->left= t2->right = NULL;
huffmanTree->right->right = t1;
huffmanTree->right->left = t2;
huffmanTree->right->weight = huffmanTree->right->right->weight + huffmanTree->right->left->weight;
huffmanTree->weight = huffmanTree->left->weight + huffmanTree->right->weight;
InsertHeap(heap , a->num +b->num , '#');
}else{
if(a->num == huffmanTree->left->weight){
if(a->c == '#')
CreateHuffmanTreeNode(b , huffmanTree,heap , true);
else
CreateHuffmanTreeNode(a , huffmanTree,heap , true);
}else{
if(a->c == '#')
CreateHuffmanTreeNode(a , huffmanTree,heap , true);
else
CreateHuffmanTreeNode(b , huffmanTree,heap , true);
}
}
}
return huffmanTree;
}
void PrintHuffmanCode(HuffmanTree *huffmanTree , char *code , int n){
if(huffmanTree != NULL){
if(huffmanTree->c != '#'){
cout << huffmanTree->c <<endl;
for(int i = 1;i <= n ;i ++){
cout <<code[i];
}
cout <<""<<endl;
}
code[n] = '0';
PrintHuffmanCode(huffmanTree->left , code , n + 1);
code[n] = '1';
PrintHuffmanCode(huffmanTree->right , code , n + 1);
}
}
int main(){
int num;
MinHeap *minHeap;
HuffmanTree *huffmanTree;
InitHeap(&minHeap);
CreateOriginHeap(minHeap);
num = minHeap->Size;
cout << "原始二叉树" <<endl;
PrintHeap(minHeap);
for(int i = minHeap->Size/2 ;i > 0 ;i --){
PercDown(minHeap , i);
}
cout <<"最小堆" <<endl;
PrintHeap(minHeap);
cout << "HuffmanTree :" <<endl;
huffmanTree = InitHuffmanTree(minHeap);
huffmanTree = CreateHuffmanTree(huffmanTree , minHeap);
PreOrderTraversal(huffmanTree);
cout << "Huffman编码 :" <<endl;
int n = 1;
while(num < pow(2,n - 1) || num > pow(2 , n))
n ++;
char code[n];
PrintHuffmanCode(huffmanTree , code , 1);
return 0;
}