-
Notifications
You must be signed in to change notification settings - Fork 0
/
Huffman.cpp
139 lines (103 loc) · 2.02 KB
/
Huffman.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
//Á¤»óÀ±_508
#include<stdio.h>
#include<stdlib.h>
typedef struct Element {
int value;
struct Element *left_child;
struct Element *right_child;
}Element;
typedef struct HeapType {
Element heap[30010];
int heap_size;
}HeapType;
Element et[35000];
int huff_size = 0;
void Insert(HeapType *H, Element e) {
int i;
i = ++(H->heap_size);
while ((i != 1) && (e.value < H->heap[i / 2].value)) {
H->heap[i] = H->heap[i / 2];
i = i / 2;
}
H->heap[i] = e;
}
void CreateHeap(HeapType *H, int size) {
int i;
H->heap_size = 0;
for (i = 0; i < size; i++) {
if (et[i].value) {
Insert(H, et[i]);
}
}
}
Element *Delete(HeapType *H) {
int parent = 1, child = 2;
Element *e = (Element*)malloc(sizeof(Element));
Element tmp;
*e = H->heap[parent];
tmp = H->heap[(H->heap_size)--];
while (child <= H->heap_size) {
if ((child < H->heap_size) && (H->heap[child].value > H->heap[child + 1].value)) {
child++;
}
if (tmp.value < H->heap[child].value) {
break;
}
H->heap[parent] = H->heap[child];
parent = child;
child *= 2;
}
H->heap[parent] = tmp;
return e;
}
Element *HuffmanTree(HeapType *H) {
Element *e1, *e2;
Element tmp;
while (H->heap_size > 1) {
e1 = Delete(H);
e2 = Delete(H);
tmp.value = e1->value + e2->value;
tmp.left_child = e1;
tmp.right_child = e2;
Insert(H, tmp);
}
return &(H->heap[1]);
}
void CheckHuff(Element *e, int size) {
if (e->left_child) {
CheckHuff(e->left_child, size + 1);
}
if (e->right_child) {
CheckHuff(e->right_child, size + 1);
}
if (!(e->left_child) && !(e->right_child)) {
huff_size += e->value * size;
}
}
int main() {
int N, num, S, i, k = 0;
char name[5];
HeapType H;
Element *e;
scanf("%d", &N);
for (i = 0; i < N; i++) {
scanf("%s %d", name, &num);
et[i].value = num;
et[i].left_child = NULL;
et[i].right_child = NULL;
}
scanf("%d", &S);
CreateHeap(&H, N);
e = HuffmanTree(&H);
CheckHuff(e, 0);
N--;
while (N > 0) {
k++;
N /= 2;
}
if (k == 0) {
k++;
}
printf("%d\n%d\n", k * S, huff_size);
return 0;
}