-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMostFrequentWords.cpp
More file actions
193 lines (178 loc) · 4.66 KB
/
MostFrequentWords.cpp
File metadata and controls
193 lines (178 loc) · 4.66 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
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
/*
* C++ Program to Find k Most Frequent Words in a File
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
# define MAX_CHARS 26
# define MAX_WORD_SIZE 30
using namespace std;
/*
* Trie Node Declaration
*/
struct TrieNode
{
bool isEnd;
unsigned frequency;
int indexMinHeap;
TrieNode* child[MAX_CHARS];
};
/*
* Min Heap Node
*/
struct MinHeapNode
{
TrieNode* root;
unsigned frequency;
char* word;
};
/*
* Min Heap
*/
struct MinHeap
{
unsigned capacity;
int count;
MinHeapNode* array;
};
// create a new Trie node
TrieNode* newTrieNode()
{
TrieNode* trieNode = new TrieNode;
trieNode->isEnd = 0;
trieNode->frequency = 0;
trieNode->indexMinHeap = -1;
for (int i = 0; i < MAX_CHARS; ++i)
trieNode->child[i] = NULL;
return trieNode;
}
// create a Min Heap of given capacity
MinHeap* createMinHeap(int capacity)
{
MinHeap* minHeap = new MinHeap;
minHeap->capacity = capacity;
minHeap->count = 0;
minHeap->array = new MinHeapNode [minHeap->capacity];
return minHeap;
}
// swap two min heap nodes.
void swapMinHeapNodes (MinHeapNode* a, MinHeapNode* b)
{
MinHeapNode temp = *a;
*a = *b;
*b = temp;
}
// minHeapify function
void minHeapify(MinHeap* minHeap, int idx)
{
int left, right, smallest;
left = 2 * idx + 1;
right = 2 * idx + 2;
smallest = idx;
if (left < minHeap->count && minHeap->array[left].frequency < minHeap->array[smallest].frequency)
smallest = left;
if (right < minHeap->count && minHeap->array[right].frequency < minHeap->array[smallest].frequency)
smallest = right;
if (smallest != idx)
{
minHeap->array[smallest].root->indexMinHeap = idx;
minHeap->array[idx].root->indexMinHeap = smallest;
swapMinHeapNodes (&minHeap->array[smallest], &minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}
// build a heap
void buildMinHeap(MinHeap* minHeap)
{
int n, i;
n = minHeap->count - 1;
for (i = ( n - 1 ) / 2; i >= 0; --i)
minHeapify(minHeap, i);
}
// Inserts a word to heap
void insertInMinHeap(MinHeap* minHeap, TrieNode** root, const char* word)
{
if ((*root)->indexMinHeap != -1)
{
++( minHeap->array[(*root)->indexMinHeap].frequency);
minHeapify(minHeap, (*root)->indexMinHeap);
}
else if(minHeap->count < minHeap->capacity)
{
int count = minHeap->count;
minHeap->array[count].frequency = (*root)->frequency;
minHeap->array[count].word = new char [strlen( word ) + 1];
strcpy(minHeap->array[count].word, word);
minHeap->array[count].root = *root;
(*root)->indexMinHeap = minHeap->count;
++( minHeap->count );
buildMinHeap( minHeap );
}
else if ((*root)->frequency > minHeap->array[0].frequency)
{
minHeap->array[0].root->indexMinHeap = -1;
minHeap->array[0].root = *root;
minHeap->array[0].root->indexMinHeap = 0;
minHeap->array[0].frequency = (*root)->frequency;
delete [] minHeap->array[0].word;
minHeap->array[0]. word = new char [strlen( word ) + 1];
strcpy( minHeap->array[0].word, word );
minHeapify (minHeap, 0);
}
}
// Inserts a new word to both Trie and Heap
void insertUtil (TrieNode** root, MinHeap* minHeap, const char* word, const char* dupWord)
{
if ( *root == NULL )
*root = newTrieNode();
if ( *word != '\0' )
insertUtil (&((*root)->child[tolower( *word ) - 97]), minHeap, word + 1, dupWord);
else // The complete word is processed
{
if ( (*root)->isEnd )
++((*root)->frequency);
else
{
(*root)->isEnd = 1;
(*root)->frequency = 1;
}
insertInMinHeap( minHeap, root, dupWord );
}
}
// add a word to Trie & min heap
void insertTrieAndHeap(const char *word, TrieNode** root, MinHeap* minHeap)
{
insertUtil(root, minHeap, word, word);
}
// Display Min Heap
void displayMinHeap(MinHeap* minHeap)
{
int i;
for (i = 0; i < minHeap->count; ++i)
{
cout<<minHeap->array[i].word<<" : "<<minHeap->array[i].frequency<<endl;
}
}
// takes a file as input, add words to heap and Trie
void printKMostFreq(FILE* fp, int k)
{
MinHeap* minHeap = createMinHeap(k);
TrieNode* root = NULL;
char buffer[MAX_WORD_SIZE];
while (fscanf(fp, "%s", buffer) != EOF)
insertTrieAndHeap(buffer, &root, minHeap);
displayMinHeap(minHeap);
}
// Main
int main()
{
int k = 5;
FILE *fp = fopen("file.txt", "r");
if (fp == NULL)
printf ("File doesn't exist ");
else
printKMostFreq (fp, k);
return 0;
}