-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
815 lines (661 loc) · 21.5 KB
/
main.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
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
#include <iostream>
#include <string>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <regex>
/* Note:
1. You will have to comment main() when unit testing your code because catch uses its own main().
2. You will submit this main.cpp file and any header files you have on Gradescope.
*/
using namespace std;
struct Student
{
string id;
string name;
//Aman said we can only use one direction
Student* left;
Student* right;
int height;
int balancefactor;
//Constructor , initlaize first two things as they are needed and the other things will be adjusted
Student(string sid, std::string& sname) : id(sid), name(sname), height(0), left(nullptr), right(nullptr) {}
//Get Height
int getHeight(Student* node)
{
if (node == nullptr)
{
return 0;
}
// Adding Plusing one as height is 1 based and recurisvely going throyugh left and right node's until a max value is reached(nullptr or leaf node)
return 1 + max(getHeight(node->left), getHeight(node->right));
}
};
class AVLTree
{
public:
//Constructor
AVLTree() : root(nullptr) {}
//Destrcutor
~AVLTree()
{
//root = deleteTree(root);
}
Student* Insert(string name, string ufid)
{
//IF search cannot find a duplicate proceed
//Note: this-=>root is the root that it was called on
this->root = insertHelper(root, name, ufid);
//If found and duplicate exist
//cout << "unsuccesful" << endl;
return root;
}
//Attribute
Student* root;
private:
//METHODS AND HELPER METHODS THAT CAN ONLY BE CALLED THROUGH PUBLIC METHODS
Student* insertHelper(Student* root, string name, string ufid)
{
if (root == nullptr)
{
root = new Student(ufid, name);
}
//ACCOUNTING FOR NEW INSERT CASE
else if (root->id == "")
{
root = new Student(ufid, name);
}
else if (stoi(ufid) > stoi(root->id))
{
root->right = insertHelper(root->right, name, ufid);
}
else if (stoi(ufid) < stoi(root->id))
{
root->left = insertHelper(root->left, name, ufid);
}
else
{
cout << "unsuccessful" << endl;
}
//Call Balance function finally
root->balancefactor = getBalanceFactor(root);
// Get balance of node inserted and do approriate rotation(not yet we don't have the functions)
//Rotation Cases FOR RR LL RL LR
if (root->balancefactor > 1 && ufid < root->left->id)
return rightRotation(root);
else if (root->balancefactor < -1 && ufid > root->right->id)
return leftRotation(root);
else if (root->balancefactor > 1 && ufid > root->left->id)
return leftRightRotation(root);
else if (root->balancefactor < -1 && ufid < root->right->id)
return rightLeftRotation(root);
return root;
}
void deleteTree(Student* root)
{
//If root already doesn't exist return nothing
if (root == NULL)
{
return;
}
//If it does, clear both subtrees, and then delete main node and then returm null
else
{
//Going through each nodes right and left and deleting it if a value is presetn and returning nullptr if its not
deleteTree(root->left);
deleteTree(root->right);
delete root;
}
//Return nullptr to indicate everything is gone;
}
//Rotations
Student* leftRotation(Student* root)
{
//Creawted Temporary Variables
Student* Parent = root->right;
Student* grandchild = root->right->left;
//Swapped Places/Rotation
Parent->left = root;
root->right = grandchild;
//Return New node/ Parent
return Parent;
}
Student* rightRotation(Student* root)
{
//Creawted Temporary Variables
Student* Parent = root->left;
Student* grandchild = root->left->right;
//Swapped Places/Rotation
Parent->right = root;
root->left = grandchild;
//Return New node/ Parent
return Parent;
}
Student* rightLeftRotation(Student* root)
{
// Perform right rotation on the node's right child
root->right = rightRotation(root->right);
// Perform left rotation on the node
return leftRotation(root);
}
Student* leftRightRotation(Student* root)
{
// Perform right rotation on the node's right child
root->left = leftRotation(root->left);
// Perform left rotation on the node
return rightRotation(root);
}
//Get Balance Factor(Subtract Left Depth - Right Depth)
int getBalanceFactor(Student* root)
{
if (root == nullptr)
{
return 0;
}
int leftheight = root->left->getHeight(root->left);
int rightheight = root->right->getHeight(root->right);
return (leftheight - rightheight);
}
};
void printInOrder(Student* root, vector<string>& names)
{
//Left Root Right
if (root == nullptr)
{
//Ends exectuion of a function which is useful for going stopping at a certain pouint in recursion whena criteria is met
return;
}
//Go through all left nodes until nullptr is reached then return 0
printInOrder(root->left, names);
//Once 0 is reached push it back into vector to record data to store it in the machine later
names.push_back(root->name);
//Iteratre through all right nodes in case they have any left nullptrs that can be returned
printInOrder(root->right, names);
}
void printPreOrderForNodeList(Student* root, vector<Student*>& sortedstudents)
{
//Left Root Right
if (root == nullptr)
{
//Ends exectuion of a function which is useful for going stopping at a certain pouint in recursion whena criteria is met
return;
}
//Once 0 is reached push it back into vector to record data to store it in the machine later
sortedstudents.push_back(root);
//Go through all left nodes until nullptr is reached then return 0
printPreOrderForNodeList(root->left, sortedstudents);
//Iteratre through all right nodes in case they have any left nullptrs that can be returned
printPreOrderForNodeList(root->right, sortedstudents);
}
vector<Student*> StudentSearchName(Student* root, string name)
{
vector<Student*> orderedlist;
vector<Student*> matchedorderedlist;
//If root is empty return empty pointer which should be cancelled in main
if (root == nullptr)
{
cout << "unsuccessful" << endl;
return orderedlist;
}
//Create vector with a preorder nodes
printPreOrderForNodeList(root, orderedlist);
//Iteratre through list and pushback on a empty vector if paramter name is matched with one in preorderedlist vector
for (unsigned int i = 0; i < orderedlist.size(); i++)
{
if (orderedlist[i]->name == name)
{
matchedorderedlist.push_back(orderedlist[i]);
}
}
if (matchedorderedlist.size() == 0)
{
cout << "unsuccessful" << endl;
}
return matchedorderedlist;
}
Student* StudentSearchID(Student* root, int id)
{
if (root == nullptr)
{
cout << "unsuccessful" << endl;
return nullptr;
}
else if (stoi(root->id) == id)
{
return root;
}
else if (stoi(root->id) < id)
{
StudentSearchID(root->right, id);
}
else if (stoi(root->id) > id)
{
StudentSearchID(root->left, id);
}
}
Student* StudentSearchIDForInsert(Student* root, int id)
{
if (root == nullptr)
{
return nullptr;
}
else if (stoi(root->id) == id)
{
return root;
}
else if (stoi(root->id) < id)
{
StudentSearchID(root->right, id);
}
else if (stoi(root->id) > id)
{
StudentSearchID(root->left, id);
}
}
int PrintLevelCount(Student* root)
{
if (root == nullptr)
{
return 0;
}
int height = root->getHeight(root);
return height;
}
//Inorder sucessor is the smallest value in the roots right subtree
Student* inorderSucessorGivenRightNode(Student* root)
{
//Creating temp var(changed)
if (root == nullptr)
{
return nullptr;
}
else
{
Student* inorderSucessorGivenRightNode = root;
while (root->left != nullptr)
{
inorderSucessorGivenRightNode = inorderSucessorGivenRightNode->left;
}
return inorderSucessorGivenRightNode;
}
}
Student* removeID(string id, Student* root)
{
//If root is empty
if (root == nullptr)
{
return nullptr;
}
//Must do a mini search in id's here to get recursion information
if (stoi(id) > stoi(root->id))
{
root->right = removeID(id, root->right);
}
else if (stoi(id) < stoi(root->id))
{
root->left = removeID(id, root->left);
}
else if (root->id == id) //IF ID EQUAL AND NOT NULLPTR
{
//cout << "ID: " << root->id << endl;
//REBALANCE WITH 0 children
if (root->right == nullptr && root->left == nullptr)
{
delete root;
root = nullptr;
return root;
}
//REBALANCE WITH Left CHILD
else if (root->left != nullptr && root->right == nullptr)
{
//Swap Children with Parent
Student* temp = root;
root = root->left;
delete temp;
return root;
}
//REBALANCE WITH Right CHILD
else if (root->right != nullptr && root->left == nullptr)
{
//Swap Children with Parent
Student* temp = root;
root = root->right;
delete temp;
return root;
}
//REBALANCE WITH TWO CHILDREN
else if (root->left != nullptr && root->right != nullptr)
{
//Swap Children with Parent
//CALL Inordersucessor with root right so all we need to do is move left for the function
//WHY DID I GET A READ ACCESS ERROR WITH JUST ROOT
Student* temp = inorderSucessorGivenRightNode(root->right);
//Give root properties of root
root->name = temp->name;
root->id = temp->id;
//Remove root right by starting the whole process(maybe wrong)
root->right = removeID(root->id, root->right);
return root;
}
}
return root;
}
void printInOrderforNodeList(Student* root, vector<Student*>& sortedstudents)
{
//Left Root Right
if (root == nullptr)
{
//Ends exectuion of a function which is useful for going stopping at a certain pouint in recursion whena criteria is met
return;
}
//Go through all left nodes until nullptr is reached then return 0
printInOrderforNodeList(root->left, sortedstudents);
//Once 0 is reached push it back into vector to record data to store it in the machine later
sortedstudents.push_back(root);
//Iteratre through all right nodes in case they have any left nullptrs that can be returned
printInOrderforNodeList(root->right, sortedstudents);
}
void printPreOrder(Student* root, vector<string>& names)
{
//Left Root Right
if (root == nullptr)
{
//Ends exectuion of a function which is useful for going stopping at a certain pouint in recursion whena criteria is met
return;
}
//Once 0 is reached push it back into vector to record data to store it in the machine later
names.push_back(root->name);
//Go through all left nodes until nullptr is reached then return 0
printPreOrder(root->left, names);
//Iteratre through all right nodes in case they have any left nullptrs that can be returned
printPreOrder(root->right, names);
}
void printPostOrder(Student* root, vector<string>& names)
{
//Left Root Right
if (root == nullptr)
{
//Ends exectuion of a function which is useful for going stopping at a certain pouint in recursion whena criteria is met
return;
}
//Go through all left nodes until nullptr is reached then return 0
printPostOrder(root->left, names);
//Iteratre through all right nodes in case they have any left nullptrs that can be returned
printPostOrder(root->right, names);
//Once 0 is reached push it back into vector to record data to store it in the machine later
names.push_back(root->name);
}
void removeInOrder(Student*& root, int removalindex)
{
if (root == nullptr)
{
cout << "unsuccessful" << endl;
return;
}
//Calliling Function that has everything sorted and now N will become an index we can just delted with no rebalancing
vector<Student*> sortedstudents;
printInOrderforNodeList(root, sortedstudents);
if (removalindex < 0 || removalindex > sortedstudents.size())
{
cout << "unsuccessful" << endl;
return;
}
Student* removedstudent = sortedstudents[removalindex];
//REMOVE FUNCTION
root = removeID(removedstudent->id, root);
if (root != nullptr)
{
cout << "successful" << endl;
}
else
{
cout << "unsuccessful" << endl;
}
}
bool IDexist(Student* root, string ufid)
{
//Checking if root emtpy
if (root == nullptr) {
return false;
}
//Checking if root is equal to ufid
if (root->id == ufid) {
return true;
}
//Getting Bool blaue if the root's left nodes have the ufid
if (IDexist(root->left, ufid) == true)
{
return true;
}
if (IDexist(root->right, ufid) == true)
{
return true;
}
else
{
return false;
}
}
//Student ParseStringtree.root = tree.Insert(tree.root, "andrew", "01909393")
bool VerifyID(string id, Student* root)
{
//Checking length
if (id.length() != 8)
return false;
//Maing sure each charcer is conistent a-z, A-Z, or 0-9
for (char c : id)
{
if (!isdigit(c))
{
return false;
}
}
return true;
}
bool VerifyString(string name)
{
//Maing sure each charcer is conistent a-z, A-Z, or 0-9
for (char c : name)
{
//IF THE CHAR CONTAINS A SPACE SKIP IT
if (c == ' ')
{
continue;
}
//Isdigitlogic is checking for no nums
if (isdigit(c))
{
return false;
}
if (!isalpha(c))
{
return false;
}
}
return true;
}
int main()
{
bool start = true;
AVLTree tree;
string line;
getline(cin, line);
//While i doesn't equal the amount of commands continue
for (int i = 0; i != stoi(line); i++)
{
//Reading Inputs from files for rapid testing
// string filenum;
//cin >> filenum;
//FileDataToString(filenum);
//Store inputline response as a string variable
string line;
getline(cin, line);
//Making string from ENTIRE LINE
istringstream in(line);
//Reading first word in input string
string command;
in >> command;
if (command == "insert")
{
//Read in between quotation marks
string name = "";
getline(in, name, '"');
getline(in, name, '"');
//Reading in btween empty space of id
string id;
getline(in, id, ' ');
getline(in, id, ' ');
//Check if id and String chars are approriate
if (VerifyID(id, tree.root) == true && VerifyString(name) == true && IDexist(tree.root, id) == false)
{
//Exceute code based on commands
tree.Insert(name, id);
cout << "successful" << endl;
}
else
{
cout << "unsuccessful" << endl;
}
}
else if (command == "search")
{
//Reading in btween empty space of id
string idorsearch;
getline(in, idorsearch, ' ');
getline(in, idorsearch, ' ');
//Check if it's num or string
if (VerifyID(idorsearch, tree.root) == true)
{
Student* idstudent;
idstudent = StudentSearchID(tree.root, stoi(idorsearch));
if (idstudent != nullptr)
{
cout << idstudent->name << endl;
// cout << "successful2 " << endl;
}
}
//IT WASNT EVEN BEING CALLED IDIOT ELI YOU ARE AN IDIOT -------------------------------------------------------
else
{
//Remove quotation marks
idorsearch.erase(0, 1);
idorsearch.erase(idorsearch.length() - 1, idorsearch.length());
if (VerifyString(idorsearch) == true)
{
vector<Student*> foundstudentbyname = StudentSearchName(tree.root, idorsearch);
if (foundstudentbyname.size() > 0)
{
//HAD TO FLIP ORDER, WHEN YOU PUSHBACK INTO A FUNCTION IT FLIPS IT SINCE IT STORES LIKE A STACK SO I SUBTRACTED THE INDEX FROM SIZE OF ID(SIZE-1) OF STORAGE TO GO INREVERSE
for (unsigned int i = 0; i < foundstudentbyname.size(); i++)
{
cout << foundstudentbyname[i]->id << endl;
}
}
}
//If that's its not the string either it's no valid
else
{
cout << "unsuccessful" << endl;
}
}
}
else if (command == "printInorder")
{
vector<string> names;
printInOrder(tree.root, names);
for (unsigned int i = 0; i < names.size(); i++)
{
if (i == (int(names.size() - 1)))
{
cout << names[i] << endl;
}
else
{
cout << names[i] << ", ";
}
}
}
else if (command == "printPreorder")
{
vector<string> names;
printPreOrder(tree.root, names);
for (unsigned int i = 0; i < names.size(); i++)
{
if (i == (int(names.size()) - 1))
{
cout << names[i] << endl;
}
else
{
cout << names[i] << ", ";
}
}
}
else if (command == "printPostorder")
{
vector<string> names;
printPostOrder(tree.root, names);
for (unsigned int i = 0; i < names.size(); i++)
{
if (i == (int(names.size() - 1)))
{
cout << names[i] << endl;
}
else
{
cout << names[i] + ", ";
}
}
}
else if (command == "remove")
{
//Reading in between empty space of id
string idorsearch;
getline(in, idorsearch, ' ');
getline(in, idorsearch, ' ');
//Recording size of tree before without making it an attribute
vector<string> traversal;
printInOrder(tree.root, traversal);
int initalsize = traversal.size();
if (tree.root == nullptr)
{
cout << "unsuccessful" << endl;
}
else
{
tree.root = removeID(idorsearch, tree.root);
//Recording size of tree after without making it an attribute
vector<string> traversal2;
printInOrder(tree.root, traversal2);
int finalsize = traversal2.size();
//Comparing and seeing whether or not it was a success
if (finalsize != initalsize)
{
cout << "successful" << endl;
}
else
{
cout << "unsuccessful" << endl;
}
}
}
else if (command == "printLevelCount")
{
int levelcount = PrintLevelCount(tree.root);
cout << levelcount << endl;
}
else if (command == "removeInorder")
{
//Reading in btween empty space of id
string N;
getline(in, N, ' ');
getline(in, N, ' ');
removeInOrder(tree.root, stoi(N));
}
else
{
cout << "unsuccessful" << endl;
}
}
}