-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpaceSectorLLRBT.cpp
340 lines (270 loc) · 9 KB
/
SpaceSectorLLRBT.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
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
#include <algorithm>
#include <stack>
#include <unordered_map>
#include <queue>
#include "SpaceSectorLLRBT.h"
using namespace std;
SpaceSectorLLRBT::SpaceSectorLLRBT() : root(nullptr) {}
void SpaceSectorLLRBT::readSectorsFromFile(const std::string& filename) {
ifstream file(filename);
if (!file.is_open()) {
cerr << "Unable to open the file: " << filename << endl;
return;
}
string line;
getline(file, line); // Ignore the first line as it contains headers
while (getline(file, line)) {
stringstream ss(line);
string coordinate;
vector<int> coordinates;
while (getline(ss, coordinate, ',')) {
coordinates.push_back(stoi(coordinate));
}
if (coordinates.size() == 3) {
insertSectorByCoordinates(coordinates[0], coordinates[1], coordinates[2]);
} else {
cerr << "Invalid line format: " << line << endl;
}
}
file.close();
}
SpaceSectorLLRBT::~SpaceSectorLLRBT() {
// Root düğümünden başlayarak tüm düğümleri sil
deleteTree(root);
}
void SpaceSectorLLRBT::deleteTree(Sector* node) {
if (node == nullptr) {
return;
}
// Önce sol alt ağacı, sonra sağ alt ağacı sil (postorder traversal)
deleteTree(node->left);
deleteTree(node->right);
// Düğümü sil
delete node;
}
void SpaceSectorLLRBT::insertSectorByCoordinates(int x, int y, int z) {
root = insertRecursive(root, nullptr, x, y, z);
root->color = false; // Yeni kök düğümü siyah yap
}
Sector* SpaceSectorLLRBT::insertRecursive(Sector* node, Sector* parent, int x, int y, int z) {
if (node == nullptr) {
Sector* new_node = new Sector(x, y, z);
new_node->parent = parent; // Ebeveyn düğümü ayarla
new_node->color = true;
return new_node;
}
// LLRBT'ye göre düğümleri ekleme
if (x < node->x || (x == node->x && y < node->y) || (x == node->x && y == node->y && z < node->z)) {
node->left = insertRecursive(node->left, node, x, y, z);
} else if (x > node->x || (x == node->x && y > node->y) || (x == node->x && y == node->y && z > node->z)) {
node->right = insertRecursive(node->right, node, x, y, z);
}
// LLRBT özelliklerini kontrol et ve düzenle
if (isRed(node->right) && !isRed(node->left)) {
node = rotateLeft(node);
}
if (isRed(node->left) && isRed(node->left->left)) {
node = rotateRight(node);
}
if (isRed(node->left) && isRed(node->right)) {
flipColors(node);
}
return node;
}
Sector* SpaceSectorLLRBT::rotateRight(Sector* node) {
Sector* left_child = node->left;
node->left = left_child->right;
if (left_child->right != nullptr) {
left_child->right->parent = node;
}
left_child->right = node;
left_child->parent = node->parent;
node->parent = left_child;
left_child->color = node->color;
node->color = true; // LLRBT'de rotasyon sonrası renk değişimi
return left_child;
}
Sector* SpaceSectorLLRBT::rotateLeft(Sector* node) {
Sector* right_child = node->right;
node->right = right_child->left;
if (right_child->left != nullptr) {
right_child->left->parent = node;
}
right_child->left = node;
right_child->parent = node->parent;
node->parent = right_child;
right_child->color = node->color;
node->color = true; // LLRBT'de rotasyon sonrası renk değişimi
return right_child;
}
void SpaceSectorLLRBT::flipColors(Sector* node) {
if (node != nullptr && node->left != nullptr && node->right != nullptr) {
node->color = !node->color;
node->left->color = !node->left->color;
node->right->color = !node->right->color;
}
}
bool SpaceSectorLLRBT::isRed(Sector* node) {
if (node == nullptr) {
return false;
}
return node->color;
}
void SpaceSectorLLRBT::displaySectorsInOrder() {
std::cout << "Space sectors inorder traversal:" << std::endl;
inOrderTraversal(root);
std::cout << std::endl;
}
void SpaceSectorLLRBT::displaySectorsPreOrder() {
std::cout << "Space sectors preorder traversal:" << std::endl;
preOrderTraversal(root);
std::cout << std::endl;
}
void SpaceSectorLLRBT::displaySectorsPostOrder() {
std::cout << "Space sectors postorder traversal:" << std::endl;
postOrderTraversal(root);
std::cout << std::endl;
}
void SpaceSectorLLRBT::inOrderTraversal(Sector* node) {
if (node == nullptr) {
return;
}
inOrderTraversal(node->left);
printSector(node);
inOrderTraversal(node->right);
}
void SpaceSectorLLRBT::preOrderTraversal(Sector* node) {
if (node == nullptr) {
return;
}
printSector(node);
preOrderTraversal(node->left);
preOrderTraversal(node->right);
}
void SpaceSectorLLRBT::postOrderTraversal(Sector* node) {
if (node == nullptr) {
return;
}
postOrderTraversal(node->left);
postOrderTraversal(node->right);
printSector(node);
}
void SpaceSectorLLRBT::printSector(Sector* node) {
if (node != nullptr) {
cout << (node->color ? "RED" : "BLACK") << " sector: " << node->sector_code << endl;
}
}
Sector* SpaceSectorLLRBT::findSector(Sector* node, const std::string& sector_code) {
if (node == nullptr) {
return nullptr;
}
// Eğer aranan sektör kodu, mevcut düğümün sektör koduysa, bulundu demektir.
if (node->sector_code == sector_code) {
return node;
}
// Eğer aranan sektör kodu, mevcut düğümün sektör kodundan küçükse,
// sol alt ağaca bakılır. Aksi takdirde sağ alt ağaca bakılır.
Sector* left_search = findSector(node->left, sector_code);
if (left_search != nullptr) {
return left_search;
}
Sector* right_search = findSector(node->right, sector_code);
return right_search;
}
bool SpaceSectorLLRBT::getPath(Sector* root, std::vector<string>& path, string x) {
if (root == nullptr) {
return false;
}
path.push_back(root->sector_code); // Düğüm değerini vektöre ekle
if (root->sector_code == x) {
return true; // Hedef düğüm bulundu
}
// Sol alt ağaca bak
if (getPath(root->left, path, x)) {
return true; // Hedef düğüm sol alt ağaçta bulundu
}
// Sağ alt ağaca bak
if (getPath(root->right, path, x)) {
return true; // Hedef düğüm sağ alt ağaçta bulundu
}
// Düğümün altında hedef düğüm bulunamadı, düğümü vektörden çıkar
path.pop_back();
return false;
}
std::vector<Sector*> SpaceSectorLLRBT::getStellarPath(const std::string& sector_code) {
std::vector<Sector*> path;
// Earth ve Dr. Elara düğümlerini bul
Sector* earth = findSector(root, "0SSS");
Sector* elara = findSector(root, sector_code);
if (earth == nullptr || elara == nullptr) {
return path;
}
// Yeni algoritma için gerekli yapıları oluşturma ve çağırma
std::vector<string> path1, path2;
getPath(root, path1, "0SSS"); // Earth'ü kök düğüm kabul ederek yolu bulma
getPath(root, path2, sector_code); // Hedef düğüme kadar olan yolu bulma
int intersection = -1;
// Kesişim noktasını bulma
int i = 0, j = 0;
while (i != path1.size() || j != path2.size()) {
if (i == j && path1[i] == path2[j]) {
i++;
j++;
} else {
intersection = j - 1;
break;
}
}
// Yolu oluşturma
for (int i = path1.size() - 1; i > intersection; i--) {
Sector* sector = findSector(root, (path1[i]));
path.push_back(sector);
}
for (int i = intersection; i < path2.size(); i++) {
Sector* sector = findSector(root, (path2[i]));
path.push_back(sector);
}
return path;
}
void SpaceSectorLLRBT::findPathToRoot(Sector* root, Sector* target, std::stack<Sector*>& path) {
if (root == nullptr || target == nullptr) {
return;
}
std::unordered_map<Sector*, Sector*> parent;
std::queue<Sector*> q;
q.push(root);
// Parent'ları bulana kadar BFS
while (!q.empty()) {
Sector* current = q.front();
q.pop();
if (current->left) {
parent[current->left] = current;
q.push(current->left);
}
if (current->right) {
parent[current->right] = current;
q.push(current->right);
}
}
// Hedef düğümden root'a kadar parent'ları takip ederek path bul
Sector* current = target;
while (current != nullptr) {
path.push(current);
current = parent[current];
}
}
void SpaceSectorLLRBT::printStellarPath(const std::vector<Sector*>& path) {
if (path.empty()) {
std::cout << "A path to Dr. Elara could not be found." << std::endl;
return;
}
std::cout << "The stellar path to Dr. Elara: ";
// Yolu ekrana yazdır
for (size_t i = 0; i < path.size(); ++i) {
std::cout << path[i]->sector_code;
if (i != path.size() - 1) {
std::cout << "->";
}
}
std::cout << std::endl;
}