-
Notifications
You must be signed in to change notification settings - Fork 0
/
huffman.cpp
198 lines (170 loc) · 4.33 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
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
#include<iostream>
#include<string>
#include<cstring>
struct HuffmanNode
{
int weight;
char value;
std::string code;
int lIndex,rIndex,parentIndex;
};
class HuffmanCode
{
public:
explicit HuffmanCode(const std::string &str); //构造函数,在构造函数中实现编码全过程
~HuffmanCode(); //析构函数,释放分配的内存
void setMin(int& lMinIndex,int& rMinIndex,int parentIndex);
void merge(int lMinIndex,int rMinIndex,int parentIndex);
void coding();
void decode(const std::string& str);
private:
int leafNumber; //存储不同字符的个数 也就是哈夫曼树叶子节点个数
HuffmanNode* huffmanTree;
};
HuffmanCode::HuffmanCode(const std:string& str)
{
huffmanTree = new HuffmanNode[256];
leafNumber = 0;
int length = str.size();
int ascii[256];
memset(ascii,0,sizeof(ascii));
for(int i = 0;i < (2 * length - 1);i++)
{
huffmanTree[i].parentIndex = huffmanTree[i].rIndex = huffmanTree[i].lIndex = -1;
huffmanTree[i].code = "";
}
//记录每个字符的权重
for(int i = 0;i < length;i++)
{
ascii[str[i]]++;
}
//记录所有叶子节点的值和权重
for(int i = 0;i < 256;i++)
{
if(ascii[i] != 0)
{
huffmanTree[leafNumber].value = (char)i; //值
huffmanTree[leafNumber].weight = ascii[i]; //权重
++leafNumber;
}
}
int lMinIndex,rMinIndex;
for(int parentIndex = leafNumber;parentIndex < (2 * leafNumber - 1);++parentIndex)
{
setMin(lMinIndex,rMinIndex,parentIndex);
merge(lMinIndex,rMinINdex,parentIndex);
}
}
HuffmanCode::~HuffmanCode()
{
delete []huffmanTree;
}
void HuffmanCode::setMin(int& lMinIndex,int& rMinIndex,int parentIndex)
{
double weight = 0;
for(int i = 0;i < parentIndex;i++)
{
if(huffmanTree[i].parentIndex != -1) //已经被选过的节点
continue;
if(weight == 0)
{
weight = huffmanTree[i].weight;
lMinIndex = i;
}
else if(huffmanTree[i].weight < weight)
{
weight = huffmanTree[i].weight;
lMinIndex = i;
}
}
weight = 0;
for(int i = 0;i < parentIndex;i++) //和上面操作一致
{
if(huffmanTree[i].parentIndex != -1)
continue;
if(weight == 0)
{
weight = huffmanTree[i].weight;
rMinIndex = i;
}
else if(huffmanTree[i].weight < weight)
{
weight = huffmanTree[i].weight;
rMinIndex = i;
}
}
}
void HuffmanCode::merge(int lMinIndex,int rMinIndex,int parentIndex)
{
huffmanTree[lMinIndex].parentIndex = huffmanTree[rMinIndex].parentIndex = parentIndex;
huffmanTree[parentIndex].lIndex = lMinIndex;
huffmanTree[parentIndex].rIndex = rMinIndex;
huffmanTree[parentIndex].weight = huffmanTree[lMinIndex].weight + huffmanTree[rMinIndex].weight;
}
void HuffmanCode::coding()
{
std::string code;
int parentIndex,tempIndex;
//从叶子节点出发
for(int i = 0;i < leafNumber;i++)
{
code = "";
tempIndex = i;
while(huffmanTree[tempIndex].parentIndex != -1)
{
parentIndex = huffmanTree[tempIndex].parentIndex;
if(tempIndex == huffmanTree[parentIndex].lIndex)
code += "0";
else
code += "1";
tempIndex = parentIndex;
}
//把编码倒过来
for(int j = (int)code.size() - 1;j >= 0;j--)
{
huffmanTree[i].code += code[j];
}
std::cout << "字符:" << huffmanTree[i].value << "的编码为:" << huffmanTree[i].code << std::endl;
}
}
void HuffmanCode::decode(const std::string &str)
{
std::string decode,tempCode;
decode = tempCode = "";
int length = (int)str.size();
int i,j;
for(i = 0;i < length;i++)
{
tempCode += str[i];
for(j = 0;j < leafNumber;j++)
{
if(huffmanTree[j].code == tempCode)
{
decode += huffmanTree[j].value;
tempCode = "";
break;
}
}
if(i == length - 1 && j == leafNumber)
{
std:: cout << "decode error" << std:endl;
return;
}
}
std::cout << decode << std::endl;
}
int main()
{
std::string str;
std::cout << "please input string to start coding:" << std::endl;
std::cin >> str;
HuffmanCode huffman(str);
std::cout << "the situation for the code of string are as followed:" << std::endl;
huffman.coding();
std::cout << std::endl;
std::cout << "please input binary code decode to string:" << std::endl;
std::cin >> str;
std::cout << "decode are as followed:" << endl;
huffman.decode(str);
return 0;
}