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
|
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include "Node.h"
using namespace std;
void fillList(char* sent, int size, vector<Node> &vec);
bool checkList(char ch, vector<Node> vec);
void getFreq(vector<Node> &vec, char* sent, int size);
void sort(vector<Node> &vec);
Node makeTree(vector<Node> &vec);
void traverse(Node *nde);
int main()
{
char ch = 0;
ifstream inFile;
ofstream outFile;
int count = 0;
vector<Node> list;
Node tree = 0;
inFile.open("message.txt");
if(!inFile)
{
cerr << "Unable to open file! Program terminating..." << endl;
return(EXIT_FAILURE);
}
while(inFile.good())
{
ch = inFile.get();
count++;
}
inFile.close();
char* msg = new char[count];
inFile.open("message.txt");
if(!inFile)
{
cerr << "Unable to open file! Program terminating..." << endl;
return(EXIT_FAILURE);
}
while(inFile.good())
{
inFile.getline(msg, count);
}
cout << msg << endl;
fillList(msg, count, list);
getFreq(list, msg, count);
sort(list);
for(size_t i = 0; i < list.size(); i++)
{
cout << list[i].letter << " - Frequency: " << list[i].frequency << endl;
}
tree = makeTree(list);
traverse(&tree);
delete[] msg;
}
void fillList(char* sent, int size, vector<Node> &vec)
{
char ch = 0;
for(int i = 0; i < size; i++)
{
ch = sent[i];
if(vec.size() == 0)
{
Node *temp = new Node(ch);
vec.push_back(*temp);
}
else if(!checkList(ch, vec))
{
Node *temp = new Node(ch);
vec.push_back(*temp);
}
}
}
bool checkList(char ch, vector<Node> vec)
{
for(size_t i = 0; i < vec.size(); i++)
{
if(ch == vec[i].letter)
{
return 1;
}
}
return 0;
}
void getFreq(vector<Node> &vec, char* sent, int size)
{
for(size_t i = 0; i < vec.size(); i++)
{
int count = 0;
for(int j = 0; j < size; j++)
{
if(vec[i].letter == sent[j])
{
count++;
}
}
vec[i].frequency = count;
}
}
void sort(vector<Node> &vec)
{
for(size_t i = 0; i < vec.size() - 1; i++)
{
for(size_t j = i + 1; j < vec.size(); j++)
{
if(vec[i].frequency > vec[j].frequency)
{
Node temp = vec[i];
vec[i] = vec[j];
vec[j] = temp;
}
}
}
}
Node makeTree(vector<Node> &vec)
{
while(vec.size() > 1)
{
Node min1 = vec[0];
Node min2 = vec[1];
vec.erase(vec.begin() + 0);
vec.erase(vec.begin() + 0);
Node* temp = new Node(min1.frequency + min2.frequency);
temp->left = &min2;
temp->right = &min1;
vec.push_back(*temp);
delete temp;
sort(vec);
}
return vec[0];
}
void traverse(Node *nde)
{
Node* tp = nde;
if(tp == NULL)
return;
else
{
traverse(tp->left);
cout << tp->frequency << endl;
traverse(tp->right);
}
}
|