-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVL.cpp
More file actions
198 lines (165 loc) · 6.18 KB
/
AVL.cpp
File metadata and controls
198 lines (165 loc) · 6.18 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
//
// Created by Tucker Zischka on 5/28/2018.
//
#include "AVL.h"
#include <stdlib.h>
#include <iostream>
#include "json.hpp"
#include <fstream>
#include <string>
int AVLTree :: height(BST &BSTTree){
int currentHeight = -1;
height(currentHeight, BSTTree.root());
}
int AVLTree :: height(int ¤tHeight, std::shared_ptr<BSTNode> currentNode){
if(currentNode->IsLeaf()) {
return currentHeight + 1;
}
int rightHeight = 0;
int leftHeight = 0;
//Left Height
if(currentNode->HasLeftChild()) {
/***********Left Node **************/
leftHeight = height(currentHeight, currentNode->getLeft());
}
//Right Height
if(currentNode->HasRightChild()) {
/************Right Node*************/
rightHeight = height(currentHeight, currentNode->getRight());
}
currentHeight = (rightHeight<leftHeight)?leftHeight:rightHeight;
currentNode->setHeight(currentHeight);
return currentHeight + 1;
}
void AVLTree ::balanceFactor(BST &BSTTree) {
balanceFactor(BSTTree.root());
}
/*========================BF = RightHeight - LeftHeight=========================*/
void AVLTree ::balanceFactor(std::shared_ptr<BSTNode> currentNode) {
if(currentNode->IsLeaf()){
currentNode->setBalance(0);
}
/*************Left Branch**************/
if(currentNode->HasLeftChild()){
balanceFactor(currentNode->getLeft());
}
/*************Right Branch*************/
if(currentNode->HasRightChild()){
balanceFactor(currentNode->getRight());
}
if (currentNode->HasLeftChild() && currentNode->HasRightChild()){
currentNode->setBalance(currentNode->getRight()->getHeight() - currentNode->getLeft()->getHeight());
}
else if (currentNode->HasRightChild()){
currentNode->setBalance(currentNode->getRight()->getHeight());
}
else if(currentNode->HasLeftChild()){
currentNode->setBalance(currentNode->getLeft()->getHeight());
}
/*=====================Balancing Code =====================*/
if (currentNode->getBalance() > 1) { // Greater than 1 (Right heavy)
if( currentNode->getRight()->getBalance()) { //right heavy one (R-R)
AVLTree::RRBalance(currentNode);
}
if (currentNode->getLeft()->getBalance()) { //left heavy one (R-L)
AVLTree::RLBalance(currentNode);
}
}
if (currentNode->getBalance() < -1) { //Less than 1 (Left heavy)
if( currentNode->getRight()->getBalance()) { //right heavy one (L-R)
AVLTree::LRBalance(currentNode);
}
if (currentNode->getLeft()->getBalance()) { //left heavy one (L-L)
AVLTree::LLBalance(currentNode);
}
} else {
//Nothing because it only needs to work when bf is over |1|
}
}
void AVLTree ::RRBalance(std::shared_ptr<BSTNode> currentNode) { //Left rotation
AVLTree::leftRotation(currentNode->getRight());
}
void AVLTree ::RLBalance(std::shared_ptr<BSTNode> currentNode) { //Right roation than left rotation
AVLTree::rightRotation(currentNode->getRight()->getLeft());
AVLTree::leftRotation(currentNode->getRight());
}
void AVLTree ::LLBalance(std::shared_ptr<BSTNode> currentNode) { //right rotation
AVLTree::rightRotation(currentNode->getLeft());
}
void AVLTree ::LRBalance(std::shared_ptr<BSTNode> currentNode) { //Left rotation than Right rotation
AVLTree::leftRotation(currentNode->getLeft()->getRight());
AVLTree::rightRotation(currentNode->getLeft());
}
/* Follow this to get a sense of the rotations that we need
* http://images-mediawiki-sites.thefullwiki.org/05/2/2/0/54834251380407153.gif
*/
void AVLTree::rightRotation(std::shared_ptr<BSTNode> currentNode){
std::shared_ptr<BSTNode> pivot = currentNode;
std::shared_ptr<BSTNode> root = currentNode->getParent().lock();
//check if any child nodes exist
if(pivot->HasRightChild()){
//roots left child equal to pivots right child
root->ReplaceChild(pivot, pivot->getRight());
}
//set pivot's right child to root
pivot->ReplaceChild(pivot->getRight(), root);
//set pivot's new parent
pivot->setParent(root->getParent());
//Set pivots new parent's child to pivot
std::shared_ptr<BSTNode> parent = pivot->getParent().lock();
parent->ReplaceChild(root, pivot);
//Set roots parent to pivot
root->setParent(pivot);
}
void AVLTree::leftRotation(std::shared_ptr<BSTNode> currentNode) {
std::shared_ptr<BSTNode> pivot = currentNode;
std::shared_ptr<BSTNode> root = currentNode->getParent().lock();
//check if any child nodes exist
if(pivot->HasLeftChild()){
//roots right child equal to pivots left child
root->ReplaceChild(pivot, pivot->getLeft());
}
//set pivot's left child to root
pivot->ReplaceChild(pivot->getLeft(), root);
//set pivot's new parent
pivot->setParent(root->getParent());
//Set pivots new parent's child to pivot
std::shared_ptr<BSTNode> parent = pivot->getParent().lock();
parent->ReplaceChild(root, pivot);
//Set roots parent to pivot
root->setParent(pivot);
}
void AVLTree :: inputFunc(std::string arg1){
std::ifstream file;
file.open(arg1); //make sure to use ./../filename.json as the input. The .'s and /'s are nessacary
nlohmann::json jsonObject;
if (file.is_open()) {
file >> jsonObject;
}
auto itr = jsonObject.begin();
BST AVLTree;
int numOperations = 0;
while(jsonObject[itr.key()] != "metadata"){
std::string command = jsonObject[itr.key()]["operation"];
if(command == "Insert") {
AVLTree.Insert(jsonObject[itr.key()]["key"]);
AVLTree::height(AVLTree);
AVLTree::balanceFactor(AVLTree);
AVLTree::height(AVLTree);
} else if (command == "Delete") {
AVLTree.Delete(jsonObject[itr.key()]["key"]);
AVLTree::height(AVLTree);
AVLTree::balanceFactor(AVLTree);
AVLTree::height(AVLTree);
} else if (command == "DeleteMin"){
AVLTree.DeleteMin();
AVLTree::height(AVLTree);
AVLTree::balanceFactor(AVLTree);
AVLTree::height(AVLTree);
}
++numOperations;
++itr;
}
}
void AVLTree :: output() {
}