-
Notifications
You must be signed in to change notification settings - Fork 0
/
Huffman.cpp
144 lines (142 loc) · 4.19 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
140
141
142
143
144
#include<iostream>
#include"linkStack.cpp"
#include<string>
template<class T>
class HuffmanTree
{
private:
struct Node{
T data;
int left,right,parent;
double pro;
Node(){}
Node(const T& m,double d,int x=-1,int y=-1,int z=-1):data(m),pro(d),left(x),right(y),parent(z){}
};
Node *p;
int size;
public:
void buildTree()
{
int index1,index2; //1为最小
double pro1,pro2;
T flag;
for(int i=size-2;i>=0;--i)
{
pro1=pro2=0x7fffffff;
for(int j=2*size-1;j>i;--j)
{
if(p[j].parent==-1)
{
if(p[j].pro<pro1)
{
index2=index1;
pro2=pro1;
index1=j;
pro1=p[j].pro;
}
else if(p[j].pro<pro2)
{
index2=j;
pro2=p[j].pro;
}
}
}
p[i]=Node(flag,pro1+pro2,index1,index2);
p[index1].parent=i;
p[index2].parent=i;
}
}
HuffmanTree(T *pointer,double*pro,int n)
{
size=n;
p=new Node[2*n-1];
for(int i=size-1;i<size*2-1;++i)
p[i]=Node(pointer[i-size+1],pro[i-size+1]);
buildTree();
}
HuffmanTree(int n) //n为需要编码的数量
{
size=n;
p=new Node[2*n-1];
T data;
double x;
std::cout<<"Please input data";
for(int i=size-1;i<2*size-1;++i) //?????
{
std::cin>>data;
std::cin>>x;
p[i]=Node(data,x);
}
buildTree();
}
~HuffmanTree(){ delete [] p;}
void show()
{
for(int i=0;i<2*size-1;++i)
{
std::cout<<p[i].data<<' '<<p[i].pro<<' '<<p[i].left<<' '<<p[i].right<<' '<<p[i].parent<<std::endl;
}
std::cout<<std::endl;
}
void getCode()
{
bool *CODE=new bool[size];
getCode(CODE);
}
void getCode(bool *CODE,int node=0,int last=0)//为了偷懒就不写成非递归啦,权值相同的节点的左右子树顺序不确定
{
if(p[node].left==-1&&p[node].right==-1)
{
std::cout<<"The code of "<<p[node].data<<" is ";
for(int i=0;i<last;++i)
std::cout<<CODE[i]?1:0;
std::cout<<std::endl;
return;
}
if(p[node].left!=-1){
CODE[last]=true;
getCode(CODE,p[node].left,last+1);
}
if(p[node].right!=-1){
CODE[last]=false;
getCode(CODE,p[node].right,last+1);
}
}
double WPL()
{
if(!size)
return 0;
double result=0;
linkStack<int> nS;
int index;
nS.push(0);
while(!nS.isEmpty())
{
index=nS.pop();
if(p[index].right!=-1)
nS.push(p[index].right);
if(p[index].left!=-1)
nS.push(p[index].left);
if(p[index].right!=-1&&p[index].left!=-1)
result+=p[index].pro;
}
return result;
}
};
// int main()
// {
// int n;
// char *pointer;
// double *weight;
// std::cin>>n;
// pointer=new char[n];
// weight=new double[n];
// for(int i=0;i<n;++i){
// std::cin>>weight[i];
// }
// HuffmanTree<char> s(pointer,weight,n);
// std::cout<<int(s.WPL());
// delete [] pointer;
// delete [] weight;
// return 0;
// }