-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearchTree.cpp
409 lines (350 loc) · 9.72 KB
/
BinarySearchTree.cpp
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
//============================================================================
// Name : BinarySearchTree.cpp
// Author : David France
// Version : 1.0
//============================================================================
#include <iostream>
#include <algorithm>
#include <time.h>
#include "CSVparser.hpp"
using namespace std;
//============================================================================
// Global definitions visible to all methods and classes
//============================================================================
// forward declarations
double strToDouble(string str, char ch);
// define a structure to hold bid information
struct Bid {
string bidId; // unique identifier
string title;
string fund;
double amount;
Bid() {
amount = 0.0;
}
};
void displayBid(Bid bid);
// Internal structure for tree node
struct Node {
Bid bid;
Node* left;
Node* right;
Node() {
left = nullptr;
right = nullptr;
}
Node(Bid newBid) : Node() {
this->bid = newBid;
}
};
//============================================================================
// Binary Search Tree class definition
//============================================================================
/**
* Define a class containing data members and methods to
* implement a binary search tree
*/
class BinarySearchTree {
private:
Node* root;
void addNode(Node* node, Bid bid);
void inOrder(Node* node);
Node* removeNode(Node* node, string bidId);
void inOrderRemove(Node* node);
public:
BinarySearchTree();
virtual ~BinarySearchTree();
void InOrder();
void Insert(Bid bid);
void Remove(string bidId);
Bid Search(string bidId);
};
/**
* Default constructor
*/
BinarySearchTree::BinarySearchTree() {
// initialize housekeeping variables
root = nullptr;
}
/**
* Destructor
*/
BinarySearchTree::~BinarySearchTree() {
// Recursive function starting at root
this->inOrderRemove(root);
}
/**
* Traverse the tree in order
*/
void BinarySearchTree::InOrder() {
this->inOrder(root);
}
/**
* Insert a bid
*/
void BinarySearchTree::Insert(Bid bid) {
// If tree is empty, bid becomes first node
if (root == nullptr) {
root = new Node(bid);
}
else {
// Recursive function starting at the root
this->addNode(root, bid);
}
}
/**
* Remove a bid
*/
void BinarySearchTree::Remove(string bidId) {
// Recursive function for removing starting at the root
this->removeNode(root, bidId);
}
/**
* Search for a bid
*/
Bid BinarySearchTree::Search(string bidId) {
Bid bid;
// Start search at the root
Node* current = root;
// Loop compares bid to bid at current node, returning a match. Search goes
// to left node if bidId is less than current bidId, else it goes to right
// node.
while (current != nullptr) {
if (current->bid.bidId.compare(bidId) == 0) {
return current->bid;
}
if (current->bid.bidId.compare(bidId) > 0) {
current = current->left;
} else {
current = current->right;
}
}
return bid;
}
/**
* Add a bid to some node (recursive)
*
* @param node Current node in tree
* @param bid Bid to be added
*/
void BinarySearchTree::addNode(Node* node, Bid bid) {
// If node's bidId is greater than bidId to insert, go to left node and check
// for nullptr, else go right. Recursively call addNode() until nullptr is
// found and bid is added
if (node->bid.bidId.compare(bid.bidId) > 0) {
if (node->left == nullptr) {
node->left = new Node(bid);
} else {
this->addNode(node->left, bid);
}
}
else {
if (node->right == nullptr) {
node->right = new Node(bid);
} else {
this->addNode(node->right, bid);
}
}
}
/**
* Remove a node containing Bid (recursive)
*
* @param node Current node in tree
* @param bidId ID of Bid to be removed
*/
Node* BinarySearchTree::removeNode(Node* node, string bidId) {
if (node == nullptr) {
return node;
}
// If bidid of node is greater than bidId to remove, go to left branch
if (node->bid.bidId.compare(bidId) > 0) {
node->left = removeNode(node->left, bidId);
}
// If bidid of node is less than bidId to remove, go to right branch
else if (node->bid.bidId.compare(bidId) < 0) {
node->right = removeNode(node->right, bidId);
}
// Current node contains bid to remove
else {
// If node has no children, delete node
if (node->left == nullptr && node->right == nullptr) {
delete node;
node = nullptr;
}
// If node has one left child, node = left node, then delete node
else if (node->left != nullptr && node->right == nullptr) {
Node* temp = node;
node = node->left;
delete temp;
}
// If node has one right child, node = right, then delete node
else if (node->left == nullptr && node->right != nullptr) {
Node* temp = node;
node = node->right;
delete temp;
}
// If node has two children, find the leftmost node of the right child
// Make that node's bid the current node's bid, then remove that node
else {
Node* temp = node->right;
while (temp->left != nullptr) {
temp = temp->left;
}
node->bid = temp->bid;
node->right = removeNode(node->right, temp->bid.bidId);
}
}
return node;
}
/**
* Print each Bid in id order
* @param node current Node in tree
*/
void BinarySearchTree::inOrder(Node* node) {
if (node != nullptr) {
inOrder(node->left);
displayBid(node->bid);
inOrder(node->right);
}
}
/**
* Remove each Bid in id order
* @param node current Node in tree
*/
void BinarySearchTree::inOrderRemove(Node* node) {
if (node != nullptr) {
inOrderRemove(node->left);
Node* temp = node;
delete node;
inOrderRemove(temp->right);
delete temp;
}
}
//============================================================================
// Static methods used for testing
//============================================================================
/**
* Display the bid information to the console (std::out)
*
* @param bid struct containing the bid info
*/
void displayBid(Bid bid) {
cout << bid.bidId << ": " << bid.title << " | " << bid.amount << " | "
<< bid.fund << endl;
return;
}
/**
* Load a CSV file containing bids into a container
*
* @param csvPath the path to the CSV file to load
* @return a container holding all the bids read
*/
void loadBids(string csvPath, BinarySearchTree* bst) {
cout << "Loading CSV file " << csvPath << endl;
// initialize the CSV Parser using the given path
csv::Parser file = csv::Parser(csvPath);
// read and display header row - optional
vector<string> header = file.getHeader();
for (auto const& c : header) {
cout << c << " | ";
}
cout << "" << endl;
try {
// loop to read rows of a CSV file
for (unsigned int i = 0; i < file.rowCount(); i++) {
// Create a data structure and add to the collection of bids
Bid bid;
bid.bidId = file[i][1];
bid.title = file[i][0];
bid.fund = file[i][8];
bid.amount = strToDouble(file[i][4], '$');
//cout << "Item: " << bid.title << ", Fund: " << bid.fund << ", Amount: " << bid.amount << endl;
// push this bid to the end
bst->Insert(bid);
}
} catch (csv::Error &e) {
std::cerr << e.what() << std::endl;
}
}
/**
* Simple C function to convert a string to a double
* after stripping out unwanted char
*
* credit: http://stackoverflow.com/a/24875936
*
* @param ch The character to strip out
*/
double strToDouble(string str, char ch) {
str.erase(remove(str.begin(), str.end(), ch), str.end());
return atof(str.c_str());
}
/**
* The one and only main() method
*/
int main(int argc, char* argv[]) {
// process command line arguments
string csvPath, bidKey;
switch (argc) {
case 2:
csvPath = argv[1];
bidKey = "98109";
break;
case 3:
csvPath = argv[1];
bidKey = argv[2];
break;
default:
csvPath = "eBid_Monthly_Sales_Dec_2016.csv";
bidKey = "98109";
}
// Define a timer variable
clock_t ticks;
// Define a binary search tree to hold all bids
BinarySearchTree* bst;
Bid bid;
int choice = 0;
while (choice != 9) {
cout << "Menu:" << endl;
cout << " 1. Load Bids" << endl;
cout << " 2. Display All Bids" << endl;
cout << " 3. Find Bid" << endl;
cout << " 4. Remove Bid" << endl;
cout << " 9. Exit" << endl;
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
bst = new BinarySearchTree();
// Initialize a timer variable before loading bids
ticks = clock();
// Complete the method call to load the bids
loadBids(csvPath, bst);
//cout << bst->Size() << " bids read" << endl;
// Calculate elapsed time and display result
ticks = clock() - ticks; // current clock ticks minus starting clock ticks
cout << "time: " << ticks << " clock ticks" << endl;
cout << "time: " << ticks * 1.0 / CLOCKS_PER_SEC << " seconds" << endl;
break;
case 2:
bst->InOrder();
break;
case 3:
ticks = clock();
bid = bst->Search(bidKey);
ticks = clock() - ticks; // current clock ticks minus starting clock ticks
if (!bid.bidId.empty()) {
displayBid(bid);
} else {
cout << "Bid Id " << bidKey << " not found." << endl;
}
cout << "time: " << ticks << " clock ticks" << endl;
cout << "time: " << ticks * 1.0 / CLOCKS_PER_SEC << " seconds" << endl;
break;
case 4:
bst->Remove(bidKey);
break;
}
}
cout << "Good bye." << endl;
return 0;
}