-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVL.cpp
226 lines (198 loc) · 5.5 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
#include "AVL.h"
using namespace std;
AVL::AVL() {
root = NULL;
}
AVL::~AVL() {
clear();
root = NULL;
}
/*
* Returns the root node for this tree
*
* @return the root node for this tree.
*/
Node* AVL::getRootNode(){
return root;
}
/*
* Attempts to add the given int to the AVL tree
*
* @return true if added
* @return false if unsuccessful (i.e. the int is already in tree)
*/
bool AVL::add(int data){
bool was_added = false;
if (root == NULL){
root = new Node;
root->data = data;
was_added = true;
} else {
if (contains(data, root) == NULL){
root = recursiveAdd(data, root);
was_added = true;
} else {
was_added = false;
}
}
return was_added;
}
Node* AVL::recursiveAdd(int data, Node* current_search){
if (current_search == NULL){
Node* location_added = new Node;
location_added->data = data;
return balance(location_added);
}
if (data > current_search->data){
current_search->right_child = recursiveAdd(data, current_search->right_child);
} else {
current_search->left_child = recursiveAdd(data, current_search->left_child);
}
return balance(current_search);
}
Node* AVL::contains(int data, Node* current_search){
if (current_search == NULL){return NULL;}
if (current_search->data == data){
return current_search;
} else if (current_search->data < data){ // && current_search->right_child != NULL
return contains(data, current_search->right_child);
} else if (current_search->data > data){
return contains(data, current_search->left_child);
}
return current_search;
}
void AVL::print(Node* current_print){
if (current_print == NULL){return;}
cout << current_print->data << " | height = " << current_print->height << " | left child = ";
if(current_print->left_child == NULL){
cout << "NULL ";
} else {
cout << current_print->left_child->data;
}
cout << " and right_child child = ";
if(current_print->right_child == NULL){
cout << "NULL";
} else {
cout << current_print->right_child->data;
}
cout << endl;
print(current_print->right_child);
print(current_print->left_child);
}
/*
* Attempts to remove the given int from the AVL tree
*
* @return true if successfully removed
* @return false if remove is unsuccessful(i.e. the int is not in the tree)
*/
bool AVL::remove(int data){
bool was_removed = false;
if (root == NULL){
// DO NOTHING
} else {
Node* checker = contains(data, root);
if (checker != NULL && (checker->data == data)){
root = recursiveRemove(data, root);
was_removed = true;
} else {
// DO NOTHING
}
}
return was_removed;
}
Node* AVL::recursiveRemove(int data, Node* current_search){
if (current_search == NULL){
return NULL;
} else if (current_search->data < data){
current_search->right_child = recursiveRemove(data, current_search->right_child);
} else if (current_search->data > data){
current_search->left_child = recursiveRemove(data, current_search->left_child);
} else {
if (current_search->left_child == NULL){
Node* temp = current_search->right_child;
delete current_search;
return temp;
} else {
current_search->left_child = IOP(current_search->left_child, current_search);
return balance(current_search);
}
}
return balance(current_search);
}
Node* AVL::IOP(Node* current, Node* node_to_remove){
if (current->right_child == NULL){}
if (current->right_child != NULL){
current->right_child = IOP(current->right_child, node_to_remove);
return balance(current);
} else {
node_to_remove->data = current->data;
Node* temp = current->left_child;
delete current;
return temp;
}
}
/*
* Removes all nodes from the tree, resulting in an empty tree.
*/
void AVL::clear(){
while (root != NULL){
remove(root->data);
}
}
// AVL FUNCTIONS
int AVL::height(Node* node_query){
return node_query ? node_query->height : 0;
}
void AVL::fixHeight(Node* parent){
int hl = height(parent->left_child);
int hr = height(parent->right_child);
parent->height = (hl>hr ? hl : hr) + 1;
}
int AVL::heightDiff(Node* parent){
//cout << parent->data << " | " << parent->height << " = parent height | height right = " << height(parent->right_child) << " | height left = " << height(parent->left_child) << endl;
return height(parent->right_child) - height(parent->left_child);
}
Node* AVL::balance(Node* parent){
fixHeight(parent);
int heightDifference = heightDiff(parent);
if (heightDifference == 2){
//cout << "heightDiff == 2" << endl;
if (heightDiff(parent->right_child) < 0)
parent->right_child = rotateRight(parent->right_child);
return rotateLeft(parent);
} else if (heightDifference == -2){
//cout << "heightDiff == -2" << endl;
if (heightDiff(parent->left_child) > 0)
parent->left_child = rotateLeft(parent->left_child);
return rotateRight(parent);
} else {
//cout << "heightDifference = " << heightDifference << " thus no balance needed" << endl;
return parent; // no balance needed
}
}
Node* AVL::rotateRight(Node* pivot){
Node* pivot2 = pivot->left_child;
pivot->left_child = pivot2->right_child;
pivot2->right_child = pivot;
fixHeight(pivot);
fixHeight(pivot2);
return pivot2;
}
Node* AVL::rotateLeft(Node* pivot2){
Node* pivot = pivot2->right_child;
pivot2->right_child = pivot->left_child;
pivot->left_child = pivot2;
fixHeight(pivot2);
fixHeight(pivot);
return pivot;
}
Node* AVL::getMin (Node* parent){
return parent->left_child?getMin(parent->left_child):parent;
}
Node* AVL::removeMin (Node* parent){
if (parent->left_child == 0){
return parent->right_child;
}
parent->left_child = removeMin(parent->left_child);
return balance(parent);
}