-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab2.cpp
More file actions
434 lines (419 loc) · 12.7 KB
/
Copy pathlab2.cpp
File metadata and controls
434 lines (419 loc) · 12.7 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
424
425
426
427
428
429
430
431
432
433
434
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cerrno>
#include <stdio.h>
typedef unsigned long long i64;
int flagI = 1;
int flagR = 1;
int isCorrect = 1;
typedef struct node {
char* key;
i64 value;
node* left;
node* right;
int balance;
node(char* k, i64 vl) {
key = new char[strlen(k) + 1];
strcpy(key, k);
value = vl;
left = NULL;
right = NULL;
balance = 0;
}
} Node;
node* RotateToLeft(node* root) {
node* temp = root->right;
root->right = temp->left;
temp->left = root;
return temp;
}
node* RotateToRight(node* root) {
node* temp = root->left;
root->left = temp->right;
temp->right = root;
return temp;
}
node* FixBalance(node* root) {
int rb = root->balance;
if (rb == -2) {
int cb = (root->right)->balance;
if (cb == -1) {
root->balance += 2;
(root->right)->balance += 1;
root = RotateToLeft(root);
} else if (cb == 0) {
root->balance += 1;
(root->right)->balance += 1;
root = RotateToLeft(root);
} else if (cb == 1) {
int ind = ((root->right)->left)->balance;
if (ind == 1) {
root->balance += 2;
(root->right)->balance -= 2;
((root->right)->left)->balance -= 1;
} else if (ind == 0) {
root->balance += 2;
(root->right)->balance -= 1;
} else if (ind == -1) {
root->balance += 3;
(root->right)->balance -= 1;
((root->right)->left)->balance += 1;
} else {
std::cout << "Unknown case." << std::endl;
exit(11);
}
root->right = RotateToRight(root->right);
root = RotateToLeft(root);
} else {
std::cout << "Unknown case." << std::endl;
exit(12);
}
} else if (rb == 2) {
int cb = (root->left)->balance;
if (cb == 1) {
root->balance -= 2;
(root->left)->balance -= 1;
root = RotateToRight(root);
} else if (cb == 0) {
root->balance -= 1;
(root->left)->balance -= 1;
root = RotateToRight(root);
} else if (cb == -1) {
int ind = ((root->left)->right)->balance;
if (ind == -1) {
root->balance -= 2;
(root->left)->balance += 2;
((root->left)->right)->balance += 1;
} else if (ind == 0) {
root->balance -= 2;
(root->left)->balance += 1;
} else if (ind == 1) {
root->balance -= 3;
(root->left)->balance += 1;
((root->left)->right)->balance -= 1;
} else {
std::cout << "Unknown case." << std::endl;
exit(13);
}
root->left = RotateToLeft(root->left);
root = RotateToRight(root);
} else {
std::cout << "Unknown case." << std::endl;
exit(14);
}
}
return root;
}
node* Insert(char* k, i64 vl, node* root) { //works
flagI = 1;
if (!root) {
std::cout << "OK" << std::endl;
node* temp = new node(k, vl);
return temp;
}
if (strcmp(k, root->key) > 0) {
root->right = Insert(k, vl, root->right);
if (flagI) {
root->balance -= 1;
}
if (root->balance == 0) {
flagI = 0;
} else if (root->balance == -2) {
node* temp = FixBalance(root);
if (temp->balance == 0) {
flagI = 0;
}
return temp;
}
} else if (strcmp(k, root->key) < 0){
root->left = Insert(k, vl, root->left);
if (flagI) {
root->balance += 1;
}
if (root->balance == 0) {
flagI = 0;
} else if (root->balance == 2) {
node* temp = FixBalance(root);
if (temp->balance == 0) {
flagI = 0;
}
return temp;
}
} else {
std::cout << "Exist" << std::endl;
flagI = 0;
return root;
}
return root;
}
void MinNode(node* root, node* mN) {
if (root->left) {
MinNode(root->left, mN);
} else {
delete [] mN->key;
mN->key = new char[strlen(root->key) + 1];
strcpy(mN->key, root->key);
mN->value = root->value;
}
}
node* RemoveMin(node* root) {
flagR = 1;
if (!root->left) {
node* temp = root->right;
delete [] root->key;
delete root;
return temp;
}
if (root->left) {
root->left = RemoveMin(root->left);
if (flagR) {
root->balance -= 1;
}
if (root->balance == -2) {
node* temp = FixBalance(root);
if (temp->balance == -1 || temp->balance == 1) {
flagR = 0;
}
return temp;
}
if (root->balance == -1 || root->balance == 1) {
flagR = 0;
}
}
return root;
}
node* Remove(char* k, node* root) {
flagR = 1;
if (!root) {
std::cout << "NoSuchWord" << std::endl;
flagR = 0;
return NULL;
}
if (strcmp(k, root->key) > 0) {
root->right = Remove(k, root->right);
if (flagR) {
root->balance += 1;
}
if (root->balance == 2) {
node* temp = FixBalance(root);
if (temp->balance == -1 || temp->balance == 1) {
flagR = 0;
}
return temp;
}
if (root->balance == -1 || root->balance == 1) {
flagR = 0;
}
} else if (strcmp(k, root->key) < 0) {
root->left = Remove(k, root->left);
if (flagR) {
root->balance -= 1;
}
if (root->balance == -2) {
node* temp = FixBalance(root);
if (temp->balance == -1 || temp->balance == 1) {
flagR = 0;
}
return temp;
}
if (root->balance == -1 || root->balance == 1) {
flagR = 0;
}
} else {
std::cout << "OK" << std::endl;
node* lt = root->left;
node* rt = root->right;
int bl = root->balance;
delete [] root->key;
delete root;
if(!rt) {
flagR = 1;
return lt;
}
char temp[2] = " ";
node* minNode = new node(temp,0);
MinNode(rt, minNode);
minNode->balance = bl;
minNode->right = RemoveMin(rt);
minNode->left = lt;
if (flagR) {
minNode->balance += 1;
}
if (minNode->balance == 2) {
node* temp = FixBalance(minNode);
if (temp->balance == -1 || temp->balance == 1) {
flagR = 0;
}
return temp;
}
if (minNode->balance == 1 || minNode->balance == -1) {
flagR = 0;
}
return minNode;
}
return root;
}
void Find(char* k, node* root) {
if (!root) {
std::cout << "NoSuchWord" << std::endl;
} else {
if (strcmp(k, root->key) > 0) {
Find(k, root->right);
} else if (strcmp(k, root->key) < 0) {
Find(k, root->left);
} else {
std::cout << "OK: " << root->value << std::endl;
}
}
}
void Serialise(node* root, FILE* f) {
char nll= 'z';
char leaf = 'l';
char notLeaf = 'n';
if (!root) {
fwrite(&nll, sizeof(char), 1, f);
} else {
if (!root->right && !root->left) {
fwrite(&leaf, sizeof(char), 1, f);
int size = strlen(root->key) + 1;
fwrite(&size, sizeof(int), 1, f);
fwrite((root->key), size * sizeof(char), 1, f);
fwrite(&(root->value), sizeof(i64), 1, f);
fwrite(&(root->balance), sizeof(int), 1, f);
} else {
fwrite(¬Leaf, sizeof(char), 1, f);
int size = strlen(root->key) + 1;
fwrite(&size, sizeof(int), 1, f);
fwrite((root->key), size * sizeof(char), 1, f);
fwrite(&(root->value), sizeof(i64), 1, f);
fwrite(&(root->balance), sizeof(int), 1, f);
Serialise(root->left, f);
Serialise(root->right, f);
}
}
}
node* Deserialise(FILE* f) {
char lf;
if ((lf = getc(f)) != EOF ) {
if (lf != 'z') {
char k[257];
i64 v;
int b;
int size;
fread(&size, sizeof(int), 1, f);
fread(k, size * sizeof(char), 1, f);
fread(&v, sizeof(i64), 1, f);
fread(&b, sizeof(int), 1, f);
node* res = new node(k,v);
res->balance = b;
if (lf != 'l') {
res->left = Deserialise(f);
res->right = Deserialise(f);
}
return res;
} else {
return NULL;
}
} else {
std::cout << "ERROR: empty file." << std::endl;
isCorrect = 0;
return NULL;
}
}
node* DeleteTree(node* root) {
if (root->right) {
DeleteTree(root->right);
}
if (root->left) {
DeleteTree(root->left);
}
delete [] root->key;
delete root;
return NULL;
}
void Print(node* root, int i) {
if (root) {
Print(root->right, i+1);
for (int k = 0; k < i; k++) {
std::cout << " ";
}
std::cout << root->key << "\t" << root->value << ", balance: " << root->balance <<std::endl;
Print(root->left, i+1);
}
}
int main() {
FILE* f;
struct node* root = NULL;
char action;
while(std::cin >> action) {
if(action == '+') {
char str[257];
i64 num;
std::cin >> str >> num;
std::transform(str, str + strlen(str), str, (int (*)(int))std::tolower);
root = Insert(str, num, root);
} else if (action == '-') {
char str[257];
std::cin >> str;
std::transform(str, str + strlen(str), str, (int (*)(int))std::tolower);
root = Remove(str,root);
} else if (action == '!') {
char act[5];
char path[257];
std::cin >> act >> path;
if (!strcmp(act, "Load")) {
f = fopen(path, "rb");
if (f) {
rewind(f);
char temp[7];
fread(&temp, 7 * sizeof(char), 1, f);
if (!strcmp(temp, "MYFILE")) {
node* temp = Deserialise(f);
if (temp) {
root = DeleteTree(root);
root = temp;
} else if (!temp && isCorrect) {
root = NULL;
}
std::cout << "OK" << std::endl;
} else {
std::cout << "ERROR: incorrect file." << std::endl;
}
} else {
std::cout << "ERROR: can not open (load) tree." << std::endl;
}
if (fclose(f) == EOF) {
std::cout << "ERROR: can not load tree." << std::endl;
}
} else if (!strcmp(act, "Save")) {
f = fopen(path, "wb");
fclose(f);
f = fopen(path, "ab");
if (f) {
char psswd[7] = "MYFILE";
fwrite(&psswd, 7 * sizeof(char), 1, f);
rewind(f);
Serialise(root, f);
std::cout << "OK" << std::endl;
} else std::cout << "ERROR: can not save tree." << std::endl;
if (fclose(f) == EOF) {
std::cout << "ERROR: can not save tree." << std::endl;
}
} else std::cout << "ERROR: unknown command." << std::endl;
} else {
ungetc(action, stdin);
char str[256];
std::cin >> str;
std::transform(str, str + strlen(str), str, (int (*)(int))std::tolower);
Find(str, root);
}
}
if (root) {
root = DeleteTree(root);
}
return 0;
}