-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
138 lines (113 loc) · 3.09 KB
/
Copy pathmain.cpp
File metadata and controls
138 lines (113 loc) · 3.09 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
#include <vector>
#include <iostream>
#include <queue>
#include <string>
/* TODO:
add support for space character
add support for numerical values
*/
using namespace std;
// tree node
struct node {
char inputChar;
unsigned freqChar;
// left = l, right = r
node* l, * r;
node(char inputChar, unsigned freqChar)
{
l = r = NULL;
this->inputChar = inputChar;
this->freqChar = freqChar;
}
};
// For comparison of two heap nodes
struct cmpr {
bool operator()(node* l, node* r)
{
// comparison between two values
// checking if left node is greater than the node on the right
return (l->freqChar > r->freqChar);
}
};
// creating two global vectors that store both the characters
// and the frequency
vector<int> values;
vector<char> stringval;
void charValFreq(string str, bool extraCredit = false)
{
if (extraCredit) {
str = str + "ntht"; // extra credit
}
int n = str.size();
int freq[100];
memset(freq, 0, sizeof(freq));
for (int i = 0; i < n; i++) {
if (isalpha(str[i])) {
freq[str[i] - 'A']++;
}
}
for (int i = 0; i < n; i++) {
if (freq[str[i] - 'A'] != 0) {
stringval.push_back(str[i]);
values.push_back(freq[str[i] - 'A']);
freq[str[i] - 'A'] = 0;
}
}
}
void code(struct node* root, string datastr)
{
if (!root) {
return;
}
if (root->inputChar != '$')
cout << root->inputChar << ": " << datastr << "\n";
// left and right nodes assignments 1, 0 we can change these values if we have left to represent 0 instead
code(root->l, datastr + "0");
code(root->r, datastr + "1");
}
void encode(char data[], int freq[], int sizedata)
{
struct node* l, * r, * top;
priority_queue<node*, vector<node*>, cmpr> nodeheap;
for (int j = 0; j < sizedata; ++j)
nodeheap.push(new node(data[j], freq[j]));
while (nodeheap.size() != 1) {
//remove left and right element from the top
l = nodeheap.top();
nodeheap.pop();
r = nodeheap.top();
nodeheap.pop();
top = new node('$', l->freqChar + r->freqChar);
top->l = l;
top->r = r;
nodeheap.push(top);
}
code(nodeheap.top(), "");
}
int main()
{
// intitalizing string, prompting user for input
string tobeEncoded;
cout << "Huffman Encoder:" << endl;
getline(cin, tobeEncoded);
// taking the input and calculating characters and frequency
charValFreq(tobeEncoded);
// converting our vectors into a usable array
char* characters = stringval.data();
int* freq_values = values.data();
// table with both char and freq
cout << "char\t | \tfreq" << endl;
for (int i = 0; i < stringval.size(); i++) {
cout << characters[i] << "\t|\t" << freq_values[i] << endl;
}
cout << endl << endl;
/*
function returns huffman encoding
characters: char[]
freq_values: int[]
stringval: int
*/
encode(characters, freq_values, stringval.size());
system("pause");
return 0;
}