-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
126 lines (117 loc) · 3.93 KB
/
main.cpp
File metadata and controls
126 lines (117 loc) · 3.93 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
#include <iostream>
#include <string>
#include <sstream>
#include <queue>
#include "AVLTree.h"
using namespace std;
int main() {
//defining variables
string command = "";
string name = "";
string line = "";
string ufid = "";
string ufidOrName;
int ufidInt = 0;
int numcommands = 0;
string n = "";
int nInt = 0;
vector < string > names;
AVLTree tree;
//taking in number of commands
cin >> numcommands;
//for loop to take in commands
for (int i = 0; i < numcommands; i++) {
cin >> command;
//insert function
if (command == "insert") {
//taking input for insert function, gets name and ufid
string space = "";
getline(cin, line);
istringstream in (line);
getline(in, space, '"');
getline(in, name, '"');
in >> ufid;
//if name and ufid are valid, insert into tree
if (tree.checkName(name) && tree.checkUFID(ufid) != -1) {
ufidInt = tree.checkUFID(ufid);
//if the ufid is already in the tree, print unsuccessful, else insert it into tree
if (tree.search(tree.getRoot(), ufidInt) != NULL) {
cout << "unsuccessful" << endl;
} else {
tree.setRoot((tree.insert(tree.getRoot(), name, ufidInt)));
}
} else {
cout << "unsuccessful" << endl;
}
//search function
} else if (command == "search") {
cin >> ufidOrName;
//finding out if UFID or Name
if (tree.checkUFID(ufidOrName) != -1) {
//ufid is an int
ufidInt = tree.checkUFID(ufidOrName);
//if ufid is found in tree, print name, else print unsuccessful
if (tree.search(tree.getRoot(), ufidInt) != NULL) {
cout << tree.search(tree.getRoot(), ufidInt) -> name << endl;
} else {
cout << "unsuccessful" << endl;
}
//if ufid is a name, search for the name in the tree and store results in vector names
} else if (tree.checkName(ufidOrName.substr(1, ufidOrName.length() - 2))) {
tree.searchName(tree.getRoot(), ufidOrName.substr(1, ufidOrName.length() - 2));
}
//if command is print inorder, preorder, or postorder, call the respective function and print the vector, then clear the vector
} else if (command == "printInorder") {
tree.printInorder(tree.getRoot());
} else if (command == "printPreorder") {
tree.printPreorder(tree.getRoot());
} else if (command == "printPostorder") {
tree.printPostorder(tree.getRoot());
//printing the level count of the tree
} else if (command == "printLevelCount") {
tree.printLevelCount(tree.getRoot());
//remove function
} else if (command == "remove") {
//taking in ufid
cin >> ufid;
//if the ufid exists in the tree, remove it, else print unsuccessful
if (tree.checkUFID(ufid) != -1) {
ufidInt = tree.checkUFID(ufid);
if (tree.search(tree.getRoot(), ufidInt) == NULL) {
cout << "unsuccessful" << endl;
} else {
//setting root to the new root after removing the node
tree.setRoot(tree.remove(tree.getRoot(), ufidInt));
cout << "successful" << endl;
}
} else {
cout << "unsuccessful" << endl;
}
} else if (command == "removeInorder") {
//taking in the element number to remove, n
cin >> n;
//if the tree has a root
if (tree.getRoot() != NULL) {
//if the element number is valid, remove the node, else print unsuccessful
if (tree.checkInorder(n)) {
nInt = stoi(n);
tree.setRoot(tree.removeInorder(tree.getRoot(), nInt));
} else
cout << "unsuccessful" << endl;
} else {
cout << "unsuccessful" << endl;
}
} else {
getline(cin, line);
//if the command is not valid, print unsuccessful
cout << "unsuccessful" << endl;
}
//resetting values
ufidOrName = "";
name = "";
command = "";
ufid = "";
ufidInt = 0;
n = "";
}
}