struct flight данные об авиарейсе char flight_number char company 128

  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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
struct flight//данные об авиарейсе
{
char flight_number[8];
char company[128];
char from_airport[128];
char to_airport[128];
char data[11];
char time[6];
int all_places;
int free_places;
int balance;//баланс дерева
flight *left;//левый потомок
flight *right;//правый потомок
} *root;//корень;
struct stack_elem{
flight *vert;
char pos;
} stack[1000];
int stack_len=0;
fstream db;//файл с данными
void copy_flight(flight *&a, flight *&b)
{
strcpy(a->flight_number, b->flight_number);
strcpy(a->company, b->company);
strcpy(a->data, b->data);
strcpy(a->from_airport, b->from_airport);
strcpy(a->to_airport, b->to_airport);
strcpy(a->time, b->time);
a->all_places=b->all_places;
a->free_places=b->free_places;
}
void swap_flights(flight *&a, flight *&b)
{
flight *c=new flight;
copy_flight(c, a);
copy_flight(a, b);
copy_flight(b, c);
}
int imax(int a, int b)//возвращает большее из 2-х чисел
{
return (b<a)?a:b;
}
void SmallRightRotation(flight *&a)//малый правый поворот
{
flight *b;
flight *val_a=new flight;//для копирования данных необходимо выделить память
flight *val_b=new flight;
int h_P, h_Q, h_R;
b=a->right;
copy_flight(val_a, a);
copy_flight(val_b, b);
// val_a=a->flight_number;
// val_b=b->flight_number;
h_Q=0;
h_R=b->balance;
h_P=(imax(h_Q, h_R)+1)-a->balance;
copy_flight(a, val_b);
copy_flight(b, val_a);
// a->flight_number=val_b;
// b->flight_number=val_a;
a->right=b->right;
b->right=b->left;
b->left=a->left;
a->left=b;
b->balance=h_Q-h_P;
a->balance=h_R-(imax(h_P, h_Q)+1);
delete val_a;
delete val_b;
}
void BigRightRotation(flight *&a)//большой правый поворот
{
flight *b, *c;
flight *val_a=new flight;
flight *val_b=new flight;
flight *val_c=new flight;
int h_P, h_Q, h_R, h_S;
b=a->right;
c=b->left;
copy_flight(val_a, a);
copy_flight(val_b, b);
copy_flight(val_c, c);
// val_a=a->flight_number;
// val_b=b->flight_number;
// val_c=c->flight_number;
h_Q=0;
h_R=c->balance;
h_S=(imax(h_Q, h_R)+1)+b->balance;
h_P=1+imax(h_S, h_S-b->balance)-a->balance;
copy_flight(a, val_c);
copy_flight(c, val_a);
// a->flight_number=val_c;
// c->flight_number=val_a;
b->left=c->right;
c->right=c->left;
c->left=a->left;
a->left=c;
b->balance=h_S-h_R;
c->balance=h_Q-h_P;
a->balance=imax(h_S, h_R)-max(h_P, h_Q);
delete val_a;
delete val_b;
delete val_c;
}
void SmallLeftRotation(flight *&a)//малый левый поворот
{
swap_flights(a, a->left);
//swap(a->flight_number, a->left->flight_number);
flight* L=a->left->left;
a->left->left=a->left->right;
a->left->right=a->right;
a->right=a->left;
a->left=L;
a->balance=1+a->right->balance;
a->right->balance=-1-a->right->balance;
}
void BigLeftRotation(flight *&a)//большой левый поворот
{
swap_flights(a->left->right, a);
//swap(a->left->right->flight_number, a->flight_number);
flight* M=a->left->right->left;
a->left->right->left=a->left->right->right;
a->left->right->right=a->right;
a->right=a->left->right;
a->left->right=M;
a->balance=0;
a->left->balance=-(a->right->balance==1);
a->right->balance=(a->right->balance==-1);
}
void tree_balance(flight *&a, int &cur_balance)//балансировка дерева
{
flight *b;
if (a->balance==2)
{
b=a->right;
if (b->balance==-1)
{
BigRightRotation(a);
cur_balance=-1;
}
else
{
if (b->balance==0)
{
SmallRightRotation(a);
cur_balance=0;
}
else
{
SmallRightRotation(a);
cur_balance=-1;
}
}
}
else
{
if (a->balance==-2)
{
b=a->left;
if (b->balance==1)
{
BigLeftRotation(a);
cur_balance=-1;
}
else
{
if (b->balance==0)
{
SmallLeftRotation(a);
cur_balance=0;
}
else
{
SmallLeftRotation(a);
cur_balance=-1;
}
}
}
else
{
cur_balance=0;
}
}
}
flight* search_flight_by_number(flight *start, char k[8])//поиск авиарейса по номеру
{
if (!start) return NULL;
else
{
if (!strcmp(start->flight_number, k)) return start;
if (strcmp(start->flight_number, k)<0) return search_flight_by_number(start->right, k);
else return search_flight_by_number(start->left, k);
}
}
bool add_flight(flight *k)//добавление записи в дерево
{
flight *temp=search_flight_by_number(root, k->flight_number);
if (temp) return false;
flight *newflight, *current;
stack_len=0;
int delta=0;//для расчёта баланса
int change=0;
int newdelta=0;
if (root==NULL)//если дерево пустое, то создаём и заполняем корень
{
root=new flight;
copy_flight(root, k);
root->left=NULL;
root->right=NULL;
root->balance=0;
}
else
{
current=root;
while ((strcmp(k->flight_number, current->flight_number)>0 && current->right!=NULL) || (strcmp(k->flight_number, current->flight_number)<0 && current->left!=NULL))
{
if (strcmp(k->flight_number, current->flight_number)>0)
{
stack[stack_len].pos='r';
stack[stack_len++].vert=current;
current=current->right;
}
else
{
stack[stack_len].pos='l';
stack[stack_len++].vert=current;
current=current->left;
}
}
if (strcmp(k->flight_number, current->flight_number))
{
newflight=new flight;
copy_flight(newflight, k);
newflight->balance=0;
newflight->left=NULL;
newflight->right=NULL;
if (strcmp(k->flight_number, current->flight_number)>0)
{
stack[stack_len].vert=current;
stack[stack_len++].pos='r';
current->right=newflight;
}
else
{
stack[stack_len].vert=current;
stack[stack_len++].pos='l';
current->left=newflight;
}
delta=1;
while (delta!=0 && stack_len!=0)
{
if (stack[stack_len-1].pos=='l')
{
if (stack[stack_len-1].vert->balance==1) change=0;
else change=1;
stack[stack_len-1].vert->balance=stack[stack_len-1].vert->balance-1;
}
else
{
if (stack[stack_len-1].vert->balance==-1) change=0;
else change=1;
stack[stack_len-1].vert->balance=stack[stack_len-1].vert->balance++;
}
tree_balance(stack[stack_len-1].vert, newdelta);
delta=change+newdelta;
stack_len--;
}
}
}
return true;
}
int BoyerMooreSearch(char* text, char* pat)
{
int i, table[256];
int len=strlen(pat);
int no=0;
//int *pos=0;
int compcount=0;
for (i=0; i<256; i++) table[i]=len;
for (i=0; i<len; i++) table[pat[i]]=len-(i+1);
int ptct=len-1;
while (ptct<strlen(text))
{
int count=0;
while (count<len)
{
if (text[ptct-count]!=pat[len-1-count])
{
compcount++;
break;
}
else count++;
}
if (count==len)
{
no++;
//*(pos+(*no)-1)=(ptct-count+1);
ptct+=len;
}
else ptct+=(table[text[ptct-count]]-count);
}
return no;
}
void search_flight_by_airport(flight* start, char airport[128])
{
if (start)
{
search_flight_by_airport(start->left, airport);
search_flight_by_airport(start->right, airport);
int i=BoyerMooreSearch(start->to_airport, airport);
if (i!=0) cout<<"Flight number: "<<start->flight_number
<<" To airport : "<<start->to_airport
<<" Date: "<<start->data
<<" Time: "<<start->time<<endl;
}
}
bool delete_flight(char k[8])
{
flight *cur2, *current;
stack_len=0;
int delta=0;
int change=0;
int newdelta=0;
if (root==NULL || search_flight_by_number(root, k)==NULL) return false;
else
{
current=root;
while ((strcmp(k, current->flight_number)>0 && current->right!=NULL) || (strcmp(k, current->flight_number)<0 && current->left!=NULL))
{
if (strcmp(k, current->flight_number)>0)
{
stack[stack_len].pos='r';
stack[stack_len++].vert=current;
current=current->right;
}
else
{
stack[stack_len].pos='l';
stack[stack_len++].vert=current;
current=current->left;
}
}
if (!strcmp(k, current->flight_number))
{
if (current->left!=NULL && current->right!=NULL)
{
stack[stack_len].vert=current;
stack[stack_len++].pos='r';
cur2=current->right;
while (cur2->left!=NULL)
{
stack[stack_len].vert=cur2;
stack[stack_len++].pos='l';
cur2=cur2->left;
}
copy_flight(current, cur2);
if (stack[stack_len-1].vert!=current) stack[stack_len-1].vert->left=cur2->right;
else stack[stack_len-1].vert->right=cur2->right;
delete cur2;
}
else
{
if (current->left==NULL)
{
if (stack_len!=0)
{
if (stack[stack_len-1].pos=='l') stack[stack_len-1].vert->left=current->right;
else stack[stack_len-1].vert->right=current->right;
}
else root=current->right;
}
if (current->right==NULL)
{
if (stack_len!=0)
{
if (stack[stack_len-1].pos=='l') stack[stack_len-1].vert->left=current->left;
else stack[stack_len-1].vert->right=current->left;
}
else root=current->left;
}
delete current;
}
delta=-1;
while (delta!=0 && stack_len!=0)
{
if (stack[stack_len-1].pos=='l')
{
if (stack[stack_len-1].vert->balance==-1) change=-1;
else change=0;
stack[stack_len-1].vert->balance=stack[stack_len-1].vert->balance+1;
}
else
{
if (stack[stack_len-1].vert->balance==1) change=-1;
else change=0;
stack[stack_len-1].vert->balance=stack[stack_len-1].vert->balance-1;
}
tree_balance(stack[stack_len-1].vert, newdelta);
delta=change+newdelta;
stack_len--;
}
}
}
return true;
}
void view_all_flights(flight *start)//вывод всех авиарейсов прямым обходом
{
if (start)
{
cout<<"Flight number: "<<start->flight_number
<<" Company: "<<start->company
<<" Date: "<<start->data
<<" Time: "<<start->time
<<" Airport from: "<<start->from_airport
<<" Airport to: "<<start->to_airport
<<" Places: "<<start->all_places
<<" Free places: "<<start->free_places<<endl;
view_all_flights(start->left);
view_all_flights(start->right);
}
}
void clear_flights(flight *&start)//очистка дерева авиарейсов обратным обходом
{
if (start)
{
clear_flights(start->left);
clear_flights(start->right);
delete start;//осовбодили память
start=NULL;//обнулили указатель
}
}
bool load_flights()
{
db.clear();
db.open("flights.txt", fstream::in);//открыть
if (db.fail()) return false;//если не удалось открыть файл
flight *new_flight=new flight;
while (!db.eof())//читаем файл до конца и заполняем дерево
{
db>>new_flight->flight_number//считывание данных о gjk`nt
>>new_flight->company
>>new_flight->data
>>new_flight->time
>>new_flight->from_airport
>>new_flight->to_airport
>>new_flight->all_places
>>new_flight->free_places;
if (!add_flight(new_flight))
{
cout<<"Error! Invalid flights database!"<<endl;
db.close();
return false;
}
}
db.close();//закроем бд, чтобы не испортить данные в случае экстренного завершения работы программы
return true;
}
void save_flights(flight *start)//сохранение полётов в файл прямым обходом
{
if (start)
{
db<<endl<<start->flight_number
<<" "<<start->company
<<" "<<start->data
<<" "<<start->time
<<" "<<start->from_airport
<<" "<<start->to_airport
<<" "<<start->all_places
<<" "<<start->free_places;
save_flights(start->left);
save_flights(start->right);
}
}