-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.cpp
More file actions
281 lines (220 loc) · 5.45 KB
/
BST.cpp
File metadata and controls
281 lines (220 loc) · 5.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
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
/*
Cosimo Gonnelli
The purpose of this program is to read in an external data files
of questions. The data structure used for these questions is a BST.
Each node of the tree will hold a LLL of questions of the same
type (inheritance, dynamic binding, or operator overloading).
These questions will be randomly selected by the user to create a
test. The data structure used for the test is a DLL.
*/
#include"BST.h"
//---------------------------------b_node----------------------------
//default constructor
b_node::b_node(): right(NULL), left(NULL), myCategory(NULL)
{}
//destructor
b_node::~b_node()
{
}
//make a copy of a category
b_node::b_node(const category & toCopy)
{
right = NULL;
left = NULL;
myCategory = toCopy;
}
//getter for right
b_node *& b_node::goRight()
{
return right;
}
//getter for left
b_node *& b_node::goLeft()
{
return left;
}
//set right
void b_node::setRight(b_node * connect)
{
right = connect;
}
//set left
void b_node::setLeft(b_node * connect)
{
left = connect;
}
//display category
void b_node::display() const
{
myCategory.display();
}
//compare to decide if go left or right
int b_node::compare(const b_node * toCompare) const
{
return myCategory.compare(toCompare -> myCategory);
}
//get category
category & b_node::getCategory()
{
return myCategory;
}
//---------------------------------BST----------------------
//default constructor
BST::BST(): root(NULL)
{}
//destructor as wrapper
BST::~BST()
{
destroy();
}
//destroy wrapper
int BST::destroy()
{
if(!root) return 0;
return destroy(root);
}
//delete the tree
int BST::destroy(b_node *& root)
{
if(!root) return 0;
int count = destroy(root -> goLeft()) + destroy(root -> goRight()) + 1;
delete root;
root = NULL;
return count;
}
//wrapper to insert a category into the tree
int BST::insertCategory(category newCategory)
{
//create a new node obj right into the function call
return insertCategory(root, new b_node(newCategory));
}
//insert a category
int BST::insertCategory(b_node *& root, b_node * toAdd)
{
if(!root)
{
root = toAdd; //connect the two nodes
root -> setLeft(NULL);
root -> setRight(NULL);
return 1;
}
//check if go to the right
if(toAdd -> compare(root) >= 0)
insertCategory(root -> goRight(), toAdd);
//go to the left
else
insertCategory(root -> goLeft(), toAdd);
return 1;
}
//wrapper to insert a question
int BST::insertMulti(char * categoryToAdd, mChoice *& newQuestion)
{
return insertMulti(root, categoryToAdd, newQuestion);
}
//insert m. cohice
int BST::insertMulti(b_node *& root, char * categoryToAdd, mChoice *& toAdd)
{
if(!root) return 0;
if(strcmp(root -> getCategory().getName(), categoryToAdd) == 0)
{
root -> getCategory().insertQ(toAdd);
}
return insertMulti(root -> goLeft(), categoryToAdd, toAdd) +
insertMulti(root -> goRight(), categoryToAdd, toAdd) +1;
}
//wrapper to insert a question
int BST::insertTrueFalse(char * categoryToAdd, trueFalse *& newQuestion)
{
return insertTrueFalse(root, categoryToAdd, newQuestion);
}
//insert true false
int BST::insertTrueFalse(b_node *& root, char * categoryToAdd, trueFalse *& toAdd)
{
if(!root) return 0;
if(strcmp(root -> getCategory().getName(), categoryToAdd) == 0)
{
root -> getCategory().insertTF(toAdd);
}
return insertTrueFalse(root -> goLeft(), categoryToAdd, toAdd) +
insertTrueFalse(root -> goRight(), categoryToAdd, toAdd) +1;
}
//wrapper fro retrieve
int BST::retrieve(int number, char * category, question *& found)
{
return retrieve(root, number, category, found);
}
//retrieve a question match and supplied to the DLL
int BST::retrieve(b_node *& root, int number, char * category,question *& found)
{
if(!root) return 0;
if(strcmp(root -> getCategory().getName(), category) == 0)
return root -> getCategory().retrieveQ(number, found);
return retrieve(root -> goLeft(), number, category, found) +
retrieve(root -> goRight(), number, category, found);
}
//wrapper display
void BST::displayAll() const
{
return displayAll(root);
}
//display
void BST::displayAll(b_node * root) const
{
if(!root) return;
displayAll(root -> goLeft());
root -> display();
displayAll(root -> goRight());
}
//addition operator overloading
BST BST::operator + (const category & toAdd)
{
BST temp;
temp.insertCategory(toAdd);
return temp;
}
//addition assignment operator overloading
BST BST::operator += (const category & toAdd)
{
insertCategory(toAdd);
return * this;
}
//relational/equality operator overloading
bool BST::operator == (const category & toCompare)
{
if(!root) return false;
return check(root, toCompare);
}
//functin to check if match
bool BST::check(b_node * root, const category & toCheck)
{
if(!root) return false;
if(root -> getCategory().compare(toCheck) == 0)
return true;
return check(root -> goLeft(), toCheck) + check(root -> goRight(), toCheck);
}
//relational/equality operator overloading
bool BST::operator != (const category & toCompare)
{
if(!root) return false;
return check2(root, toCompare);
}
//functin to check if match
bool BST::check2(b_node * root, const category & toCheck)
{
if(!root) return false;
if(root -> getCategory().compare(toCheck) != 0)
return true;
return check2(root -> goLeft(), toCheck) + check2(root -> goRight(), toCheck);
}
//insertion operator overloading
istream & operator >> (istream & in, category & toAdd)
{
in >> toAdd;
return in;
}
//extraction operator overloading
ostream & operator << (ostream & out, const BST & source)
{
source.displayAll();
return out;
}