-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHuffman.cpp
More file actions
161 lines (134 loc) · 3.19 KB
/
Huffman.cpp
File metadata and controls
161 lines (134 loc) · 3.19 KB
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
// Huffman.cpp : Ce fichier contient la fonction 'main'. L'exécution du programme commence et se termine à cet endroit.
//
#include <iostream>
#include <fstream>
using namespace std;
#include <vector>
#include <map>
#include <queue>
#include <string>
#define ll long long int
struct Node {
Node( char _n, ll _w ) { w = _w; n = _n; };
ll w;
char n;
Node *father;
Node* sleft;
Node* sright;
void Set_Code(char c) {
code += c;
};
string code;
};
class CompNode
{
bool reverse;
public:
CompNode(const bool& revparam = false)
{
reverse = revparam;
}
bool operator() (const Node* lhs, const Node* rhs) const
{
if (reverse) return (lhs->w > rhs->w);
else return (lhs->w < rhs->w);
}
};
void dfs(Node *&root, int num) {
if (num == 0) {
root->code += root->father->code + '0';
}
else if (num == 1) {
root->code += root->father->code + '1';
}
if (root->sleft != nullptr) {
dfs(root->sleft, 0);
}
if (root->sright != nullptr) {
dfs(root->sright, 1);
}
}
int main()
{
string s;/* = "AABACDACA f !!!!!!!!!!!ofzen ezofizeoif opiejzf ze^pji ";
s += "osndfjosdipf fopezfpze fç)ezf à)z)çf à)r à)z izejeh rzàerhezrz er";
s += "pjsgfsdjfpsdjfpsdf,r perit reptitàer çt";
*/
ifstream myfile("text.txt");
string line;
if (myfile.is_open())
{
while (getline(myfile, line))
{
s += line;
}
myfile.close();
}
else cout << "Unable to open file";
map<char, ll> f;
for (int i = 0; i < s.length(); i++) {
f[s[i]]++;
}
int countb = 0;
vector<Node*> Byte, DelByte;
priority_queue < Node*, vector <Node*>, CompNode > p(CompNode(true));
for (auto &e : f) {
countb += e.second;
Node* n = new Node(e.first, e.second);
n->sleft = nullptr;
n->sright = nullptr;
p.push(n);
Byte.push_back(n);
DelByte.push_back(n);
//cout << e.first << " " << e.second << endl;
}
countb *= 8;
Node* first=nullptr;
Node* second = nullptr;
Node* root = nullptr;
if (s.length() == 1) {
root = p.top();
Byte.push_back(first);
p.pop();
}
else {
while (p.size()> 1) {
//cout << p.top().n << " " << p.top().w << endl;
first = p.top();
p.pop();
second = p.top();
p.pop();
Node* father = new Node('u', first->w + second->w);
first->father = father;
second->father = father;
//if (first->w > second->w) {
father->sleft = first;
father->sright = second;
/* }
else {
father->sleft = second;
father->sright = first;
}*/
p.push(father);
DelByte.push_back(father);
}
root = new Node('r', first->w + second->w);
//root->code = '1';
first->father = root;
second->father = root;
root->sleft = second;
root->sright = first;
DelByte.push_back(root);
}
dfs(root, -1);
int huff = 0;
for (int i = 0; i < Byte.size(); i++) {
cout << Byte[i]->n << " " << Byte[i]->code << endl;
huff += Byte[i]->code.length();
}
cout << "Text : " << countb << " bits huffman " << huff << " bits "<< endl;
cout << " soit " << (1.0-((double)huff / (double)countb)) * 100.0 << "% compression" << endl;
for (int i = 0; i < DelByte.size(); i++) {
delete DelByte[i];
}
}