-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
474 lines (399 loc) · 13.2 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
#include <iostream>
#include <sstream>
#include <string>
#include <regex>
#include <vector>
using namespace std;
class AVLTree {
public:
struct TreeNode {
int value;
TreeNode* left;
TreeNode* right;
string name;
int height;
TreeNode(int value, string name) {
this->name = name;
this->value = value;
left = nullptr;
right = nullptr;
height = 0;
}
};
TreeNode* root;
int numNodes = 0;
AVLTree(){
root = nullptr;
}
//height function
int findHeight(TreeNode* node){
// if(node == nullptr){
// return 0; //leaf nodes height = 0
// }
// return node->height;
int maxHeight = 0;
if (node != nullptr){
maxHeight = max(findHeight(node->left), findHeight(node->right));
}
else{
return 0;
}
return maxHeight + 1;
}
//getBalance function balanced = -1,0,1
int getBalanceFact(TreeNode* node){
if(node == nullptr){
return 0;
}
else
//height of left subtree - right subtree
return (findHeight(node->left) - findHeight(node->right));
}
//rotation functions
TreeNode* rotateLeft (TreeNode* node)
{
TreeNode * newParent = node->right;
TreeNode* grandchild = node->right->left;
newParent->left = node;
node->right = grandchild;
return newParent; //aka new root
}
TreeNode* rotateRight (TreeNode* node)
{
TreeNode* newParent = node->left;
TreeNode* grandchild = node->left->right;
newParent->right = node;
node->left = grandchild;
return newParent; //aka new root
}
TreeNode* insertHelper(TreeNode* root, int value, string name){
if(root == nullptr){
TreeNode* newNode = new TreeNode(value, name);
cout << "successful" << endl;
numNodes++;
return newNode;
}
else if(value < root->value ){
root->left = insertHelper(root->left, value, name);
}
else if(value > root->value){
root->right = insertHelper(root->right, value, name);
}
else{
cout << "unsuccessful"<< endl;
return root;
}
//update height
// root->height = 1 + max(findHeight(root->left),
// findHeight(root->right));
//get balanced factor of root
int balancedFactor = getBalanceFact(root);
if(balancedFactor > 1 && value < root->left->value){ //L L
return rotateRight(root);
}
if(balancedFactor < -1 && value > root->right->value){ // R R
return rotateLeft(root);
}
if(balancedFactor > 1 && value > root->left->value) { //L R
root->left = rotateLeft(root->left);
return rotateRight(root);
}
if(balancedFactor < -1 && value < root->right->value){
root->right = rotateRight(root->right); // R L
return rotateLeft(root);
}
return root;
}
void insert(string name, int id){
if(this->root == nullptr) {
this->root = insertHelper(this->root, id, name);
}
else{
this->root = insertHelper(this->root,id, name);
}
}
TreeNode* removeHelper(TreeNode* root, int value, string name){
if(root == nullptr){
return root;
}
string idDNE = searchID(value);
if(idDNE.empty()){
cout << "unsuccessful" << endl;
}
else if(value < root->value){
root->left = removeHelper(root->left, value, name);
}
else if(value > root->value){
root->right = removeHelper(root->right, value, name);
}
else if (value == root->value){ //item is in the local root
if(root->left == nullptr && root->right == nullptr ){ //check no children
delete (root);
cout << "successful"<< endl;
numNodes--;
return nullptr;
}
else if(root->right == nullptr ){ //check if root has one child
TreeNode* temp = root->left;
delete(root);
cout << "successful" << endl;
numNodes--;
return temp;
}
else if(root->left == nullptr){
TreeNode* temp = root->right;
delete(root);
cout << "successful" << endl;
numNodes--;
return temp;
}
else { //check if root has two children: inorder successor
TreeNode* temp = root->right;
while(temp->left != nullptr){
temp = temp->left;
}
root->value = temp->value;
root->name = temp->name;
root->right = removeHelper(root->right, temp->value, temp->name);
}
}
return root;
}
void remove(string name, int id){
this->root = removeHelper(this->root, id, name);
}
void removeInorderHelper(TreeNode* root, int n, int& curr){ //remove node at nth location
if(root == nullptr){
return;
}
removeInorderHelper(root->left, n, curr);
if(curr == n){
remove(root->name, root->value);
if(n == curr){ //to ensure only removing one element
curr++;
return;
}
}
curr++;
removeInorderHelper(root->right, n, curr);
}
void removeInorder(int n){
int curr = 0;
if(n > numNodes){
cout << "unsuccessful" << endl;
}
else {
removeInorderHelper(this->root, n, curr);
}
}
string searchIDHelper(TreeNode* root, int id, bool &n, string &nameToReturn){
if(root == nullptr){
n = false;
return nameToReturn;
}
if(root->value == id) {
n = true;
nameToReturn = root->name;
return nameToReturn;
}
else if(root->value > id && n == false) {
searchIDHelper(root->left, id, n, nameToReturn);
}
else if(root->value < id && n == false) {
searchIDHelper(root->right, id, n, nameToReturn);
}
return nameToReturn;
}
string searchID(int id){
bool n = false;
string nameToReturn = "";
searchIDHelper(this->root, id, n, nameToReturn);
return nameToReturn;
}
void searchNameHelper(TreeNode* root, string name, vector<int>& duplicateNameIds){
if(root == nullptr){
return;
}
if (name == root->name) {
duplicateNameIds.push_back(root->value);
}
searchNameHelper(root->left, name, duplicateNameIds);
searchNameHelper(root->right, name, duplicateNameIds);
}
void searchName(string name, vector<int>& duplicateNameIds){
searchNameHelper(this->root, name, duplicateNameIds);
}
void inorderHelper(TreeNode* root, vector<string> &vec, bool finished){
if (root == nullptr) {
return;
}
else {
inorderHelper(root->left, vec, finished);
vec.push_back(root->name);
inorderHelper(root->right, vec, finished);
}
}
void printInorder(vector<string> &vec){
bool finished = false;
inorderHelper(this->root, vec, finished);
}
void preorderHelper(TreeNode* root, vector<string> &vec) {
if (root == nullptr) {
return;
}
else {
vec.push_back(root->name);
preorderHelper(root->left, vec);
preorderHelper(root->right, vec);
}
}
void printPreorder(vector<string> &vec){
preorderHelper(this->root, vec);
}
void postorderHelper(TreeNode* root, vector<string> &vec){
if (root == nullptr) {
return;
}
else {
postorderHelper(root->left, vec);
postorderHelper(root->right, vec);
vec.push_back(root->name);
}
}
void printPostorder(vector<string> &vec){
postorderHelper(this->root, vec);
}
void printLevelCountHelper(TreeNode* root){
if (root == nullptr){ //no height
cout << 0 << endl;
}
else {
cout << findHeight(root) << endl;
}
}
void printLevelCount(){
printLevelCountHelper(this->root);
}
};
int main() {
AVLTree t;
int numLines;
string line, command;
getline(cin, line);
numLines = stoi(line); // access to number of lines
while(numLines != 0) {
string input;
getline(cin, input);
vector<string> output; //contain all the strings -> for traversals
vector<int> duplicateNameIds; //searchName command
stringstream s_stream(input);
getline(s_stream, command, ' '); //insert
int idNum;
unsigned int finder, finder2, finder3; //parsing helpers
string name, idString;
if(command == ("insert")){
//parse to get the name ->
finder = input.find('"'); // inside " -> should be index 0
name = input.substr(finder + 1); //ex. Steven" 12345678
finder2 = name.find('"');
name = name.substr(0, finder2); // print everything before " (name)
//parse to get the idNum
idString = input.substr(finder + finder2 + 3); // print everything after " (id)
idNum = stoi(idString);
//check line to ensure a-z A-z ' '
regex nameCheck = regex("^[a-zA-Z\\s]*$");
bool isAlpha = regex_match(name, nameCheck);
//check IdNum to ensure 8 only digits
regex idCheck = regex("^[0-9]{8}$");
bool isIdNum = regex_match(idString, idCheck);
if (!isIdNum || !isAlpha) {
cout << "unsuccessful" << endl;
}
else{
t.insert(name, idNum);
}
}
else if(command == ("remove")){
finder3 = input.find(' ');
idString = input.substr(finder3 + 1); // 12345678 -> print everything after command
idNum = stoi(idString);
//check IdNum to ensure 8 only digits
regex idCheck2 = regex("^[0-9]{8}$");
bool isIdNum = regex_match(idString, idCheck2);
if (!isIdNum) {
cout << "unsuccessful" << endl;
}
t.remove(name, idNum);
}
else if(command == "removeInorder"){
finder3 = input.find(' ');
string inputString = input.substr(finder3 + 1);
int inputNum = stoi(inputString);
t.removeInorder(inputNum);
}
else if(command == "printPreorder"){
output.clear();
t.printPreorder(output);
for(int i = 0; i < output.size() - 1; i++){
cout << output[i] << ", ";
}
cout << output[output.size() - 1] << endl;
}
else if(command == "printInorder"){
output.clear();
t.printInorder(output);
for(int i = 0; i < output.size() - 1; i++){
cout << output[i] << ", ";
}
cout << output[output.size() - 1] << endl;
}
else if(command == "printPostorder"){
output.clear();
t.printPostorder(output);
for(int i = 0; i < output.size() - 1; i++){
cout << output[i] << ", ";
}
cout << output[output.size() - 1] << endl;
}
else if(command == "search") {
finder3 = input.find(' ');
string searchString = input.substr(finder3 + 1); // print everything after command
//check IdNum to ensure 8 only digits
regex idCheck = regex("^[0-9]{8}$");
bool isIdNum = regex_match(searchString, idCheck);
if (isIdNum) {
idNum = stoi(searchString);
string idName = t.searchID(idNum);
if (idName == "") {
cout << "unsuccessful" << endl;
} else {
cout << idName << endl;
}
}
finder = input.find('"');
name = input.substr(finder + 1);
finder2 = name.find('"');
name = name.substr(0, finder2);
//check input to ensure a-z A-z ' '
regex nameCheck = regex("^[a-zA-Z\\s]*$") ;
bool isAlpha = regex_match(name, nameCheck);
if(isAlpha){
t.searchName(name, duplicateNameIds);
if(duplicateNameIds.size() == 0){
cout << "unsuccessful" << endl;
}
else {
for (int i = 0; i < duplicateNameIds.size(); i++) {
cout << duplicateNameIds[i] << endl;
}
}
}
}
else if(command == "printLevelCount"){
t.printLevelCount();
}
//decrement after operation complete
numLines--;
}
return 0;
}