-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathex-4-1-binaryTree.cpp
More file actions
284 lines (268 loc) · 8.44 KB
/
ex-4-1-binaryTree.cpp
File metadata and controls
284 lines (268 loc) · 8.44 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
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
using namespace std;
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <time.h>
class lNode{
private:
int line;
lNode *next;
public:
int getLine(){return line;}
lNode *getNext(){return next;}
void setNext(lNode *next){ this->next=next; }
lNode(int line){
this->line=line;
next=NULL;
}
};
class wNode{
private:
string word;
wNode* left;
wNode* right;
lNode* head;
public:
string getWord(){return word;}
wNode *getLeft(){return left;}
void setLeft(wNode *left){this->left=left;}
wNode *getRight(){return right;}
void setRight(wNode *right){this->right=right;}
wNode(string word, int lineNum){
this->word=word;
left=NULL;
right=NULL;
head=new lNode(lineNum);
}
wNode(string word, wNode *left, int lineNum){
this->word=word;
this->left=left;
right=NULL;
head=new lNode(lineNum);
}
wNode(string word, int lineNum, wNode *right){
this->word=word;
left=NULL;
this->right=right;
head=new lNode(lineNum);
}
wNode(string word, wNode *left, int lineNum, wNode *right){
this->word=word;
this->left=left;
this->right=right;
head=new lNode(lineNum);
}
void display(){
cout << word << ':';
lNode *temp=head;
do{
cout << ' ' << temp->getLine();
if(temp->getNext()!=NULL) cout << ',';
temp=temp->getNext();
}while(temp!=NULL);
cout << endl;
}
int getTimes(){
lNode *temp=head;
int times=0;
do{
times++;
temp=temp->getNext();
}while(temp!=NULL);
return times;
}
void addPos(int lineNum){
lNode *temp = head;
while(temp->getNext()!=NULL)
temp=temp->getNext();
if(temp->getLine()==lineNum) return;
temp->setNext(new lNode(lineNum));
}
};
string convert(string);
void displayAll(wNode *);
bool addLeft(wNode *,string, int);
bool addRight(wNode *,string,int);
bool add(wNode *,string,int);
wNode *fnd(wNode *, string);
void del(wNode **, string , int &, int &);
void displayStats(wNode *, int ,int ,int );
void exportToFile(wNode *, int ,int ,int);
int main() {
string path;
int textWords=0, indexWords=0;
wNode *root=NULL;
cout << "Insert relative file path:";
cin >> path;
ifstream myfile(path.c_str());
if(myfile.is_open()) {
clock_t start = clock();
string lineString;
int lineNum=0;
while ( getline(myfile,lineString) ) {
istringstream lineStream(lineString);
lineNum++;
string word;
while (lineStream >> word){
textWords++;
word=convert(word);
if(textWords==1) {
root = new wNode(word,lineNum);
indexWords++;
}
else if(add(root, word, lineNum)==true) indexWords++;
}
}
myfile.close();
double elapsed= ((double)(clock() - start))/CLOCKS_PER_SEC;
int choice=0;
while(choice!=3){
if(choice==0)
displayStats(root,textWords,indexWords,elapsed);
else if(choice==1){
cout << "Insert word: ";
string word;
cin >> word;
cout << "Insert line number: ";
int lineNum;
cin >> lineNum;
textWords++;
word=convert(word);
if(textWords==1) {
root = new wNode(word,lineNum);
indexWords++;
}
else if(add(root, word, lineNum)==true) indexWords++;
}
else if(choice==2){
cout << "Insert word: ";
string word;
cin >> word;
word=convert(word);
del(&root,word,textWords,indexWords);
}
else cout << "Wrong input" << endl;
cout << "0. Show stats" << endl << "1. Insert word" << endl << "2. Delete word" << endl << "3. Exit" << endl;
cout << "Insert the number of your choice: ";
cin >> choice;
}
exportToFile(root,textWords,indexWords,elapsed);
}
else
cout << "Can't process file!" << endl;
}
string convert(string word){
for(int i = 0; i < word.size(); i++)
word.at(i) = toupper(word.at(i));
if(!isalpha(word.at(word.size()-1)))
word=word.substr(0,word.size()-1);
return word;
};
void displayAll(wNode *root){
if(root==NULL) { cout << "The index is empty!" << endl; return; }
while(root->getLeft()!=NULL)
root=root->getLeft();
while(root!=NULL){
root->display();
root=root->getRight();
}
}
bool addLeft(wNode *root, string word, int lineNum){
while(root->getLeft()!=NULL && root->getWord()>word)
root=root->getLeft();
if(root->getWord()==word){
root->addPos(lineNum);
return false;
}
else if(root->getWord()<word){
root->setRight(new wNode(word, root, lineNum, root->getRight()));
root->getRight()->getRight()->setLeft(root->getRight());
}
else root->setLeft(new wNode(word,lineNum,root));
return true;
}
bool addRight(wNode *root, string word, int lineNum) {
while(root->getRight()!=NULL && root->getWord()<word)
root=root->getRight();
if(root->getWord()==word){
root->addPos(lineNum);
return false;
}
else if(root->getWord()>word){
root->setLeft(new wNode(word,root->getLeft(),lineNum,root));
root->getLeft()->getLeft()->setRight(root->getLeft());
}
else root->setRight(new wNode(word,root,lineNum));
return true;
}
bool add(wNode *root, string word, int lineNum) {
if(word>root->getWord()){
if(addRight(root,word,lineNum)==false)
return false;
}
else if(word<root->getWord()){
if(addLeft(root,word,lineNum)==false)
return false;
}
else if(word==root->getWord()){
root->addPos(lineNum);
return false;
}
return true;
}
wNode *fnd(wNode *root, string word){
if(root!=NULL){
if(word==root->getWord())
return root;
else if(word<root->getWord() && root->getLeft()!=NULL){
if(root->getWord()>word) return NULL;
else return fnd(root->getLeft(),word);
}
else if(root->getRight()!=NULL)
return fnd(root->getRight(),word);
}
return NULL;
}
void del(wNode **root, string word, int &textWords, int &indexWords) {
wNode *tmp=*root;
if(*root==NULL) { cout << "Can't delete because empty!" << endl; return; }
else if (tmp->getWord()==word) {
textWords-=tmp->getTimes();
if(tmp->getRight()!=NULL)
*root=tmp->getRight();
else if(tmp->getLeft()!=NULL)
*root=tmp->getLeft();
else { *root=NULL; return; }
tmp->getLeft()->setRight(tmp->getRight());
tmp->getRight()->setLeft(tmp->getLeft());
}
else {
wNode *del=NULL;
del=fnd(*root,word);
if(del!=NULL){
textWords-=del->getTimes();
if(del->getLeft()!=NULL)
del->getLeft()->setRight(del->getRight());
if(del->getRight()!=NULL)
del->getRight()->setLeft(del->getLeft());
}
else { cout << "Den vrethike h leksh" << endl; return; }
}
cout << "Diagrafike epituxws" << endl;
indexWords--;
}
void displayStats(wNode *root, int textWords,int indexWords,int elapsed){
cout << "Word Index:\n--------------------------------------------------\n";
displayAll(root);
cout << "Number of text words: " << textWords << endl;
cout << "Number of index words: " << indexWords << endl;
cout << "Time elapsed to create binary tree: " << elapsed << " sec." << endl;
}
void exportToFile(wNode *root, int textWords,int indexWords,int elapsed){
ofstream out("index_bst.txt");
streambuf *coutbuf = std::cout.rdbuf();
cout.rdbuf(out.rdbuf());
displayStats(root,textWords,indexWords,elapsed);
cout.rdbuf(coutbuf);
}