-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVL.cpp
344 lines (327 loc) · 11.4 KB
/
AVL.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
#include "AVL.h"
#include <sstream>
//Rotation Functions:
Node* AVLTree::rotateLeft(Node* rootNode)
{
//parent balance factor = -2; child balance factor = -1
Node* rightChild = rootNode->right;
rootNode->right = rightChild->left;
rightChild->left = rootNode;
return rightChild; //new root
}
Node* AVLTree::rotateRight(Node* rootNode)
{
//parent balance factor = +2; child balance factor = +1
Node* leftChild = rootNode->left;
rootNode->left = leftChild->right;
leftChild->right = rootNode;
return leftChild; //new root
}
Node* AVLTree::rotateLeftRight(Node* rootNode)
{
//parent balance factor = +2; child balance factor = -1
rootNode->left = rotateLeft(rootNode->left);
return rotateRight(rootNode);
}
Node* AVLTree::rotateRightLeft(Node* rootNode)
{
//parent balance factor = -2; child balance factor = +1
rootNode->right = rotateRight(rootNode->right);
return rotateLeft(rootNode);
}
int AVLTree::calculateHeight(Node* rootNode)
{
if (rootNode == nullptr) return 0;
return 1 + std::max(calculateHeight(rootNode->left), calculateHeight(rootNode->right));
}
int AVLTree::calculateBF(Node* rootNode)
{
int leftHeight = calculateHeight(rootNode->left);
int rightHeight = calculateHeight(rootNode->right);
return leftHeight - rightHeight;
}
bool AVLTree::insert(std::string &name, std::string &ufid)
{
//My searchID prints something, but I just want bool value so Ima suppress cout
std::streambuf* OGcoutBuffer = std::cout.rdbuf(); // original cout buffer
std::ostringstream tempStream; // cout to null (suppress output)
std::cout.rdbuf(tempStream.rdbuf()); // Output now goes to temp stream and not cout
bool foundDuplicateID = searchID(ufid); //I can call searchID without "successful/unsuccessful" printing
std::cout.rdbuf(OGcoutBuffer); // Now cout back to normal
if (foundDuplicateID){
std::cout << "unsuccessful\n";
return false;
}
root = insertHelper(root, name, ufid);
if (root != nullptr)
{
std::cout << "successful\n";
return true;
}
else
{
std::cout << "unsuccessful\n";
return false;
}
}
Node* AVLTree::insertHelper(Node* rootNode, std::string &name, std::string &ufid)
{
if (rootNode == nullptr) return new Node(name, ufid); //empty tree so inserted node becomes root
if (std::stoi(ufid) < std::stoi(rootNode->ufid))
{
//add to left subtree
rootNode->left = insertHelper(rootNode->left, name, ufid);
}
else if (std::stoi(ufid) > std::stoi(rootNode->ufid))
{
//add to right subtree
rootNode->right = insertHelper(rootNode->right, name, ufid);
}
else
{
return nullptr; //duplicate: unsuccessful
}
return balanceTree(rootNode); //return the balanced tree's root
}
Node* AVLTree::balanceTree(Node* rootNode) {
if (rootNode == nullptr) return nullptr;
int parentBF = calculateBF(rootNode);
//Left Rotation when: parentBF = -2 and rightChildBF = -1
if (parentBF == -2 && calculateBF(rootNode->right) == -1)
{
return rotateLeft(rootNode);
}
//Right Rotation when: parentBF = 2 and rightChildBF = 1
if (parentBF == 2 && calculateBF(rootNode->left) == 1)
{
return rotateRight(rootNode);
}
//LeftRight Rotation when: parentBF = 2 and leftChildBF = -1
if (parentBF == 2 && calculateBF(rootNode->left) == -1) {
return rotateLeftRight(rootNode);
}
//RightLeft Rotation when: parentBF = -2 and rightChildBF = 1
if (parentBF == -2 && calculateBF(rootNode->right) == 1){
return rotateRightLeft(rootNode);
}
return rootNode;
}
//Preorder recursion: Root, Left, Right
std::string AVLTree::printPreOrder(Node* rootNode, std::string &fullList)
{
if (rootNode == nullptr) return "";
if (!fullList.empty()) fullList += ", "; //add comma before if not first element
fullList += rootNode->name;
printPreOrder(rootNode->left, fullList);
printPreOrder(rootNode->right, fullList);
return fullList;
}
//Inorder recursion: Left, Root, Right
std::string AVLTree::printInOrder(Node* rootNode, std::string &fullList)
{
if (rootNode == nullptr) return "";
printInOrder(rootNode->left, fullList);
if (!fullList.empty()) fullList += ", "; //add comma before if not first element
fullList += rootNode->name;
printInOrder(rootNode->right, fullList);
return fullList;
}
std::string AVLTree::printInOrderIDS(Node* rootNode, std::string &fullList) //for test cases
{
if (rootNode == nullptr) return "";
printInOrder(rootNode->left, fullList);
if (!fullList.empty()) fullList += ", "; //add comma before if not first element
fullList += rootNode->ufid;
printInOrder(rootNode->right, fullList);
return fullList;
}
//Inorder recursion: Left, Root, Right
Node* AVLTree::deletePostOrder(Node *rootNode) {
if (rootNode == nullptr) return nullptr;
deletePostOrder(rootNode->left);
deletePostOrder(rootNode->right);
delete rootNode;
rootNode = nullptr;
return nullptr;
}
//Create vectors of names and id with preorder recursion
Node* AVLTree::vectorsPreOrder(Node *rootNode, std::vector<std::string> &namesVec, std::vector<std::string> &idVec) {
if (rootNode == nullptr) return nullptr;
namesVec.push_back(rootNode->name);
idVec.push_back(rootNode->ufid);
vectorsPreOrder(rootNode->left, namesVec, idVec);
vectorsPreOrder(rootNode->right, namesVec, idVec);
return nullptr;
}
//Create vectors of names and id with inorder recursion
Node* AVLTree::vectorsInOrder(Node *rootNode, std::vector<std::string> &namesVec, std::vector<std::string> &idVec) {
if (rootNode == nullptr) return nullptr;
vectorsInOrder(rootNode->left, namesVec, idVec);
namesVec.push_back(rootNode->name);
idVec.push_back(rootNode->ufid);
vectorsInOrder(rootNode->right, namesVec, idVec);
return nullptr;
}
//Postorder recursion: Left, Right, Root
std::string AVLTree::printPostOrder(Node* rootNode, std::string &fullList)
{
if (rootNode == nullptr) return "";
printPostOrder(rootNode->left, fullList);
printPostOrder(rootNode->right, fullList);
if (!fullList.empty()) fullList += ", "; //add comma before if not first element
fullList += rootNode->name;
return fullList;
}
Node* AVLTree::findNode(Node* rootNode, std::string& id)
{
//Use preorder traversal
if (rootNode == nullptr) return nullptr;
if (rootNode->ufid == id) return rootNode;
Node* foundNode = findNode(rootNode->left, id);
if (foundNode != nullptr) return foundNode;
return findNode(rootNode->right, id);
}
Node* AVLTree::findParent(Node* rootNode, Node* childNode)
{
//Use preorder traversal
if (rootNode == nullptr) return nullptr;
if ((rootNode->left != nullptr && rootNode->left->ufid == childNode->ufid) ||
(rootNode->right != nullptr && rootNode->right->ufid == childNode->ufid)){
return rootNode;
}
Node* foundParent = findParent(rootNode->left, childNode);
if (foundParent != nullptr) return foundParent;
return findParent(rootNode->right, childNode);
}
Node* AVLTree::findSuccessor(Node* rootNode)
{
if (rootNode->left == nullptr) return rootNode; //no left child
return findSuccessor(rootNode->left);
}
bool AVLTree::remove(std::string& id)
{
Node* nodeFound = findNode(root, id);
if (nodeFound == nullptr)
{
std::cout << "unsuccessful\n"; //ID doesn't exist in tree
return false;
}
//Find parent of node you want to delete
Node* parentNode = findParent(root, nodeFound);
//Deleted node has two children
if (nodeFound->left != nullptr && nodeFound->right != nullptr)
{
Node* successor = findSuccessor(nodeFound->right);
nodeFound->ufid = successor->ufid;
nodeFound->name = successor->name;
if (successor->right != nullptr) nodeFound->right = successor->right;
else nodeFound->right = nullptr;
delete successor;
successor = nullptr;
}
//Deleted node has one left child
else if (nodeFound->left != nullptr)
{
if (parentNode == nullptr) root = nodeFound->left; //cuz deleting root
else if (stoi(nodeFound->ufid) < stoi(parentNode->ufid)){
parentNode->left = nodeFound->left;
}
else if (stoi(nodeFound->ufid) > stoi(parentNode->ufid)){
parentNode->right = nodeFound->left;
}
delete nodeFound;
nodeFound = nullptr;
}
//Deleted node has one right child
else if (nodeFound->right != nullptr)
{
if (parentNode == nullptr) root = nodeFound->right; //cuz deleting root
else if (stoi(nodeFound->ufid) < stoi(parentNode->ufid)){
parentNode->left = nodeFound->right;
}
else if (stoi(nodeFound->ufid) > stoi(parentNode->ufid)){
parentNode->right = nodeFound->right;
}
delete nodeFound;
nodeFound = nullptr;
}
//Deleted node has no children
else
{
if (parentNode == nullptr) root = nullptr; //deleting only node in tree (root)
else if (stoi(nodeFound->ufid) < stoi(parentNode->ufid)){
parentNode->left = nullptr;
}
else if (stoi(nodeFound->ufid) > stoi(parentNode->ufid)){
parentNode->right = nullptr;
}
delete nodeFound;
nodeFound = nullptr;
}
root = balanceTree(root); //Optional balancing post deletion
std::cout << "successful\n";
return true;
}
//Search by ID and print NAME if found
bool AVLTree::searchID(std::string& id)
{
std::vector<std::string> namesVec;
std::vector<std::string> idVec;
vectorsPreOrder(root, namesVec, idVec);
for (int i = 0; i < (int)idVec.size(); i++)
{
if (idVec[i] == id)
{
std::cout << namesVec[i] << "\n";
return true;
}
}
std::cout << "unsuccessful\n";
return false;
}
//Search by NAME in preOrder and print IDs if found
bool AVLTree::searchName(std::string& name)
{
bool found = false;
std::vector<std::string> namesVec;
std::vector<std::string> idVec;
vectorsPreOrder(root, namesVec, idVec);
for (int i = 0; i < (int)namesVec.size(); i++)
{
if (namesVec[i] == name)
{
std::cout << idVec[i] << std::endl; //print out ID
found = true;
}
}
if (!found) std::cout << "unsuccessful\n";
return found;
}
bool AVLTree::printLevelCount()
{
if (root == nullptr){
std::cout << "0\n";
return false;
}
std::cout << calculateHeight(root) << std::endl; //height is level count cuz 1-based
return true;
}
bool AVLTree::removeInOrder(int N)
{
std::vector<std::string> namesVec;
std::vector<std::string> idVec;
vectorsInOrder(root, namesVec, idVec);
if ((int)namesVec.size() <= N){ //index is larger than number of nodes in tree
std::cout << "unsuccessful\n";
return false;
}
std::string nodeID = idVec[N];
Node* deleteNode = findNode(root, nodeID);
if (deleteNode != nullptr){
remove(deleteNode->ufid); //already prints successful
return true;
} else {
std::cout << "unsuccessful\n";
return false;
}
}