-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVLTree.cpp
More file actions
663 lines (615 loc) · 21.5 KB
/
Copy pathAVLTree.cpp
File metadata and controls
663 lines (615 loc) · 21.5 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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
#include <iostream>
#include<string>
#include <vector>
#include <queue>
#include <fstream>
#include <regex>
using namespace std;
// every node contains a Student object w/ their name & ID
struct Student{
string name;
string ID;
Student(){};
Student(string name, string ID);
};
Student::Student(string name, string ID) {
this->name = name;
this->ID = ID;
}
// cited from the code given in class
class BST
{
private:
struct TreeNode
{
Student val;
TreeNode *left;
TreeNode *right;
TreeNode(string name, string ID) : val(name, ID), left(nullptr), right(nullptr) {}
};
TreeNode* root = nullptr;
BST::TreeNode* helperInsert(TreeNode* helpRoot, const string& name, const string& ID, vector<TreeNode*> &insertedRoots);
void helperInOrder(TreeNode* helpRoot, vector<TreeNode*> &returnVec);
void helperPreOrder(TreeNode* helpRoot, vector<TreeNode*> &returnVec);
void helperPostOrder(TreeNode* helpRoot, vector<TreeNode*> &returnVec);
BST::TreeNode* helperSearchID(TreeNode* helpRoot, string searchID);
void helperSearchName(TreeNode* helpRoot, const string& searchName, vector<string> &matchNames);
void helperDelete(TreeNode* &helpRoot, const string& searchID, vector<TreeNode*> &deletedRoots);
int helperLevelCount(TreeNode* helpRoot);
void helperRemoveInOrder(TreeNode* &helpRoot, int &n, vector<TreeNode*> &deletedRoots);
TreeNode* rotateLeft(TreeNode* &node);
TreeNode* rotateRight(TreeNode* &node);
public:
vector<BST::TreeNode*> inorder();
vector<BST::TreeNode*> preorder();
vector<BST::TreeNode*> postorder();
vector<BST::TreeNode*> insert(string name, string ID);
void searchID(string searchID);
vector<string> searchName(string name);
vector<BST::TreeNode*> removeID(string ID);
void printLevelCount();
vector<TreeNode *> removeInOrder(int n);
// added this return vector for Catch2 test cases
vector<BST::TreeNode*> returnVec;
};
// both level count & height finder
int BST::helperLevelCount(TreeNode* helpRoot) {
// base case: we've gotten to the end of the tree
if (helpRoot == nullptr){
return 0;
}
// finding the height of the left subtree
int maxLeft = helperLevelCount(helpRoot->left);
// finding the height of the right subtree
int maxRight = helperLevelCount(helpRoot->right);
// return the max of either of these for each root, we should find the height
// also add one for the current level
return 1 + max(maxLeft, maxRight);
}
// cited from the given lecture slides (slide 12 on balanced trees)
BST::TreeNode* BST::rotateLeft(BST::TreeNode* &node) {
// check for nullptr
if (node == nullptr){
return node;
}
TreeNode* grandchild = node->right->left;
TreeNode* newParent = node->right;
newParent->left = node;
node->right = grandchild;
return newParent;
}
BST::TreeNode* BST::rotateRight(BST::TreeNode *&node) {
// check for nullptr
if (node == nullptr){
return node;
}
// opposite of left rotation
TreeNode* grandchild = node->left->right;
TreeNode* newParent = node->left;
newParent->right = node;
node->left = grandchild;
return newParent;
}
// helper function to insert node and check balance factor for AVL tree
BST::TreeNode* BST::helperInsert(BST::TreeNode *helpRoot, const string& name, const string& ID, vector<TreeNode*> &insertedNodes) {
if (helpRoot == nullptr){
insertedNodes.push_back(new TreeNode(name, ID));
return new TreeNode(name,ID);
}
// Comparing the id thats being passed in to the id that is already in there
// Is the ID less than the current node val?
else if (ID.compare(helpRoot->val.ID) < 0){
helpRoot->left = helperInsert(helpRoot->left, name, ID, insertedNodes);
}
// Is the ID greater than the current node val?
else if (ID.compare(helpRoot->val.ID) > 0){
helpRoot->right = helperInsert(helpRoot->right, name, ID, insertedNodes);
}
else if (ID.compare(helpRoot->val.ID) == 0) {
return helpRoot;
}
// check the height of the deepest node's right and left subtrees
int heightLeft = helperLevelCount(helpRoot->left);
int heightRight = helperLevelCount(helpRoot->right);
// right heavy
if (heightLeft - heightRight < -1){
// check if right subtree is left heavy
if (helperLevelCount(helpRoot->right->left) - helperLevelCount(helpRoot->right->right) >= 1){
// perform right left rotation
helpRoot->right = rotateRight(helpRoot->right);
helpRoot = rotateLeft(helpRoot);
} else {
// perform left rotation
helpRoot = rotateLeft(helpRoot);
}
}
// left heavy
if (heightLeft - heightRight > 1){
// check if left subtree is right heavy
if (helperLevelCount(helpRoot->left->left) - helperLevelCount(helpRoot->left->right) <= -1){
// perform left right rotation
helpRoot->left = rotateLeft(helpRoot->left);
helpRoot = rotateRight(helpRoot);
}
else {
// perform right rotation
helpRoot = rotateRight(helpRoot);
}
}
return helpRoot;
}
// helper function to ensure inorder: left, node, right
void BST::helperInOrder(BST::TreeNode *helpRoot, vector<TreeNode*> &roots) {
if (helpRoot == nullptr){
cout << "";
}
else {
helperInOrder(helpRoot->left, roots);
roots.push_back(helpRoot);
helperInOrder(helpRoot->right, roots);
}
}
// helper function to ensure preorder: node, left, right
void BST::helperPreOrder(BST::TreeNode *helpRoot, vector<TreeNode*> &roots){
if (helpRoot == nullptr){
cout << "";
}
else {
roots.push_back(helpRoot);
helperPreOrder(helpRoot->left, roots);
helperPreOrder(helpRoot->right, roots);
}
}
// helper function to ensure postorder: left, right, node
void BST::helperPostOrder(BST::TreeNode *helpRoot, vector<TreeNode*> &roots) {
if (helpRoot == nullptr) {
cout << "";
}
else {
helperPostOrder(helpRoot->left, roots);
helperPostOrder(helpRoot->right, roots);
roots.push_back(helpRoot);
}
}
// finds name searching for IDS
BST::TreeNode* BST::helperSearchID(BST::TreeNode *helpRoot, string searchID) {
if (helpRoot == nullptr || helpRoot->val.ID == searchID){
return helpRoot;
}
// is the id we search for less than the current root?
if (searchID.compare(helpRoot->val.ID) < 0){
return helperSearchID(helpRoot->left, searchID);
}
// greater than current root
return helperSearchID(helpRoot->right, searchID);
}
// searches for every ID associated w/ name & prints them
void BST::helperSearchName(BST::TreeNode *helpRoot, const string &searchName, vector<string> &matchNames) {
if (helpRoot == nullptr){
return;
}
// preorder traverse the entire tree
if (helpRoot->val.name == searchName){
cout << helpRoot->val.ID << "\n";
// this vector is to check if the function has actually found any matching nodes
matchNames.push_back(helpRoot->val.ID);
}
// search to the left and right nodes
helperSearchName(helpRoot->left, searchName, matchNames);
helperSearchName(helpRoot->right, searchName, matchNames);
}
// Olog(n)
void BST::helperDelete(BST::TreeNode *&helpRoot, const string& searchID, vector<TreeNode*> &deletedRoots) {
// if it ever gets to this point, ID is not in the tree
if (helpRoot == nullptr){
cout << "unsuccessful" << endl;
return;
}
// node to be deleted is less than current id
if (searchID.compare(helpRoot->val.ID) < 0){
helperDelete(helpRoot->left, searchID, deletedRoots);
}
// greater than current id
else if (searchID.compare(helpRoot->val.ID) > 0){
helperDelete(helpRoot->right, searchID, deletedRoots);
}
// we found the node to delete
else {
// case where removed root has no children
if (helpRoot->left == nullptr && helpRoot->right == nullptr){
deletedRoots.push_back(helpRoot);
helpRoot = nullptr;
delete helpRoot;
}
// case where removed root has one child
else if (helpRoot->left == nullptr || helpRoot->right == nullptr){
// if the left is not null, copy its value to the current node and delete the left
if (helpRoot->left != nullptr){
deletedRoots.push_back(helpRoot);
helpRoot->val.ID = helpRoot->left->val.ID, helpRoot->val.name = helpRoot->left->val.name;
helpRoot->left = nullptr;
delete helpRoot->left;
}
// same process for the right
if (helpRoot->right != nullptr){
// vector to keep track of deleted root
deletedRoots.push_back(helpRoot);
helpRoot->val.ID = helpRoot->right->val.ID, helpRoot->val.name = helpRoot->right->val.name;
helpRoot->right = nullptr;
delete helpRoot->right;
}
}
// case where removed root has two children
else{
// right child has no left; its the inorder successor
if (helpRoot->right->left == nullptr){
deletedRoots.push_back(helpRoot);
helpRoot->val.ID = helpRoot->right->val.ID, helpRoot->val.name = helpRoot->right->val.name;
helperDelete(helpRoot->right, helpRoot->right->val.ID,deletedRoots);
// set right child leftmost to be inorder successor, then run the function again to delete the node
} else {
// init temp root to hold deleted value
TreeNode* temp = helpRoot->right;
deletedRoots.push_back(helpRoot);
// looking for inorder successor - leftmost child
while (temp->left != nullptr){
temp = temp->left;
}
// replacing helpRoot w/ inorder successor value
helpRoot->val.ID = temp->val.ID, helpRoot->val.name = temp->val.name;
// we now delete the inorder successor w/ the value
helperDelete(helpRoot->right, temp->val.ID, deletedRoots);
}
}
}
}
void BST::helperRemoveInOrder(BST::TreeNode* &helpRoot, int &n, vector<TreeNode*> &deletedRoots) {
// base case
if (helpRoot == nullptr || n < 0) {
return;
}
// traverse the tree in order & only decrement when we reach the farthest node to the left
helperRemoveInOrder(helpRoot->left, n, deletedRoots);
// when n is 0 we are at the node to delete
if (n == 0){
helperDelete(helpRoot, helpRoot->val.ID, deletedRoots);
// set n to negative 1 for a stopping condition
n = -1;
return;
}
n = n-1;
// look at right subtree
helperRemoveInOrder(helpRoot->right, n, deletedRoots);
}
// inserting node into tree
vector<BST::TreeNode*> BST::insert(string name, string ID){
vector<TreeNode*> nodes;
this->root = helperInsert(this->root, name, ID,nodes);
if(nodes.empty()){
cout << "unsuccessful" << endl;
} else {
cout << "successful" << endl;
}
return nodes;
}
// printing nodes inorder
vector<BST::TreeNode*> BST::inorder() {
// testing purposes: make tree is being traversed correctly
vector <TreeNode*> orderedRoots;
helperInOrder(this->root, orderedRoots);
for (int i = 0; i < orderedRoots.size(); i++){
if (i == orderedRoots.size() - 1){
cout << orderedRoots[i]->val.name;
} else {
cout << orderedRoots[i]->val.name << ", ";
}
}
cout << endl;
return orderedRoots;
}
// printing nodes preorder
vector<BST::TreeNode*> BST::preorder(){
// testing purposes: make tree is being traversed correctly
vector <TreeNode*> orderedRoots;
helperPreOrder(this->root, orderedRoots);
for (int i = 0; i < orderedRoots.size(); i++){
if (i == orderedRoots.size() - 1){
cout << orderedRoots[i]->val.name;
} else {
cout << orderedRoots[i]->val.name << ", ";
}
}
cout << endl;
return orderedRoots;
}
// printing nodes postorder
vector<BST::TreeNode*> BST::postorder() {
// testing purposes: make tree is being traversed correctly
vector <TreeNode*> orderedRoots;
helperPostOrder(this->root, orderedRoots);
for (int i = 0; i < orderedRoots.size(); i++){
if (i == orderedRoots.size() - 1){
cout << orderedRoots[i]->val.name;
} else {
cout << orderedRoots[i]->val.name << ", ";
}
}
cout << endl;
return orderedRoots;
}
// search for specific ID, print name associated w/ it
void BST::searchID(string searchID){
// create a new TreeNode object to hold the value of searchID
TreeNode* foundRoot = helperSearchID(this->root, searchID);
if (helperSearchID(this->root, searchID) == nullptr){
cout << "unsuccessful" << endl;
return;
}
cout << foundRoot->val.name << endl;
}
// search for name, return a vector with all roots that have that name
vector<string> BST::searchName(string name) {
// create vector to hold matched name, empty = unsuccessful search
vector<string> IDRoots;
helperSearchName(this->root, name, IDRoots);
if (IDRoots.empty()){
cout << "unsuccessful" << endl;
}
return IDRoots;
}
// removing a node based on ID
vector<BST::TreeNode*> BST::removeID(string ID) {
vector <TreeNode*> deletedRoots;
helperDelete(this->root, ID, deletedRoots);
if (!deletedRoots.empty()){
cout << "successful" << endl;
}
return deletedRoots;
}
void BST::printLevelCount() {
cout << helperLevelCount(this->root);
}
// removes nodes in tree based on inorder traversal
vector<BST::TreeNode *> BST::removeInOrder(int n) {
vector <TreeNode*> deletedRoots;
helperRemoveInOrder(this->root, n, deletedRoots);
if (deletedRoots.empty()){
cout << "unsuccessful\n";
} else {
cout << "successful\n";
}
return deletedRoots;
}
// verify the name of commands passed in
bool verifyName(const string& name){
// name only has alphabetic characters
regex obj = regex("^[A-Za-z\\s]+$");
return regex_search(name, obj);
}
// verify ID of command passed in
bool verifyID(const string& ID){
// ID needs to be 8 characters
if (ID.length() != 8){
return false;
}
// ID is only numeric
regex obj = regex("^[0-9]*$");
return regex_search(ID, obj);
}
// this is a test function for catch2
// should return false for any unsuccessful commands
bool correctCommand(string command, BST tree){
// read in command
cin >> command;
// user insert
if (command == "insert"){
string name;
// reading until first quote
getline(cin ,name, '"');
// now we read the name until the last quote
getline(cin, name, '"');
// name validation
if (!verifyName(name)){
cout << "unsuccessful" << endl;
return false;
}
string ID;
Student s;
getline(cin, ID, ' ');
getline(cin, ID);
// ID validation
if (!verifyID(ID)){
cout << "unsuccessful" << endl;
return false;
} else {
s.name = name, s.ID = ID;
tree.insert(name, ID);
cout << "successful" << endl;
}
}
// user printInorder
else if (command == "printInorder"){
tree.inorder();
}
// user printPreorder
else if (command == "printPreorder"){
tree.preorder();
}
// user printPostorder
else if (command == "printPostorder"){
tree.postorder();
}
// user remove
else if (command == "remove"){
string ID;
string next;
getline(cin, next, ' ');
getline(cin, ID);
if (!verifyID(ID)){
cout << "unsuccessful" << endl;
return false;
} else {
cout << "successful" << endl;
tree.removeID(ID);
}
}
// user printLevelCount
else if (command == "printLevelCount"){
tree.printLevelCount();
cout << endl;
}
// user removeInorder
else if (command == "removeInorder"){
int n;
string next;
getline(cin, next, ' ');
cin >> n;
tree.removeInOrder(n);
}
// user searchID or search NAME
else if (command == "search") {
// skip the space after the command
cin.ignore(1, ' ');
// if next character is a quote, we know it's a name
if (cin.peek() == '"') {
// skip quote
cin.ignore();
// Read the name until the next quote
string name;
getline(cin, name, '"');
// Attempt to search by name
if (!verifyName(name)) {
cout << "unsuccessful" << endl;
} else {
tree.searchName(name);
}
} else {
// Read the ID otherwise
string ID;
getline(cin, ID);
// Attempt to search by ID
if (!verifyID(ID)) {
cout << "unsuccessful" << endl;
} else {
tree.searchID(ID);
}
}
}
// wrong command
else {
cout << "unsuccessful." << endl;
return false;
}
return true;
}
int main(){
BST tree;
// iterate for # of lines
// read the entire line
// find position of first space: from beginning of line to space is the first command
// name: first quote to last quote
// UFID: last space to the end
int lines;
cin >> lines;
for (int i = 0; i < lines; i++){
// read in command
string command;
cin >> command;
// user insert
if (command == "insert"){
string name;
// vector of IDs to keep
// reading until first quote
getline(cin ,name, '"');
// now we read the name until the last quote
getline(cin, name, '"');
// name validation
string ID;
Student s;
getline(cin, ID, ' ');
getline(cin, ID);
// ID validation
if (!verifyID(ID) || !verifyName(name)){
cout << "unsuccessful" << endl;
continue;
} else {
s.name = name, s.ID = ID;
tree.insert(name, ID);
// cout << "successful" << endl;
}
}
// user printInorder
else if (command == "printInorder"){
tree.inorder();
}
// user printPreorder
else if (command == "printPreorder"){
tree.preorder();
}
// user printPostorder
else if (command == "printPostorder"){
tree.postorder();
}
// user remove
else if (command == "remove"){
string ID;
string next;
getline(cin, next, ' ');
getline(cin, ID);
// check ID input
if (!verifyID(ID)){
cout << "unsuccessful" << endl;
} else {
tree.removeID(ID);
}
}
// user printLevelCount
else if (command == "printLevelCount"){
tree.printLevelCount();
cout << endl;
}
// user removeInorder
else if (command == "removeInorder"){
int n;
string next;
getline(cin, next, ' ');
cin >> n;
tree.removeInOrder(n);
}
// user searchID or search NAME
else if (command == "search") {
// skip the space after the command
cin.ignore(1, ' ');
// if next character is a quote, we know it's a name
if (cin.peek() == '"') {
// skip quote
cin.ignore();
// read the name until the next quote
string name;
getline(cin, name, '"');
// check name validation
if (!verifyName(name)) {
cout << "unsuccessful" << endl;
} else {
tree.searchName(name);
}
} else {
// otherwise read ID
string ID;
getline(cin, ID);
// input ID validation
if (!verifyID(ID)) {
cout << "unsuccessful" << endl;
} else {
tree.searchID(ID);
}
}
}
// wrong/misspelled command
else {
// ignoring 1000 characters to reset the command if it's unsuccessful
cin.ignore(1000, '\n');
cout << "unsuccessful" << endl;
}
}
}