-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST_of_Words.cpp
More file actions
167 lines (158 loc) · 4.43 KB
/
BST_of_Words.cpp
File metadata and controls
167 lines (158 loc) · 4.43 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
#include"BST_of_Words.h"
#include<algorithm>
#include <iterator>
using namespace std;
BST_of_Words::BST_of_Words()
{
pRoot=NULL;
}
void BST_of_Words::addFile(string fileName)
{
ifstream infile;
infile.open(fileName.c_str());
string line;
vector<string> tokens;
bool endOfFile=infile.eof();
int count=0;
Word* wording=new Word;
while(!endOfFile)
{
endOfFile=getStringAndTokens(infile,line,tokens);
// If I'm already read past the end of the file then the last read is invalid.
count++;
lines.add(line,fileName,count);
for(int i=0;i<tokens.size();i++)
{
wording=new Word;
wording->wordText=tokens[i];
wording->lineWhereFound=lines.tail;
addWord(*wording);
}
}
endOfFile=getStringAndTokens(infile,line,tokens);
// If I've already read past the end of the file then the last read is invalid.
if(!endOfFile)
{
count++;
lines.add(line,fileName,count);
for(int i=0;i<tokens.size();i++)
{
wording=new Word;
wording->wordText=tokens[i];
wording->lineWhereFound=lines.tail;
addWord(*wording);
}
}
infile.close();
}
TreeNode** BST_of_Words::findInsertionPoint(TreeNode** ppRoot, string wordToAdd)
{
if ((*ppRoot)==NULL)
return ppRoot;
if(wordToAdd<=((*((**ppRoot).pData)).wordText))
return findInsertionPoint(&((**ppRoot).pLeft),wordToAdd);
else
return findInsertionPoint(&((**ppRoot).pRight),wordToAdd);
}
void BST_of_Words::addWord(Word& wordToAdd)
{
string wordLetters=wordToAdd.wordText;
Line* lineNumber=wordToAdd.lineWhereFound;
TreeNode** ppInsertPoint=findInsertionPoint(&pRoot,wordLetters);
TreeNode* pNewTreeNode=new TreeNode;
pNewTreeNode->pLeft=NULL;
pNewTreeNode->pRight=NULL;
(*pNewTreeNode).pData=&wordToAdd;
(*ppInsertPoint)=pNewTreeNode;
}
string BST_of_Words::print()
{
string result="";
result=print(pRoot, result);
return result;
}
string BST_of_Words::print(TreeNode* pRootOfCurrentTree, string& result)
{
if(pRootOfCurrentTree==NULL)
return"";
print((*pRootOfCurrentTree).pLeft, result);
result+=(*((*pRootOfCurrentTree).pData)).wordText; //the data contained in what is pointed to by the data member of what is pointed to by pRootOfCurrentTree
result+="\n";
print((*pRootOfCurrentTree).pRight, result);
return result;
}
set<Word*> BST_of_Words::myFind(string thingToFind)
{
set<Word*> wordSet;
TreeNode** ppFound=myFind(&pRoot,thingToFind,&wordSet);
return wordSet;
}
TreeNode** BST_of_Words::myFind(TreeNode** ppRoot,string wordToFind,set<Word*>* wordSet)
{
if((*ppRoot)==NULL)
return ppRoot;
if((*((**ppRoot).pData)).wordText==wordToFind)
wordSet->insert((**ppRoot).pData);
if(wordToFind<=(*((**ppRoot).pData)).wordText)
return myFind(&((**ppRoot).pLeft),wordToFind,wordSet);
else
return myFind(&((**ppRoot).pRight),wordToFind,wordSet);
}
set<Line*> BST_of_Words::lineFind(string thingToFind)
{
set<Line*> lineSet;
TreeNode** ppFound=lineFind(&pRoot,thingToFind,&lineSet);
return lineSet;
}
TreeNode** BST_of_Words::lineFind(TreeNode** ppRoot,string wordToFind,set<Line*>* lineSet)
{
if((*ppRoot)==NULL)
return ppRoot;
if((*((**ppRoot).pData)).wordText==wordToFind)
lineSet->insert((**ppRoot).pData->lineWhereFound);
if(wordToFind<=(*((**ppRoot).pData)).wordText)
return lineFind(&((**ppRoot).pLeft),wordToFind,lineSet);
else
return lineFind(&((**ppRoot).pRight),wordToFind,lineSet);
}
void BST_of_Words::clear(TreeNode* pRootOfCurrentTree)
{
if(pRootOfCurrentTree==NULL)
return;
clear((*pRootOfCurrentTree).pLeft);
delete(*pRootOfCurrentTree).pData;
clear((*pRootOfCurrentTree).pRight);
}
BST_of_Words::~BST_of_Words()
{
clear(pRoot);
}
set<Line*> BST_of_Words::query(string queryLine)
{
sTree.pSRoot=new sTreeNode;
sTree.addContent(queryLine,sTree.pSRoot);
set<Line*> finalSet=evaluate(sTree.pSRoot);
return finalSet;
}
set<Line*> BST_of_Words::evaluate(sTreeNode* expression)
{
set<Line*> leftSet;
set<Line*> rightSet;
set<Line*> resultSet;
if(expression->content=="&")//then return a set that is the intersection of the two children.
{
leftSet=evaluate(expression->pLeftS);
rightSet=evaluate(expression->pRightS);
set_intersection(leftSet.begin(),leftSet.end(),rightSet.begin(),rightSet.end(),inserter(resultSet,resultSet.begin()));
return resultSet;
}
else if(expression->content=="|")
{
leftSet=evaluate(expression->pLeftS);
rightSet=evaluate(expression->pRightS);
set_union(leftSet.begin(),leftSet.end(),rightSet.begin(),rightSet.end(),inserter(resultSet,resultSet.begin()));
return resultSet;
}
else
return lineFind(expression->content);
}