huffman encoding

This is our code from a class assignment. I am posting it in case it helps anyone else out. I know it is a little messy, but it works (had to get it done quickly). I ran diff on the original and decoded files and got 0 differences.

I have plans to change the code around. Create a header struct, possibly read/write binary, restructure to use less memory, and just clean it up in general.

As you may notice, generating a trie using a priority queue is only a few lines of code, very simple and clean. The key is to use a custom compare function to reverse the PQ.

The biggest epiphany I had in doing this program was that after creating a string of bitcodes, I already had the 1's and 0's in the order I needed so the encoding (insertBits function) only needs an OR and a left shift.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <sstream>
#include <fstream>
#include <string>
#include <stdlib.h>

class bitChar{
public:
	unsigned char* c;
	int shift_count;
	std::string BITS;

	bitChar();
	void setBITS(std::string _X);
	int insertBits(std::ofstream& outf);
	std::string getBits(unsigned char _X);
	void writeBits(std::ofstream& outf);
	~bitChar();
};

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
#include "bitChar.h"

bitChar::bitChar()
{
	shift_count = 0;
	c = (unsigned char*)calloc(1, sizeof(char));
}

void bitChar::setBITS(std::string _X)
{
	BITS = _X;
}

//Returns number of bits inserted
int bitChar::insertBits(std::ofstream& outf)
{
	int total = 0;

	while(BITS.length())
	{
		if(BITS[0] == '1')
			*c |= 1;
		*c <<= 1;
		++shift_count;
		++total;
		BITS.erase(0, 1);

		if(shift_count == 7)
		{
			writeBits(outf);
			shift_count = 0;
			free(c);
			c = (unsigned char*)calloc(1, sizeof(char));
		}
	}

	//Account for any trailing bits and push them over
	if(shift_count > 0)
	{
		*c <<= (8 - shift_count);
		writeBits(outf);
		free(c);
		c = (unsigned char*)calloc(1, sizeof(char));
	}

	return total;
}

//Outputs a char in binary format
std::string bitChar::getBits(unsigned char _X)
{
	std::stringstream _itoa;

	int _size = sizeof(unsigned char) * 8;

	for(unsigned _s = 0; _s < _size - 1; ++_s)
	{
		_itoa << ((_X >> (_size - 1 - _s)) & 1);
	}

	return _itoa.str();
}

void bitChar::writeBits(std::ofstream& outf)
{
	outf << *c;
}

bitChar::~bitChar()
{
	if(c)
		free(c);
}

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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <queue>
#include <iostream>
#include <vector>
#include <fstream>
#include <iomanip>
#include "bitChar.h"

const std::string magicNum="7771234777";
const int asciiSize = 256;
int lCount[asciiSize];
std::string str_code[asciiSize];

struct node{
	char ch;
	int count;
	node* left;
	node* right;
};

class cmp{
public:
	bool operator()(const node* lhs, const node* rhs) const
	{
		return lhs->count > rhs->count;
	}
};

node* makeNode(char ch, int count)
{
	node* tmp = new node;
	tmp->ch = ch;
	tmp->count = count;
	tmp->left = NULL;
	tmp->right = NULL;
	return tmp;
};

typedef std::priority_queue<node*, std::vector<node*>, cmp> mypq;

void trie(mypq& _X)
{
	while(_X.size() > 1)
	{
		node* holder = new node;
		holder->left = _X.top(); _X.pop();
		holder->right = _X.top(); _X.pop();
		holder->count = holder->left->count + holder->right->count;
		holder->ch = -1;
		_X.push(holder);
	}
}

//Create bit codes by recursively traversing the trie, adding a 0 for left and 1 for right, the key is to remove the end char when the recursion breaks and you have to go up a level
void code(node* _X)
{
	static std::string bits = "";
	if (_X->right != NULL)
	{
		bits += "1";
		code(_X->right);
		bits = bits.substr(0, bits.size() - 1);
	}
	if (_X->left != NULL)
	{
		bits += "0";
		code(_X->left);
		bits = bits.substr(0, bits.size() - 1);
	}
	if(!_X->left && !_X->right)
	{
		str_code[_X->ch] = bits;
	}
}

void count(std::string file, int& _X){
	char letter;
	std::ifstream inf(file.c_str());

	inf >> std::noskipws;

	//Clears array
	for(int i = 0;i < asciiSize; ++i)
		lCount[i] = 0;

	//Goes through text and counts
	while(inf >> letter){
		if(letter >= 0 && letter < asciiSize)
		{
			++lCount[letter];
			++_X;
		}
	}
	inf.close();
}

//Generates a string of the bit codes in the order they appear in the file
//Used during encoding
std::string BITSstring(std::string inFile)
{
	char input;
	std::string BITS = "";

	//Open input stream and create BITS string of entire file
	std::ifstream inf(inFile.c_str());
	inf >> std::noskipws;

	while(inf >> input)
	{
		BITS += str_code[input];
	}

	inf.close();

	//Append ascii 3 EOT character to signify end of text
	BITS += str_code[3];

	return BITS;
}

int main(int argc, char** argv)
{
	int rc;
	char choice;
	unsigned char inChar;
	std::string inFile = "", outFile = "", BITS = "", BITSsub = "", mn = "";
	std::ofstream outf;
	std::ifstream inf;
	mypq pq;
	bitChar bchar;
	int origSize = 0;

	std::cout << "Menu..." << std::endl << "e) Encode file" << std::endl << "d) Decode file" << std::endl;
	std::cin >> choice;

	switch(choice)
	{
	case 'e':
		//Get input filename and set output filename
		std::cout<<"Enter File Name to Encode: "<<std::endl;
		std::cin>>inFile;

		outFile = inFile + ".mpc";

		std::cout << std::left << std::setw(17);
		std::cout << "Input filename: " << inFile << std::endl;
		std::cout << std::left << std::setw(17);
		std::cout << "Output filename:" << outFile << std::endl;
		std::cout << std::endl;

		//Open output streams
		outf.open(outFile.c_str());

		//count and populate array of letter occurrences (lCount) and add one EOT char
		count(inFile, origSize);
		if(lCount[3] == 0)
			lCount[3] = 1;

		//Output compressed file header
		outf<<magicNum<<std::endl;
		outf<<inFile<<std::endl;
		for(int i = 0; i < asciiSize; ++i)
		{
			outf << lCount[i] << " ";
		}
		outf << std::endl;

		//Create nodes based on the available ascii characters and push them into the priority queue
		for(int i = 0; i < asciiSize; ++i)
		{
			if(lCount[i] > 0)
			{
				node* tmp = makeNode(i, lCount[i]);
				pq.push(tmp);
			}
		}

		//Create trie and bit codes
		trie(pq);
		code(pq.top());

		//Create string of bitcodes for actual huffman encoding and do it
		BITS = BITSstring(inFile);
		outf << '#';
		bchar.setBITS(BITS);
		outf << std::noskipws;
		rc = bchar.insertBits(outf);

		if(rc == BITS.length())
		{
			std::cout << "Encoding succsessful! :)" << std::endl;
			std::cout << "The compression ration is: " << (float)rc / ((float)origSize * 8.0) * 100.0 << "%" << std::endl;
		}
		else
		{
			std::cout << "There was an error writing the bits! :(" << std::endl;
			std::cout << "Expected: " << BITS.length() * 8 << " but got: " << rc << std::endl;
		}

		break;
	case 'd':
		//Get input filename and set output filename
		std::cout<<"Enter File Name to Decode: "<<std::endl;
		std::cin>>inFile;

		inf.open(inFile.c_str());
		inf >> mn;
		if(mn != magicNum)
		{
			std::cout << "Magic number does not match, this is not a valid file..." << std::endl;
			return 1;
		}

		inf >> outFile;
		if(outFile != inFile.substr(0, inFile.length() - 4))
		{
			std::cout << outFile << " " << inFile.substr(0, inFile.length() - 4) << std::endl;
			std::cout << "File names do not match but will attempt to decode anyway..." << std::endl;
		}
		outf.open(outFile.c_str());

		//Read in the letter count and add valid one to the priority queue
		for(int i = 0; i < asciiSize; ++i)
		{
			inf >> lCount[i];
			if(lCount[i] > 0)
			{
				node* tmp = makeNode(i, lCount[i]);
				pq.push(tmp);
			}
		}

		//Create trie and bit codes
		trie(pq);
		code(pq.top());

		while(inChar != '#')
		{
			inf >> inChar;
		}

		inf >> std::noskipws;
		//Read in encoded chars and create BITS
		while(inf >> inChar)
		{
			BITS += bchar.getBits(inChar);
		}

		inf.close();

		for(int i = 0; i < BITS.length(); ++i)
		{
			BITSsub += BITS[i];
			for(int j = 0; j < asciiSize; ++j)
			{
				if(BITSsub == str_code[j])
				{
					//End of text has been hit and file is over, write newline and exit
					if(j == 3)
					{
						outf << "\n";
						i = BITS.length();
						break;
					}
					outf << (char)j;
					BITSsub = "";
					break;
				}
			}
		}

		break;
	default:
		std::cout << "Invalid choice...." << std::endl;
		break;
	}

	outf.close();

	return 0;
}
Last edited on
Topic archived. No new replies allowed.