-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathttTree.cpp
333 lines (302 loc) · 10.3 KB
/
ttTree.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
328
329
330
331
332
333
// Assignment: Project 3
// Created By: Jevan Smith, Isaac Davidson
// Course: CS 415
// Date: April 16, 2019
// Descriptions: A simple Binary Search Tree, and 2-3 Tree
// implementation supporting features like
// search, insert and inorder traversal.
#include "ttTree.h"
#include "time.h"
#include <iomanip>
#include <sstream>
ttTree::ttTree(){
root = NULL;
}
void ttTree::printTree(ostream & out) {
out << "2-3 Tree Index:\n-------------------------\n";
printTreeHelper(root, out);
}
void ttTree::contains() {
vector<int> lines;
string input;
cout << "Search word: ";
cin >> input;
bool found = searchTreeHelper(root, input, lines);
if(found) {
cout << "Line Numbers:";
cout << " " << lines[0];
for (int i = 1; i < lines.size(); i++)
cout << ", " << lines[i];
}
else {
cout << "\"" << input << "\" " << "is not in the document";
}
cout << endl;
}
void ttTree::buildTree(ifstream & input) {
cout << "-----------------------------------------------------" << endl;
cout << "[MSG] BUILDING 2-3 TREE..." << endl;
int line = 1, numWords = 0, distWords = 0, treeHeight = 0;
string tempLine, tempWord;
double totalTime, finishTime, startTime = clock();
while (!input.eof()) {
//Read a whole line of text from the file
getline(input, tempLine);
for (int i = 0; i < tempLine.length(); i++) {
//Insert valid chars into tempWord until a delimiter( newline or space) is found
while (tempLine[i] != ' ' && tempLine[i] != '\n' && i < tempLine.length() ) {
tempWord.insert(tempWord.end(), tempLine[i]);
i++;
}
//Trim any punctuation off end of word. Will leave things like apostrophes
//and decimal points
while(tempWord.length() > 0 && !isalnum(tempWord[tempWord.length() - 1]))
tempWord.resize(tempWord.size() - 1);
if (tempWord.length() > 0)
{
words.push_back(tempWord);
//Once word is formatted, call insert with the word, the line of the input
//file it came from, the root of our tree, and the distinct word counter
root = insert(tempWord, line, root, distWords);
//Increment our total number of words inserted
numWords++;
//Clear out tempWord so we can use it again
tempWord.clear();
}
}
line++;
}
//Do time and height calculation
finishTime = clock();
totalTime = (double) (finishTime - startTime)/CLOCKS_PER_SEC;
treeHeight = findHeight(root);
//Print output
cout << "=====================================================" << endl;
cout << "- Built Tree results (2-3 Tree)" << endl;
cout << "=====================================================" << endl;
cout << setw(40) << std::left;
cout << "Total number of words: " << numWords << endl;
cout << setw(40) << std::left
<< "Total number of distinct words: " << distWords << endl;
cout << setw(40) << std::left
<<"Total time spent building index: " << totalTime << endl;
cout << setw(40) << std::left
<<"Height of 2-3 Tree is: " << treeHeight << endl;
}
void ttTree::timeTaken() {
cout << "-----------------------------------------------------" << endl;
cout << "[MSG] SEARCHING ALL WORDS IN 2-3 TREE..." << endl;
double totalTime, finishTime, startTime = clock();
vector<int> lines;
for(int i = 0; i < words.size(); i++) {
searchTreeHelper(root, words[i], lines);
}
finishTime = clock();
totalTime = (double) (finishTime - startTime)/CLOCKS_PER_SEC;
cout << "=====================================================" << endl;
cout << "- Time Taken Searching all words in index (2-3 Tree)" << endl;
cout << "=====================================================" << endl;
cout << setw(40) << std::left;
cout << "Total time taken by 2-3 Tree: " << totalTime << endl;
}
ttTree::node * ttTree::createNode(const string x, node * left, node * middle, node * right) {
node * t = new node();
t->leftKey = x;
t->left = left;
t->middle = middle;
t->right = right;
return t;
}
bool ttTree::isLeafNode(node * x) {
if(x->left == NULL && x->middle == NULL && x->right == NULL) {
return true;
}
else {
return false;
}
}
ttTree::node * ttTree::add(node * x, node * n) {
if(x->rightKey.empty()) {
if(x->leftKey < n->leftKey) {
x->rightKey = n->leftKey; x->rightLines = n->leftLines;
x->middle = n->left;
x->right = n->middle;
}
else { //SWAP
x->rightKey = x->leftKey; x->rightLines = x->leftLines;
x->leftKey = n->leftKey; x->leftLines = n->leftLines;
x->right = x->middle;
x->middle = n->middle;
x->left = n->left;
}
return x;
}
// ADD TO LEFT
else if(x->leftKey >= n->leftKey) {
node * newNode = createNode(x->leftKey, n, x, NULL);
newNode->leftLines = x->leftLines;
x->left = x->middle; x->middle = x->right; x->right = NULL;
x->leftKey = x->rightKey; x->rightKey.clear();
x->leftLines = x->rightLines; x->rightLines.clear();
return newNode;
}
// ADD TO CENTER
else if(x->rightKey >= n->leftKey) {
node * newNode = createNode(x->rightKey, n->middle, x->right, NULL);
newNode->leftLines = x->rightLines;
x->middle = n->left;
n->middle = newNode; n->left = x;
x->rightKey.clear(); x->right = NULL; x->rightLines.clear();
return n;
}
// ADD TO RIGHT
else {
node * newNode = createNode(x->rightKey, x, n, NULL);
newNode->leftLines = x->rightLines;
x->right = NULL; x->rightKey.clear(); x->rightLines.clear();
return newNode;
}
}
ttTree::node * ttTree::insert(const string &key, int line, node * root, int &distWord){
if(root == NULL) {
node * newNode = createNode(key, NULL, NULL, NULL);
newNode->leftLines.push_back(line);
distWord++;
return newNode;
}
if(key == root->leftKey || key == root->rightKey) { // Remove's duplicates
if(key == root->leftKey)
root->leftLines.push_back(line);
else
root->rightLines.push_back(line);
return root;
}
if(isLeafNode(root)) {
node * newNode = createNode(key, NULL, NULL, NULL);
newNode->leftLines.push_back(line);
node * finalNode = add(root, newNode);
distWord++;
return finalNode;
}
// LEFT
if(key < root->leftKey) {
node * newNode = insert(key, line, root->left, distWord);
if(newNode == root->left)
return root;
else
return add(root, newNode);
}
// MIDDLE
else if (root->rightKey.empty() || key < root->rightKey) {
node * newNode = insert(key, line, root->middle, distWord);
if(newNode == root->middle)
return root;
else
return add(root, newNode);
}
// RIGHT
else {
node * newNode = insert(key, line, root->right, distWord);
if(newNode == root->right)
return root;
else
return add(root, newNode);
}
}
bool ttTree::searchTreeHelper(node * x, string value, vector<int> &lines) {
if(root == NULL ) {
return false;
}
if(value == x->leftKey) {
lines = x->leftLines;
return true;
}
if(value == x->rightKey) {
lines = x->rightLines;
return true;
}
if(isLeafNode(x))
return false;
else if(!x->rightKey.empty()) {
if (value < x->leftKey) {
return searchTreeHelper(x->left, value, lines);
} else if (value > x->leftKey && value < x->rightKey) {
return searchTreeHelper(x->middle, value, lines);
} else {
return searchTreeHelper(x->right, value, lines);
}
}
else {
if (value < x->leftKey)
return searchTreeHelper(x->left, value, lines);
else
return searchTreeHelper(x->middle, value, lines);
}
}
void ttTree::printLines(node * x, bool isLeft, ostream & out) {
if(isLeft) {
if(!x->leftLines.empty()) {
out << " " << x->leftLines[0];
for (int i = 1; i < x->leftLines.size(); i++)
out << ", " << x->leftLines[i];
}
}
else {
if(!x->rightLines.empty()) {
out << " " << x->rightLines[0];
for (int i = 1; i < x->rightLines.size(); i++)
out << ", " << x->rightLines[i];
}
}
}
void ttTree::print(node * x, ostream & out) {
if(x != NULL) {
if(x->rightKey.empty()) {
out << setw(30) << std::left;
out << x->leftKey; printLines(x, true, out);
out << endl;
} else {
out << setw(30) << std::left;
out << x->leftKey; printLines(x, true, out);
out << endl;
out << setw(30) << std::left;
out << x->rightKey; printLines(x, false, out);
out << endl;
}
}
}
void ttTree::printTreeHelper(node * x, ostream & out) { //Inorder
if (x != NULL) {
if (isLeafNode(x)) {
print(x, out);
} else {
printTreeHelper(x->left, out);
out << setw(30) << std::left;
out << x->leftKey; printLines(x, true, out);
out << endl;
if (x->middle) {
printTreeHelper(x->middle, out);
if (!x->rightKey.empty()) {
out << setw(30) << std::left;
out << x->rightKey; printLines(x, false, out);
out << endl;
}
}
printTreeHelper(x->right, out);
}
}
}
int ttTree::findHeight(node * x){
if(x == NULL)
return 0;
else {
int leftHeight = findHeight(x->left), rightHeight = findHeight(x->right);
int middleHeight = findHeight(x->middle);
if(leftHeight > rightHeight && leftHeight > middleHeight)
return(leftHeight+1);
else if(rightHeight > leftHeight && rightHeight > middleHeight)
return (rightHeight+1);
else
return(middleHeight+1);
}
}