-
Notifications
You must be signed in to change notification settings - Fork 1
/
正常huffman.cpp
67 lines (60 loc) · 1.21 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
#include<iostream>
#include<cstdlib>
using namespace std;
struct node{
int weight;
struct node *left, *right;
}*head;
int sum;
int main(){
void create(),solve();
int m;
cin >> m;
while (m--){
create();
solve();
}
return 0;
}
void create(){
int n;
cin >> n;
head = new node[n+1];
for (int i = 0; i < n; i++){
cin >> head[i].weight;
head[i].left = head[i].right = NULL;
}
head[n].weight = 0;
}
void solve(){
sum = 0;
void sort();
void show(node * root, int level);
int i = 0;
while (head[1].weight){
sort();
for (i = 0; head[i + 2].weight; i++);
node *p1=new node , *p2 =new node , *q =new node;
*p1 = head[i], *p2 = head[i + 1];
q->left = p1; q->right = p2;q->weight = p1->weight + p2->weight;
head[i] = *q;head[i + 1] = head[i + 2];
}
show(head,0);
cout << sum<< endl;
}
void sort(){
for (int i=0;head[i].weight; i++)
for (int j = i + 1; head[j].weight; j++)
if (head[i].weight<head[j].weight){
node ntemp;
ntemp = head[i]; head[i] = head[j]; head[j] = ntemp;
}
}
void show(node * root,int level){
if (root->left)
show(root->left, level + 1);
if (root->right)
show(root->right, level + 1);
if (root->left==NULL && root->right==NULL)
sum += root->weight*level;
}