-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreqAVL.cpp
327 lines (266 loc) · 7.2 KB
/
freqAVL.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//
// Zübeyr Furkan Eryýlmaz
// 09.12.2015
// 21202676 CS202-01
// Assignment 4
//
#include "freqAVL.h"
#include "math.h"
#include <string.h>
#include <climits>
TreeNode::TreeNode()
{
this->count = 0;
this->item = "";
this->leftChildPtr = NULL;
this->rightChildPtr = NULL;
}
TreeNode::TreeNode(string newWord)
{
this->count = 1;
this->item = newWord;
this->leftChildPtr = NULL;
this->rightChildPtr = NULL;
}
freqAVL::freqAVL()
{
root = NULL;
}
freqAVL::~freqAVL()
{
}
int freqAVL::getBalance( TreeNode *node)
{//Return height difference between right and left sub tree
return getHeight(node->rightChildPtr) - getHeight(node->leftChildPtr);
}
int freqAVL::getHeight( TreeNode *node)
{//Return height
if ( node != NULL)
{
return 1 + max(getHeight(node->leftChildPtr),getHeight(node->rightChildPtr));
}
return 0;
}
void freqAVL::rotateRight( TreeNode* &node)
{//Rotate AVL tree to right
TreeNode *temp = node->leftChildPtr;
node->leftChildPtr = temp->rightChildPtr;
temp->rightChildPtr = node;
node = temp;
temp = NULL;
}
void freqAVL::rotateLeft( TreeNode* &node)
{//Rotate AVL tree to left
TreeNode *temp = node->rightChildPtr;
node->rightChildPtr = temp->leftChildPtr;
temp->leftChildPtr = node;
node = temp;
temp = NULL;
}
void freqAVL::balance(TreeNode* &node)
{//Balance the AVL tree
//Check balance factors of all nodes from leaf to root
//and balance the tree accordingly
if (getBalance(node) > 1)
{
if (getBalance(node->rightChildPtr) < 0)
{
rotateRight(node->rightChildPtr);
rotateLeft(node);
}
else
{
rotateLeft(node);
}
}
else if (getBalance(node) < -1) {
if (getBalance(node->leftChildPtr) > 0)
{
rotateLeft(node->leftChildPtr);
rotateRight(node);
}
else
{
rotateRight(node);
}
}
}
void freqAVL::findWordCount( TreeNode *node, long long &count)
{//Count word with an inorder traversal
if(node != NULL)
{
findWordCount(node->leftChildPtr, count);
count++;
findWordCount(node->rightChildPtr, count);
}
}
void freqAVL::findMostFrequent( TreeNode* node, string &item, int &count)
{//While inorder traversaling it compares and finds the most frequent word
if(node != NULL)
{
findMostFrequent(node->leftChildPtr, item, count);
if (node->count > count)
{
count = node->count;
item = node -> item;
}
findMostFrequent(node->rightChildPtr, item, count);
}
}
void freqAVL::findLeastFrequent( TreeNode* node, string &item, int &count)
{//While inorder traversaling it compares and finds the least frequent word
if(node != NULL)
{
findLeastFrequent(node->leftChildPtr, item, count);
if (node->count < count)
{
count = node->count;
item = node -> item;
}
findLeastFrequent(node->rightChildPtr, item, count);
}
}
void freqAVL::printInorder( TreeNode* node, ofstream &file)
{//While inorder traversaling it prints the words and their frequencies
if( node != NULL)
{
printInorder(node->leftChildPtr, file);
file << (node->item) << " " << (node->count) << "\n";
printInorder(node->rightChildPtr, file);
}
}
void freqAVL::totalFrequence(TreeNode* node, double &total)
{//While inorder traversaling sum all the frequencies of the words
if( node != NULL)
{
totalFrequence(node->leftChildPtr, total);
total = total + node->count;
totalFrequence(node->rightChildPtr, total);
}
}
void freqAVL::calculateSquareofDifferences(TreeNode* node, double average, double &squareDiff)
{//While inorder traversaling it calculates sum of square of difference between average and each words frequency.
if( node != NULL)
{
calculateSquareofDifferences(node->leftChildPtr, average, squareDiff);
squareDiff = squareDiff + (average - node->count) * (average - node->count);
calculateSquareofDifferences(node->rightChildPtr, average, squareDiff);
}
}
void freqAVL::addWord(string newWord, TreeNode* &node)
{//Add given word to the AVL
if( node == NULL)
{
node = new TreeNode(newWord);
}
else
{
if ( newWord.compare(node->item) > 0)
{
addWord(newWord,node->rightChildPtr);
balance(node);
}
else if( newWord.compare(node->item) == 0)
{
node->count++;
}
else
{
addWord(newWord,node->leftChildPtr);
balance(node);
}
}
}
void freqAVL::generateTree(string fileName)
{//Read from file and add to tree one by one
ifstream file;
file.open(fileName.c_str());
string words;
bool strStarted;
while (file >> words)
{
strStarted = false;
int start = -1;
int end = -1;
//Change non-alphabetic characters with spaces and if it is a alphabetic character make it lowercase
for (int i = 0; i < words.length(); i++)
{
if (isalpha(words[i]))
{
words[i] = tolower(words[i]);
}
else
{
words[i] = ' ';
}
}
//Split the word according to spaces and add them to AVL
char* newWord;
char* wordsChar = new char[words.length() + 1];
strcpy(wordsChar,words.c_str());
newWord = strtok(wordsChar," ");
while (newWord != NULL)
{
addWord(newWord,root);
newWord = strtok (NULL, " ");
}
}
}
// These functions write the calculated data to the text files
void freqAVL::printHeight()
{
ofstream file;
file.open ("statistics.txt",file.app);
file << "Tree Height: " << getHeight(root) << "\n";
file.close();
}
void freqAVL::printTotalWordCount()
{
long long count = 0;
findWordCount( root, count);
ofstream file;
file.open ("statistics.txt");
file << "Total Word Count: " << count << "\n";
file.close();
}
void freqAVL::printWordFrequencies()
{
ofstream file;
file.open ("wordfreqs.txt");
printInorder(root,file);
file.close();
}
void freqAVL::printMostFrequent()
{
int count = 0;
string item = "";
findMostFrequent( root, item, count);
ofstream file;
file.open ("statistics.txt",file.app);
file << "Most Frequent: " << item << " " << count << "\n";
file.close();
}
void freqAVL::printLeastFrequent()
{
int count = INT_MAX;
string item = "";
findLeastFrequent( root, item, count);
ofstream file;
file.open ("statistics.txt",file.app);
file << "Least Frequent: " << item << " " << count << "\n";
file.close();
}
void freqAVL::printStandartDeviation()
{
long long count = 0;
findWordCount( root, count);
double total = 0;
totalFrequence(root, total);
double average = total/count;
double squareDiff = 0;
calculateSquareofDifferences(root, average, squareDiff);
ofstream file;
file.open ("statistics.txt",file.app);
file << "Standart Deviation: " << (sqrt(squareDiff/count)) << "\n";
file.close();
}