-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAVLTree.cpp
234 lines (210 loc) · 5.27 KB
/
AVLTree.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
#include "AVLTree.h"
#include <assert.h>
//implementation
std::vector<Node*>* AVLTree::getPath(int item) const
{
Node *currNode = this->root;
if(!currNode)
return NULL;
// #warning do not forget to delete vector to which pathElements points in order to release allocated memory
std::vector<Node*> *pathElements = new std::vector<Node*>;
while(currNode->item != item && currNode != NULL)
{
//
pathElements->push_back(currNode);
if(currNode->item > item){
currNode = currNode->left;
}else if(currNode->item < item){
currNode = currNode->right;
}
}
//push node that item belongs to
pathElements->push_back(currNode);
return pathElements;
}
Node* AVLTree::createNewNodeWithItem(int item)
{
Node* newNode = BinarySearchTree::createNewNodeWithItem(item);
newNode->height = 1;
return newNode;
}
int max(int first, int second)
{
return (first > second) ? first : second ;
}
int AVLTree::balanceFactor(Node* subtreePtr)
{
//bf = h(r) - h(l)
int balanceF = subtreePtr->right->getHeight() - subtreePtr->left->getHeight();
if (balanceF < -1){
return -2;
}
else if (balanceF > 1){
return 2;
}
else
return balanceF;
}
void AVLTree::updateHeight(Node *node)
{
#ifdef _DEBUG_
assert(node!=NULL);
#endif
node->height = 1 + max(node->left->getHeight(), node->right->getHeight());
}
//rotation
void AVLTree::leftRotate(Node* &nodeA, Node* &parentOfA)
{
Node* A = nodeA;
Node* B = nodeA->right;
if (nodeA == this->root){
//B is the new root
this->root = B;
}
else{
if (nodeA == parentOfA->left){
parentOfA->left = B;
}
else {
parentOfA->right = B;
}
}
A->right = B->left;
B->left = A;
updateHeight(A);
updateHeight(B);
}
void AVLTree::rightRotate(Node* &nodeA, Node* &parentOfA)
{
Node* A = nodeA;
Node* B = nodeA->left;
if (nodeA == this->root){
//B is the new root
this->root = B;
}
else{
if (nodeA == parentOfA->left){
parentOfA->left = B;
}
else {
parentOfA->right = B;
}
}
A->left = B->right;
B->right = A;
updateHeight(A);
updateHeight(B);
}
void AVLTree::balancePath(Node* &node)
{
//1.Get the path from the node that contains e to the root
vector<Node*>* pathFromNodeEToRoot = getPath(node->item);
Node* parentOfA;
//2. For each node in the path leading to the root
for (int i=pathFromNodeEToRoot->size()-1; i >= 0; i--)
{
//3. Update height of each node up to root
updateHeight(pathFromNodeEToRoot->at(i));
//4. Get parentOfA
if(i >= 1){
parentOfA = pathFromNodeEToRoot->at(i-1);
}else{
//if node is root, parent is NULL
parentOfA = NULL;
}
//4. Calculate balanceFactor of each node, and if AVL property is violated
//rebalance the tree, else do nothing
//cout << node->item << " " << endl;
switch(balanceFactor(pathFromNodeEToRoot->at(i))){
case -2:
if (balanceFactor(pathFromNodeEToRoot->at(i)->left) > 0){
//LR imbalance
this->leftRotate(pathFromNodeEToRoot->at(i)->left, pathFromNodeEToRoot->at(i));
}
//
this->rightRotate(pathFromNodeEToRoot->at(i), parentOfA);
break;
case 2:
if (balanceFactor(pathFromNodeEToRoot->at(i)->right) < 0){
//RL imbalance
this->rightRotate(pathFromNodeEToRoot->at(i)->right, pathFromNodeEToRoot->at(i));
}
this->leftRotate(pathFromNodeEToRoot->at(i), parentOfA);
break;
}
}
}
bool AVLTree::add(const ItemType& newData)
{
//1. Create new node and return to newNode
Node *newNode = NULL;
try{
newNode = AVLTree::createNewNodeWithItem(newData);
}
catch (bad_alloc e){
//cout << e.what();
}
//2. Call inorderInsert method and pass root of the tree along with new node
insertInOrder(root, newNode);
//3. Restore AVL property if is violated
balancePath(newNode);
numberOfNodes++;
return false;
}
bool AVLTree::remove(const ItemType& data)
{
//BinarySearchTree::remove(data);
if(root == NULL)
return false;// Element is not in the tree
// Locate the node to be deleted and also locate its parent node
Node* currNode = root;
Node* parentNode = NULL;
while(currNode != NULL){
if(data > currNode->item){
parentNode = currNode;
currNode= currNode->right;
}else if (data < currNode->item){
parentNode = currNode;
currNode = currNode->left;
}else{
break; //Element is in the tree pointed by currNode
}
}
if(currNode == NULL)
return false;//element is not founded
// Case 1: current has no left children
if(currNode->left == NULL){
// Connect the parent with the right child of the current node
if(parentNode == NULL)
root = currNode->right;
else {
if(data < parentNode->item )
parentNode->left = currNode->right;
else
parentNode->right = currNode->right;
balancePath(parentNode);
}
}
else
{
// Case 2: The current node has a left child
// Locate the rightmost node in the left subtree of
// the current node and also its parent
Node *parentOfRightMost = currNode;
Node *rightMost = currNode->left;
while(rightMost != NULL){
parentOfRightMost = rightMost;
rightMost = rightMost->right;
}
currNode->item = rightMost->item;
//delete rightMost
if(parentOfRightMost->right == rightMost)
parentOfRightMost->right = rightMost->left;
else
// Special case: parentOfRightMost is current
parentOfRightMost->left = rightMost->left;
// Balance the tree if necessary
balancePath(parentOfRightMost);
}
return true;
}