-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
423 lines (377 loc) · 14.1 KB
/
Copy pathmain.cpp
File metadata and controls
423 lines (377 loc) · 14.1 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#include <iostream>
#include <vector>
#include <chrono> // Do pomiaru czasu
#include <cmath>
#define N 1000
struct Node {
int value;
Node *left;
Node *right;
Node(int val) :
value(val),
left(nullptr),
right(nullptr) {}
};
int height(Node* node) {
if (node == nullptr)
return 0;
return std::max(height(node->left), height(node->right)) + 1;
}
int countNodes(Node* root) {
if (root == nullptr) return 0;
return 1 + countNodes(root->left) + countNodes(root->right);
}
void createBackbone(Node* root) {
Node* prev = nullptr;
Node* curr = root;
while (curr != nullptr) {
if (curr->left != nullptr) {
// Rotate right to eliminate left child
Node* temp = curr->left;
curr->left = temp->right;
temp->right = curr;
curr = temp;
if (prev != nullptr) prev->right = temp;
else root = temp;
}
else {
prev = curr;
curr = curr->right;
}
}
}
int balanceFactor(Node* node) {
if (node == nullptr)
return 0;
return std::abs(height(node->left) - height(node->right));
}
Node* leftRotate(Node* node) {
Node* newRoot = node->right;
node->right = newRoot->left;
newRoot->left = node;
return newRoot;
}
Node* rightRotate(Node* node) {
Node* newRoot = node->left;
node->left = newRoot->right;
newRoot->right = node;
return newRoot;
}
void compress(Node* grand, int m)
{
Node* tmp = grand->right;
for (int i = 0; i < m; i++) {
Node* oldTmp = tmp;
tmp = tmp->right;
grand->right = tmp;
oldTmp->right = tmp->left;
tmp->left = oldTmp;
grand = tmp;
tmp = tmp->right;
}
}
Node* balanceTree(Node* root) {
if (root == nullptr)
return nullptr;
int leftHeight = height(root->left);
int rightHeight = height(root->right);
if (leftHeight - rightHeight > 1) {
if (height(root->left->left) >= height(root->left->right))
root = rightRotate(root);
else {
root->left = leftRotate(root->left);
root = rightRotate(root);
}
}
else if (rightHeight - leftHeight > 1) {
if (height(root->right->right) >= height(root->right->left))
root = leftRotate(root);
else {
root->right = rightRotate(root->right);
root = leftRotate(root);
}
}
return root;
}
Node* dsw_balance(Node* root) {
if (root == nullptr) return nullptr;
Node* grand = new Node(0);
grand->right = root;
createBackbone(root); //gdyby drzewo nie bylo zdegenerowane, nalezy na poczatku sprowadzic je do winorosli
int n = countNodes(root);
int h = floor(log2(n + 1));
int m = pow(2, h) - 1;
compress(grand, n - m);
for (m = m / 2; m > 0; m /= 2) {
compress(grand, m);
}
return grand->right;
}
void addNode(Node *root, int n) {
if (root->value > n) {
if (root-> left != nullptr)
addNode(root->left, n);
else
root->left = new Node(n);
}
else {
if (root->right != nullptr)
addNode(root->right, n);
else
root->right = new Node(n);
}
}
void inorderPrint(Node *root) {
if (root != nullptr) {
inorderPrint(root->left);
std::cout << root->value << " -> ";
inorderPrint(root->right);
}
}
void preorderPrint(Node *root) {
if (root != nullptr) {
std::cout << root->value << " -> ";
preorderPrint(root->left);
preorderPrint(root->right);
}
}
void postorderPrint(Node *root) {
if (root != nullptr) {
postorderPrint(root->left);
postorderPrint(root->right);
std::cout << root->value << " -> ";
}
}
Node* deleteNode(Node* root, int val) {
if (root == nullptr)
return root;
if (val < root->value)
root->left = deleteNode(root->left, val);
else if (val > root->value)
root->right = deleteNode(root->right, val);
else {
if (root->left == nullptr) {
Node* temp = root->right;
delete root;
return temp;
}
else if (root->right == nullptr) {
Node* temp = root->left;
delete root;
return temp;
}
Node* temp = root->right;
while (temp->left != nullptr)
temp = temp->left;
root->value = temp->value;
root->right = deleteNode(root->right, temp->value);
}
return root;
}
void postOrderDelete(Node *root) {
if (root != nullptr) {
postOrderDelete(root->left);
postOrderDelete(root->right);
std::cout << "Deleting node: " << root->value << "\n";
delete root;
}
}
int findMax(Node *root) {
if (root == nullptr){
std::cout << "The tree is empty.\n";
return -1;
}
std::cout << "Search path (from the root to the maximum element): ";
while (root->right != nullptr) {
std::cout << root->value << " -> ";
root = root->right;
}
return root->value;
}
int findMin(Node *root) {
if (root == nullptr) {
std::cout << "The tree is empty.\n";
return -1;
}
std::cout << "Search path (from the root to the minimum element): ";
while (root->left != nullptr) {
std::cout << root->value << " -> ";
root = root->left;
}
return root->value;
}
Node* buildAVL(std::vector<int>& sortedArray, int start, int end) {
if (start > end)
return nullptr;
int mid = (start + end) / 2; // assumes the list is sorted (sort it beforehand otherwise)
Node* root = new Node(sortedArray[mid]);
root->left = buildAVL(sortedArray, start, mid - 1);
root->right = buildAVL(sortedArray, mid + 1, end);
return root;
}
int main() {
int option_generate;
std::cout << "Choose an option:\n";
std::cout << "1. Generate the trees automatically\n";
std::cout << "2. Enter a list of numbers\n";
std::cin >> option_generate;
int n;
std::vector<int> numbers;
if (option_generate == 2) {
std::cout << "Enter the number of elements: ";
std::cin >> n;
std::cout << "Enter " << n << " numbers:\n";
numbers.resize(n);
for (int i = 0; i < n; ++i) {
std::cin >> numbers[i];
}
std::sort(numbers.begin(), numbers.end());
} else {
std::cout << "Enter the number of elements: ";
std::cin >> n;
numbers.resize(n);
for (int i = 0; i < n; ++i) {
numbers[i] = i + 1;
}
}
// BUILDING A DEGENERATE BST
// int *n;
// n = new int[N];
// for(int i=0; i < N; ++i)
// n[i] = i+1;
std::cout<<"Building the (degenerate) BST: ";
Node *bstRoot = new Node(numbers[0]);
auto start = std::chrono::steady_clock::now();
for (int i = 1; i < n; ++i) {
addNode(bstRoot, numbers[i]);
}
auto end = std::chrono::steady_clock::now();
std::cout << "BST created. Operation time: " << std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() << " microseconds" << std::endl;
// // BUILDING THE AVL TREE BY BINARY HALVING
// std::vector<int> sortedArray;
// for (int i = 1; i <= n; ++i) {
// sortedArray.push_back(i);
// }
std::cout<<"Building the AVL tree by binary halving: ";
start = std::chrono::steady_clock::now();
Node* avlRoot = buildAVL(numbers, 0, numbers.size() - 1);
end = std::chrono::steady_clock::now();
std::cout << "AVL tree created. Operation time: " << std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() << " microseconds" << std::endl;
// MENU LOOP
int option;
do {
std::cout << "\nMenu:\n";
std::cout << "1. Find the smallest and largest element in the tree\n";
std::cout << "2. Delete a tree element with a given key\n";
std::cout << "3. Print all tree elements in-order and pre-order\n";
std::cout << "4. Delete the whole tree node by node (post-order)\n";
std::cout << "5. Balance the tree\n";
std::cout << "0. Exit\n";
std::cout << "Choose an option: ";
std::cin >> option;
switch (option) {
case 1:
{
auto start = std::chrono::steady_clock::now();
std::cout << "Drzewo BST:\n";
std::cout << "Maksymalny element: " << findMax(bstRoot) << std::endl;
// std::cout << "Minimalny element: " << findMin(bstRoot) << std::endl;
auto end = std::chrono::steady_clock::now();
std::cout << "Czas operacji dla drzewa BST: " << std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() << " microseconds" << std::endl;
start = std::chrono::steady_clock::now();
std::cout << "Drzewo AVL:\n";
std::cout << "Maksymalny element: " << findMax(avlRoot) << std::endl;
// std::cout << "Minimalny element: " << findMin(avlRoot) << std::endl;
end = std::chrono::steady_clock::now();
std::cout << "Czas operacji dla drzewa AVL: " << std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() << " microseconds" << std::endl;
}
break;
case 2:
{
int numToDelete;
std::cout << "Podaj liczbe wezlow do usuniecia: ";
std::cin >> numToDelete;
std::cout << "Podaj wartosci kluczy do usuniecia:\n";
auto startBST = std::chrono::steady_clock::now();
for (int i = 0; i < numToDelete; ++i) {
int key;
std::cout << "Wartosc klucza " << i + 1 << ": ";
std::cin >> key;
bstRoot = deleteNode(bstRoot, key);
}
auto endBST = std::chrono::steady_clock::now();
std::cout << "Czas operacji usuwania dla drzewa BST: " << std::chrono::duration_cast<std::chrono::microseconds>(endBST - startBST).count() << " microseconds" << std::endl;
auto startAVL = std::chrono::steady_clock::now();
for (int i = 0; i < numToDelete; ++i) {
int key;
std::cout << "Wartosc klucza " << i + 1 << ": ";
std::cin >> key;
avlRoot = deleteNode(avlRoot, key);
}
auto endAVL = std::chrono::steady_clock::now();
std::cout << "Czas operacji usuwania dla drzewa AVL: " << std::chrono::duration_cast<std::chrono::microseconds>(endAVL - startAVL).count() << " microseconds" << std::endl;
}
break;
case 3:
{
auto startBST = std::chrono::steady_clock::now();
std::cout << "Drzewo BST (In-order): ";
inorderPrint(bstRoot);
std::cout << std::endl;
// std::cout << "Drzewo BST (Pre-order): ";
// preorderPrint(bstRoot);
// std::cout << std::endl;
auto endBST = std::chrono::steady_clock::now();
std::cout << "Czas operacji dla wypisywania elementow dla drzewa BST: " << std::chrono::duration_cast<std::chrono::microseconds>(endBST - startBST).count() << " microseconds" << std::endl;
auto startAVL = std::chrono::steady_clock::now();
std::cout << "Drzewo AVL (In-order): ";
inorderPrint(avlRoot);
std::cout << std::endl;
// std::cout << "Drzewo AVL (Pre-order): ";
// preorderPrint(avlRoot);
// std::cout << std::endl;
auto endAVL = std::chrono::steady_clock::now();
std::cout << "Czas operacji dla wypisywania elementow dla drzewa AVL: " << std::chrono::duration_cast<std::chrono::microseconds>(endAVL - startAVL).count() << " microseconds" << std::endl;
}
break;
case 4:
{
auto startBST = std::chrono::steady_clock::now();
postOrderDelete(bstRoot);
std::cout << "Drzewo BST zostalo usuniete.\n";
auto endBST = std::chrono::steady_clock::now();
std::cout << "Czas operacji usuwania drzewa BST: " << std::chrono::duration_cast<std::chrono::microseconds>(endBST - startBST).count() << " microseconds" << std::endl;
auto startAVL = std::chrono::steady_clock::now();
postOrderDelete(avlRoot);
std::cout << "Drzewo AVL zostalo usuniete.\n";
auto endAVL = std::chrono::steady_clock::now();
std::cout << "Czas operacji usuwania drzewa AVL: " << std::chrono::duration_cast<std::chrono::microseconds>(endAVL - startAVL).count() << " microseconds" << std::endl;
}
break;
case 5:
{
auto startBST = std::chrono::steady_clock::now();
std::cout << "Wywoluje rownowazenie drzewa BST...\n";
bstRoot = dsw_balance(bstRoot);
std::cout << "Drzewo BST zostalo zrownowazone.\n";
auto endBST = std::chrono::steady_clock::now();
std::cout << "Czas operacji rownowazenia drzewa BST: " << std::chrono::duration_cast<std::chrono::microseconds>(endBST - startBST).count() << " microseconds" << std::endl;
// auto startAVL = std::chrono::steady_clock::now();
// std::cout << "Wywoluje rownowazenie drzewa AVL...\n";
// avlRoot = balanceTree(avlRoot);
// std::cout << "Drzewo AVL zostalo zrownowazone.\n";
// auto endAVL = std::chrono::steady_clock::now();
// std::cout << "Czas operacji rownowazenia drzewa AVL: " << std::chrono::duration_cast<std::chrono::microseconds>(endAVL - startAVL).count() << " microseconds" << std::endl;
}
break;
case 0:
std::cout << "Koniec programu.\n";
break;
default:
std::cout << "Niepoprawna opcja. Sprobuj ponownie.\n";
break;
}
} while (option != 0);
// delete[] n;
return 0;
}