-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ#03_Lab9.cpp
More file actions
227 lines (189 loc) · 5.62 KB
/
Q#03_Lab9.cpp
File metadata and controls
227 lines (189 loc) · 5.62 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
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
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* leftChild;
Node* rightChild;
int height;
Node(int value) {
data = value;
leftChild = NULL;
rightChild = NULL;
height = 1;
}
};
int calculateHeight(Node* node) {
if (node == NULL) {
return 0;
}
int leftHeight = calculateHeight(node->leftChild);
int rightHeight = calculateHeight(node->rightChild);
return 1 + max(leftHeight, rightHeight);
}
int calculateBalanceFactor(Node* node) {
if (node == NULL) {
return 0;
}
int leftHeight = calculateHeight(node->leftChild);
int rightHeight = calculateHeight(node->rightChild);
return leftHeight - rightHeight;
}
Node* performLLRotation(Node* node) {
Node* leftSubtreeRoot = node->leftChild;
Node* leftRightSubtree = leftSubtreeRoot->rightChild;
leftSubtreeRoot->rightChild = node;
node->leftChild = leftRightSubtree;
node->height = calculateHeight(node);
leftSubtreeRoot->height = calculateHeight(leftSubtreeRoot);
return leftSubtreeRoot;
}
Node* performRRRotation(Node* node) {
Node* rightSubtreeRoot = node->rightChild;
Node* rightLeftSubtree = rightSubtreeRoot->leftChild;
rightSubtreeRoot->leftChild = node;
node->rightChild = rightLeftSubtree;
node->height = calculateHeight(node);
rightSubtreeRoot->height = calculateHeight(rightSubtreeRoot);
return rightSubtreeRoot;
}
Node* performLRRotation(Node* node) {
node->leftChild = performRRRotation(node->leftChild);
return performLLRotation(node);
}
Node* performRLRotation(Node* node) {
node->rightChild = performLLRotation(node->rightChild);
return performRRRotation(node);
}
Node* insertNode(Node* root, int value) {
if (root == NULL) {
return new Node(value);
}
if (value < root->data) {
root->leftChild = insertNode(root->leftChild, value);
} else if (value > root->data) {
root->rightChild = insertNode(root->rightChild, value);
} else {
return root;
}
root->height = calculateHeight(root);
int balance = calculateBalanceFactor(root);
if (balance > 1 && value < root->leftChild->data) {
return performLLRotation(root);
}
if (balance < -1 && value > root->rightChild->data) {
return performRRRotation(root);
}
if (balance > 1 && value > root->leftChild->data) {
return performLRRotation(root);
}
if (balance < -1 && value < root->rightChild->data) {
return performRLRotation(root);
}
return root;
}
void inorderTraversal(Node* root) {
if (root == NULL) {
return;
}
inorderTraversal(root->leftChild);
cout << root->data << " ";
inorderTraversal(root->rightChild);
}
void preorderTraversal(Node* root) {
if (root == NULL) {
return;
}
cout << root->data << " ";
preorderTraversal(root->leftChild);
preorderTraversal(root->rightChild);
}
Node* findMinValueNode(Node* node) {
while (node->leftChild != NULL) {
node = node->leftChild;
}
return node;
}
Node* deleteNode(Node* root, int value) {
if (root == NULL) {
return root;
}
if (value < root->data) {
root->leftChild = deleteNode(root->leftChild, value);
} else if (value > root->data) {
root->rightChild = deleteNode(root->rightChild, value);
} else {
if ((root->leftChild == NULL) || (root->rightChild == NULL)) {
Node* temp = root->leftChild ? root->leftChild : root->rightChild;
if (temp == NULL) {
temp = root;
root = NULL;
} else {
*root = *temp;
}
delete temp;
} else {
Node* temp = findMinValueNode(root->rightChild);
root->data = temp->data;
root->rightChild = deleteNode(root->rightChild, temp->data);
}
}
if (root == NULL) {
return root;
}
root->height = calculateHeight(root);
int balance = calculateBalanceFactor(root);
if (balance > 1 && calculateBalanceFactor(root->leftChild) >= 0) {
return performLLRotation(root);
}
if (balance > 1 && calculateBalanceFactor(root->leftChild) < 0) {
return performLRRotation(root);
}
if (balance < -1 && calculateBalanceFactor(root->rightChild) <= 0) {
return performRRRotation(root);
}
if (balance < -1 && calculateBalanceFactor(root->rightChild) > 0) {
return performRLRotation(root);
}
return root;
}
void postorderTraversal(Node* root) {
if (root == NULL) {
return;
}
postorderTraversal(root->leftChild);
postorderTraversal(root->rightChild);
cout << root->data << " ";
}
int main() {
Node* root = NULL;
root = insertNode(root, 1);
root = insertNode(root, 2);
root = insertNode(root, 3);
root = insertNode(root, 4);
root = insertNode(root, 5);
root = insertNode(root, 6);
root = insertNode(root, 7);
cout << "AVL Tree before deletion:" << endl;
cout << "In-order Traversal: ";
inorderTraversal(root);
cout << endl;
cout << "Pre-order Traversal: ";
preorderTraversal(root);
cout << endl;
cout << "Post-order Traversal: ";
postorderTraversal(root);
cout << endl;
root = deleteNode(root, 3);
cout << "\nAVL Tree after deleting 3:" << endl;
cout << "Pre-order Traversal: ";
preorderTraversal(root);
cout << endl;
cout << "In-order Traversal: ";
inorderTraversal(root);
cout << endl;
cout << "Post-order Traversal: ";
postorderTraversal(root);
cout << endl;
return 0;
}