-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBSTFunctions.cpp
More file actions
202 lines (135 loc) · 4.45 KB
/
BSTFunctions.cpp
File metadata and controls
202 lines (135 loc) · 4.45 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
194
195
196
197
198
199
200
201
202
#include "BSTIndexing.h"
#include <string>
#include <cstdlib>
#include <iostream>
#include <sstream>
using namespace std;
// SELECTION SORT
void alphabeticalSort(string array[], int arraySize, int lineNumberArray[] ){
string smallestElement;
int indexSmallestElement;
int index;
string temp;
int temp1;
int index1;
for (int i = 0; i < arraySize - 1; i++){
smallestElement = array[i];
indexSmallestElement = lineNumberArray[i];
for (int j = i; j < arraySize; j++){
if (array[j] <= smallestElement){
smallestElement = array[j];
indexSmallestElement = lineNumberArray[j];
index1 = j;
index = j;
}
}
temp = array[i];
array[i] = array[index];
array[index] = temp;
temp1 = lineNumberArray[i];
lineNumberArray[i] = lineNumberArray[index1];
lineNumberArray[index1] = temp1;
}
}
void BSTIndexing::addWordPrivate(string key, node* Ptr, int lineNumber){
Ptr->words[Ptr->wordIndex] = key;
Ptr->wordIndex++;
Ptr->lineNumber[Ptr->lineNumberIndex] = lineNumber;
Ptr->lineNumberIndex++;
}
void BSTIndexing::addWord(string key, node* Ptr, int lineNumber){
addWordPrivate(key, Ptr, lineNumber);
}
BSTIndexing::BSTIndexing(){
root = NULL;
}
// CREATE LEAF
// * a leaf has no children *
BSTIndexing::node* BSTIndexing::CreateLeaf(string key, int lineNumber){
node* newNode = new node;
newNode->character = key[0];
// save character
// save word to array of words for that letter
addWord(key, newNode, lineNumber);
// sort word list
alphabeticalSort(newNode->words, newNode->wordIndex, newNode->lineNumber);
// NO need to sort the array in create leaf function
// sort it in the create node function
newNode->right = NULL;
newNode->left = NULL;
return newNode;
}
void BSTIndexing::AddLeaf(string key, int lineNumber){
AddLeafPrivate(key, root, lineNumber);
}
void BSTIndexing::AddLeafPrivate(string key, node* Ptr, int lineNumber){
// if there is not root -> TREE DOESNT EXIST
if (root == NULL){
root = CreateLeaf(key, lineNumber);
// if the first letter of the word passed is less than the first letter of the
// pointer passed
} else if (key[0] < Ptr->character) {
if (Ptr->left != NULL){
AddLeafPrivate(key, Ptr->left, lineNumber);
} else {
Ptr->left = CreateLeaf(key, lineNumber);
}
// if the first letter of the word passed is less than the first letter of the
// pointer passed
} else if (key[0] > Ptr->character){
if (Ptr->right != NULL) {
AddLeafPrivate(key, Ptr->right, lineNumber);
} else {
Ptr->right = CreateLeaf(key, lineNumber);
}
// if key already exist add word to that letter
} else {
if ( Ptr->character == key[0] ){
addWord(key, Ptr, lineNumber);
alphabeticalSort(Ptr->words, Ptr->wordIndex, Ptr->lineNumber);
}
}
}
int BSTIndexing::returnWordArrayLength(){
return root->wordIndex;
}
string BSTIndexing::returnWord(int index){
return root->words[index];
}
void BSTIndexing::printInorderPrivate(node* Ptr){
if (root != NULL){
if (Ptr->left != NULL){
printInorderPrivate(Ptr->left);
}
cout << Ptr->character << endl;
cout << "~" << endl;
for(int i = 0; i < Ptr->wordIndex; i++){
cout << Ptr->words[i] << "..................(" << Ptr->lineNumber[i] << ")" << endl;
}
cout << endl;
if (Ptr->right != NULL){
printInorderPrivate(Ptr->right);
}
} else {
//tree empty
}
}
void BSTIndexing::printInOrder(){
cout << endl << endl << endl << "INDEX OF WORDS IN PAGE.TXT FILE" << endl << endl;
printInorderPrivate(root);
}
void BSTIndexing::addAllWordsFromTextFile(string filename){
fstream reader;
string word;
reader.open(filename);
string sentence;
int lineNumber = 0;
while(!reader.eof()){
getline(reader, sentence);
stringstream iss(sentence);
lineNumber++;
while (iss >> word){
AddLeaf(word, lineNumber);
}
}
}