-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
244 lines (209 loc) · 5.75 KB
/
main.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
#include <iostream>
#include<string>
using namespace std;
struct node {
int value;
struct node *rightChild;
struct node *leftChild;
int height;
int count;
};
class AVLTree {
private:
node *root;
public:
AVLTree();
void InsertElement(int key);
pair<int*,int*> getChildrenNodesValues(int *key);
int *getRootNode();
string PreOrderTraversal();
string PostOrderTraversal();
};
//initialize AVLTree
AVLTree::AVLTree(){
root = NULL;
}
int *AVLTree::getRootNode() {
return &root->value;
}
string printPreOrder(struct node *node, string s){
if (node == NULL) {
return s;
}
s += std::to_string(node->value) + " ";
if (node->leftChild != NULL){
s = printPreOrder(node->leftChild, s);
}
if (node->rightChild != NULL){
s = printPreOrder(node->rightChild, s);
}
return s;
}
string AVLTree::PreOrderTraversal() {
string s = "";
return printPreOrder(root, s);
}
string printPostOrder(struct node *node, string s){
if (node == NULL){
return s;
}
if (node->leftChild != NULL){
s = printPostOrder(node->leftChild, s);
}
if (node->rightChild != NULL){
s = printPostOrder(node->rightChild, s);
}
s += std::to_string(node->value) + " ";
return s;
}
string AVLTree::PostOrderTraversal(){
string s = "";
return printPostOrder(root, s);
}
int max (int h1, int h2){
if (h1 > h2){
return h1;
} else {
return h2;
}
}
int nodeHeight (struct node *node){
if (node == NULL){
return 0;
} else {
return node->height;
}
}
int nodeBF (struct node *node){
if (node == NULL){
return 0;
} else {
return nodeHeight(node->rightChild) - nodeHeight(node->leftChild);
}
}
// X Y
// / \ / \
// c1 Y ---> X c2
// / \ / \
// subtree c2 c1 subtree
struct node *rotateLeft(struct node *x){
struct node *y = x->rightChild;
struct node *subtree = y->leftChild;
x->rightChild = subtree;
y->leftChild = x;
x->height = 1 + max(nodeHeight(x->leftChild),nodeHeight(x->rightChild));
y->height = 1 + max(nodeHeight(y->leftChild),nodeHeight(y->rightChild));
return y;
}
// X Y
// / \ / \
// Y c2 ---> c1 X
// / \ / \
// c1 subtree subtree c2
struct node *rotateRight(struct node *x){
struct node *y = x -> leftChild;
struct node *subtree = y -> rightChild;
x->leftChild = subtree;
y->rightChild = x;
x->height = 1 + max(nodeHeight(x->leftChild),nodeHeight(x->rightChild));
y->height = 1 + max(nodeHeight(y->leftChild),nodeHeight(y->rightChild));
return y;
}
struct node *createNode(struct node* node, int key){
if (node == NULL){
struct node *node = new struct node;
node->value = key;
node->leftChild = NULL;
node->rightChild = NULL;
node->height = 1;
node->count = 0;
return node;
} else if (key > node->value){
node->rightChild = createNode(node->rightChild, key);
} else if (key < node->value){
node->leftChild = createNode(node->leftChild, key);
} else {
node->count++;
return node;
}
// update height for every node in recursion
node->height = 1 + max(nodeHeight(node->leftChild), nodeHeight(node->rightChild));
int balanceFactor = nodeBF(node);
// 1) case when BF(node) = 2 and BF(node->rightChild) = 1
// LEFT ROTATION
if (balanceFactor == 2 && nodeBF(node->rightChild) == 1){
return rotateLeft(node);
}
// 2) case when BF(node) = -2 and BF(node->leftChild) = -1
// RIGHT ROTATION
if (balanceFactor == -2 && nodeBF(node->leftChild) == -1){
return rotateRight(node);
}
// 3) case when BF(node) = -2 and BF(node->leftChild) = 1ž
// LEFT-RIGHT ROTATION
if (balanceFactor == -2 && nodeBF(node->leftChild) == 1){
node->leftChild = rotateLeft(node->leftChild);
return rotateRight(node);
}
// 4) case when BF(node) = 2 and BF(node->rightChild) = -1
// RIGHT-LEFT ROTATION
if (balanceFactor == 2 && nodeBF(node->rightChild) == -1){
node->rightChild = rotateRight(node->rightChild);
return rotateLeft(node);
}
return node;
}
void AVLTree::InsertElement(int key) {
if (root == NULL){
root = new struct node;
root->value = key;
root->leftChild = NULL;
root->rightChild = NULL;
root->height = 1;
return;
} else {
root = createNode(root, key);
}
}
pair<int*,int*> AVLTree::getChildrenNodesValues(int *key){
pair<int*,int*> result;
struct node *node = root;
while (node != NULL){
if (*key > node->value){
node = node->rightChild;
} else if (*key < node->value){
node = node->leftChild;
} else if (*key == node->value){
break;
}
}
if (node->leftChild != NULL){
result.first = &node->leftChild->value;
}
if (node->rightChild != NULL){
result.second = &node->rightChild->value;
}
return result;
}
int main(){
AVLTree avl = AVLTree();
int *root = avl.getRootNode();
avl.InsertElement(6);
avl.InsertElement(51);
avl.InsertElement(49);
avl.InsertElement(71);
avl.InsertElement(92);
avl.InsertElement(59);
avl.InsertElement(79);
avl.InsertElement(89);
avl.InsertElement(64);
avl.InsertElement(88);
avl.InsertElement(47);
avl.InsertElement(82);
cout << "Preorder: ";
string a = avl.PreOrderTraversal();
cout << a << "\n";
cout << "Postorder: ";
string b = avl.PostOrderTraversal();
cout << b << "\n";
}