-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbst.cpp
More file actions
420 lines (349 loc) · 13.3 KB
/
Copy pathbst.cpp
File metadata and controls
420 lines (349 loc) · 13.3 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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include "bst.h"
#include <fstream>
#include <stdlib.h>
using namespace std;
/*********************************************************************************************
b s t . c p p
**********************************************************************************************
Description: This is a simple program to give the student experience writing code
for binary trees. This is a CLASS implementation of the BST ADT.
The student should study, comment, correct errors, compile,
implement/un-implemented/undeveloped functions, and modify code to make
it more efficient when ever necessary. The student should be able to
discuss the advantages and disadvantages of using such an
implementation.
*********************************************************************************************/
/*********************************************************************************************
Function Name: Default Constructor
Description: the default constructor will open the test file holding all county names and populations.
It will then get each line and split the line by the first space it finds and created substrings of name and population.
The substring containing the population would not stod correctly if the string of numbers was 7 digits or more.
Default constructor will then insert each lines country name and population.
*********************************************************************************************/
bst::bst()
{
root = 0;
ifstream data;
data.open("Data/RawData.txt");
if (data.fail())
{
cout << "Country Data input did not load.\n\n";
exit(1);
}
else
{
cout << "Country data input success\n\n";
}
string temp, name, population;
int pop;
//long double poptest = 1234567; //stod will not work for string of 7 digits. (I kept getting ######e# the dtod would only work on 6 digits and float the final integer as a straing e double)
while (!data.eof())
{
getline(data, temp);
for (int i = 0; i <= temp.length(); i++)
{
if (temp[i] == ' ')
{
name = temp.substr(0, i);
population = temp.substr(i + 1);
}
}
pop = stoi(population);
//(READ ME) neither stod or static/type cast would correctly convert a string of 7 digits. (I kept getting ######e# the dtod would only work on 6 digits and float the final integer as a straing e double)
//int popint = stoi(population);
//double pop = (double)popint;
//cout << setw(12) << name << setw(12) << population << endl;
insert(name, pop);
}
}
/*********************************************************************************************
Function Name: isempty?
Description: returns whether or not the tree is empty.
*********************************************************************************************/
bool bst::empty()
{
return (root == 0);
}
/*********************************************************************************************
Function Name: insert AUX (for main)
Description: this function will be used in the main and overloaded insert to recursivly navigate
the tree and insert the data into its respective location.
*********************************************************************************************/
void bst::insert(const string& item, const int& population)
{
insert(root, item, population);
}
/*********************************************************************************************
Function Name: insert
Description: this overloaded insert function will recursivly navigate the tree from lchild (lowest)
to rchild(highest).
*********************************************************************************************/
void bst::insert(treenode*& loc_ptr, const string& item, const int& population)
{
if (loc_ptr == 0) // base case, tree is empty create new node
{
loc_ptr = new treenode;
loc_ptr->lchild = loc_ptr->rchild = 0;
loc_ptr->county_name = item;
loc_ptr->population_size = population;
}
else if (loc_ptr->county_name > item)
insert(loc_ptr->lchild, item, population);
else if (loc_ptr->county_name < item)
insert(loc_ptr->rchild, item, population);
else
cout << "the item is already in the tree\n";
}
/*********************************************************************************************
Function Name: search tree AUX (for main)
Description: This function will be used in the main and the overloaded search tree function
to navigate the tree and search from lowest to highest for the item.
*********************************************************************************************/
treenode* bst::search_tree(string item)
{
return search_tree(root, item);
}
/*********************************************************************************************
Function Name: search tree
Description: This overloaded search tree function will be used to navigate the tree and search
from lowest to highest for the item by recursively checking the left child then the right child.
*********************************************************************************************/
treenode* bst::search_tree(treenode* loc_ptr, string item)
{
if (loc_ptr != 0)
{
if (loc_ptr->county_name == item) //base case
{
return loc_ptr;
}
else if (loc_ptr->county_name > item)
return search_tree(loc_ptr->lchild, item);
else
return search_tree(loc_ptr->rchild, item);
}
else
return loc_ptr;
}
/*********************************************************************************************
Function Name: del name AUX (far main)
Description: This function will be used in the main and the overloaded del name function
to navigate the tree and delete the item.
*********************************************************************************************/
void bst::del_name(string item)
{
del_name(root, item);
}
/*********************************************************************************************
Function Name: del name
Description: This overloaded del name function will be used to navigate the tree and delete the item.
*********************************************************************************************/
void bst::del_name(treenode*& loc_ptr, string item)
{
if (loc_ptr == 0) //base case return root = 0 (else search tree)
cout << "Item not in the tree\n";
else if (loc_ptr->county_name > item)
del_name(loc_ptr->lchild, item);
else if (loc_ptr->county_name < item)
del_name(loc_ptr->rchild, item);
else
{
treenode* ptr;
if (loc_ptr->lchild == 0)
{
ptr = loc_ptr->rchild;
delete loc_ptr;
loc_ptr = ptr;
}
else if (loc_ptr->rchild == 0)
{
ptr = loc_ptr->lchild;
delete loc_ptr;
loc_ptr = ptr;
}
else
{
ptr = inorder_succ(loc_ptr);
loc_ptr->county_name = ptr->county_name;
del_name(loc_ptr->rchild, ptr->county_name);
}
}
}
/*********************************************************************************************
Function Name: in order succ
Description: reorders the left and right nodes if an item is deleted by shifting the pointers.
*********************************************************************************************/
treenode* bst::inorder_succ(treenode* loc_ptr)
{
treenode* ptr = loc_ptr->rchild;
while (ptr->lchild != 0)
{
ptr = ptr->lchild;
}
return ptr;
}
/*********************************************************************************************
Function Name: country ranges AUX (for main)
Description: This recursive function will be used in the main and the overloaded county ranges function.
*********************************************************************************************/
void bst::county_ranges(const string& min_name, const string& max_name) //recursive
{
county_ranges(root, min_name, max_name); //(recursive)
}
/*********************************************************************************************
Function Name: country ranges
Description:This recursive overloaded country ranges function will be used to navigate the tree
and print out the given (min-max) county ranges. (INCLUSIVE)
*********************************************************************************************/
void bst::county_ranges(treenode*& loc_ptr, const string& min_name, const string& max_name)
{
if (loc_ptr != 0)
{
county_ranges(loc_ptr->lchild, min_name, max_name); //(recursive) lchild < r < rchild
if (loc_ptr->county_name[0] >= min_name[0] && loc_ptr->county_name <= max_name) //inclusively
{
cout << setw(12) << loc_ptr->county_name << endl;
}
county_ranges(loc_ptr->rchild, min_name, max_name); //(recursive)
}
}
/*********************************************************************************************
Function Name: print tree AUX (for main)
Description: This recursive function will be used in the main and the overloaded print tree function
to navigate the tree and print the items.
*********************************************************************************************/
void bst::print_tree()
{
print_tree(root);
}
/*********************************************************************************************
Function Name: print tree (inorder)
Description: This recursive function will be used in the main and the overloaded print tree function
to navigate the tree and print the items.
*********************************************************************************************/
void bst::print_tree(treenode*& loc_ptr)
{
if (loc_ptr != 0)
{
/* (in order LVR) */
print_tree(loc_ptr->lchild);
cout << setw(12) << loc_ptr->county_name << endl;
//cout << setw(12) << loc_ptr->population_size << endl; //echo for testing
print_tree(loc_ptr->rchild);
/* end in order */
/* (preorder VLR) *
cout << loc_ptr->county_name << endl;
print_tree(loc_ptr->lchild);
print_tree(loc_ptr->rchild);
/* end preorder */
/* (postorder LRV) *
print_tree(loc_ptr->lchild);
print_tree(loc_ptr->rchild);
cout << loc_ptr->county_name << endl;
/* end postorder */
}
}
/*********************************************************************************************
Function Name: sorted info AUX (for main)
Description: This recursive function will be used in the main and the overloaded sorted info function
to navigate the tree and print the items to a new sorted output file (country_info.txt).
*********************************************************************************************/
void bst::sorted_info()
{
ofstream sorted;
sorted.open("Data/CountryInfo.txt");
if (sorted.fail())
{
cout << "Country Data output file did not load.\n\n";
exit(1);
}
else
{
cout << "Country data output success\n\n";
}
sorted_info(sorted, root);
sorted.close();
}
/*********************************************************************************************
Function Name: sorted info
Description: This recursive function will be used to navigate throughout the tree and diplay
the contents to the screen in order.
*********************************************************************************************/
void bst::sorted_info(ostream& sorted, treenode*& loc_ptr)
{
if (loc_ptr != 0)
{
sorted_info(sorted, loc_ptr->lchild);
sorted << setw(12) << loc_ptr->county_name << setw(12) << loc_ptr->population_size << endl;
sorted_info(sorted, loc_ptr->rchild);
}
}
/*********************************************************************************************
Function Name: empty tree
Description: de-allocate all nodes that were allocated to the bst
*********************************************************************************************/
void bst::empty_tree()
{
bst::~bst();
}
/*********************************************************************************************
Function Name: count node aux
Description: de-allocate all nodes that were allocated to the bst
*********************************************************************************************/
void bst::count()
{
count(root);
}
/*********************************************************************************************
Function Name: count nodes
Description: count the # of nodes in BT
*********************************************************************************************/
int bst::count(treenode* loc_ptr)
{
if (loc_ptr == 0)
{
return 0;
}
else
{
return 1 + count(loc_ptr->lchild) + count(loc_ptr->rchild);
}
}
/*********************************************************************************************
Function Name: count nodes with 2 children AUX
Description: count the # of nodes in BT
*********************************************************************************************/
void bst::count2()
{
count2(root);
}
/*********************************************************************************************
Function Name: count nodes with 2 children
Description: count the # of nodes in BT with 2 children
*********************************************************************************************/
int bst::count2(treenode* loc_ptr)
{
if (loc_ptr == 0)
{
return 0;
}
else if (loc_ptr->lchild != 0 && loc_ptr->rchild != 0)
{
return 1 + count2(loc_ptr->lchild) + count2(loc_ptr->rchild);
}
else
{
return count2(loc_ptr->lchild) + count2(loc_ptr->rchild);
}
}
/*********************************************************************************************
Function Name: destructor
Description: de-allocates dynamic memory allocate for tree
*********************************************************************************************/
bst::~bst()
{
cout << "The Destructor called" << endl;
while (root != 0)
{
del_name(root->county_name);
}
}