-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHuffman_basic.cpp
160 lines (150 loc) · 3.03 KB
/
Huffman_basic.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
/*
Author : Chris Andrew
Email : [email protected]
This code was written for educational purposes and is free to use and modify by anyone.
Usage:
The compiled file must be called on the command line using ./<compiled_file> <input_file> <output_file>
Reads the text from <input_file> and writes the Huffman tree to <output_file>
*/
#include <iostream>
#include <queue>
#include <string>
#include <string.h>
#include <vector>
#include <map>
#include <fstream>
#include <stdio.h>
using namespace std;
//An object that represents a charcter or a symbol in the <input_file>
struct symb
{
string a;
int n;
symb* l;
symb* r;
};
//Structure to compare the values of two 'symb' objects; Will be given to sorting function for sorting.
struct comp
{
bool operator()(symb * a, symb * b) const
{
int x=a->n;
int y=b->n;
return x > y;
}
};
//Huffman class constructs the Huffman tree, stores it in a dictionary and prints it to an output file
class Huffman
{
private:
map<char,int> chctrs; //frequency of all characters in infile
map<char,string> bin; //binary string for representing each character
string infile; //name of input file
string outfile; //name of output file
int k[1000000]; //k has contents of total file, limit set to 1000000 characters
int num; //stores the number of characters
symb *root; //root node of Huffman tree
public:
//Class constructor
Huffman(string a, string b)
{
infile=a;
outfile=b;
}
//Reads contents of input file and calculates frequency of each character
void filein()
{
char temp[100000];
int x;
for(x=0;x<infile.size();x++)
temp[x]=infile[x];
temp[x]='\0';
num=0;
ifstream f;
f.open(temp);
int m;
while(f>>m)
{
k[num]=m;
num++;
}
for(int i=0;i<num;i++)
{
char c=i;
chctrs[c]=k[i];
}
f.close();
return;
}
//Constructs Huffman tree and finds binary string of each character
void constructtree()
{
priority_queue<symb*,vector<symb*>,comp> q;
symb *t1,*t2,*t3;
map<char,int>::iterator it;
for(it=chctrs.begin(); it!=chctrs.end(); ++it)
{
t1=new symb();
string s;
s.push_back(it->first);
t1->a=s;
t1->n=it->second;
t1->l=NULL;
t1->r=NULL;
q.push(t1);
}
while(q.size()>1)
{
t1=(q.top());
q.pop();
t2=(q.top());
q.pop();
t3=new symb();
t3->n=t1->n+t2->n;
t3->a=t1->a+t2->a;
t3->l=t1;
t3->r=t2;
q.push(t3);
}
char temp[100000];
int x;
for(x=0;x<outfile.size();x++)
{
temp[x]=outfile[x];
}
temp[x]='\0';
ofstream f;
f.open(temp);
for(it=chctrs.begin(); it!=chctrs.end(); ++it)
{
t1=q.top();
bin[it->first]="";
while(t1->l!=NULL&&t1->r!=NULL)
{
if((t1->l)->a.find(it->first)<(t1->l)->a.size())
{
bin[it->first]=bin[it->first]+"0";
t1=t1->l;
}
else
{
bin[it->first]=bin[it->first]+"1";
t1=t1->r;
}
}
f<<bin[it->first]<<" ";
cout<<bin[it->first]<<endl;
}
f.close();
return;
}
};
int main(int argc,char *argv[])
{
string a(argv[1]);
string b(argv[2]);
Huffman h(a,b);
h.filein();
h.constructtree();
return 0;
}