-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaircrafts.c
593 lines (514 loc) · 17.3 KB
/
aircrafts.c
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define true 1
#define false 0
typedef struct TReceiver
{
int ID;
int flightDataMemory;
int flightDataSum;
int flightDataLength;
int * flightData;
} Receiver;
typedef enum
{ RED,
BLACK
} Color;
typedef struct TNode
{
Receiver receiver;
Color color;
struct TNode * right;
struct TNode * left;
struct TNode * parent;
} Node;
typedef struct TRedBlackTree
{
Node * root;
} RedBlackTree;
/*---------------------------------------Input part----------------------------------------*/
/*-----------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------------*/
/* A function reads the number (ID) from the stdin.
*
* Returns:
* EOF - The end of reading data
* 0 - An incorrect character
* 1 - OK
*/
int getID(int * ID)
{
int res;
if ((res = scanf(" %d", ID)) != 1 || *ID < 0)
{
if (res == EOF)
return EOF;
else
return 0;
}
return 1;
}
/*-----------------------------------------------------------------------------------------*/
/* A function reallocs the memory of an array (int), given as first parameter.
* Function returns:
* a pointer to newly allocated array of integers
*/
int * reallocMemory(int * flightData, int ** maxMemory)
{
//Increase memory limit
**maxMemory += ( **maxMemory < 100 ) ? 10 : **maxMemory / 2;
flightData = (int*) realloc(flightData, **maxMemory * sizeof(int));
return flightData;
}
/*-----------------------------------------------------------------------------------------*/
/* A function reads the flight data from the stdin.
* Example: [ 10,6,1,-3 , 9, 10]
*
* Params:
* flightDataSum - the sum of identification messages
* flightDataLength - a length of an array (flight data)
* flightDataMemory - an own memory limit of dynamically allocated array
*
* Returns:
* a pointer to array of integers
*/
int * getFlightData(int * flightDataSum, int * flightDataLength, int * flightDataMemory)
{
int * flightData = NULL;
int message;
char c;
if (scanf(" %c", &c) != 1 || c != '[')
{
return NULL;
}
while (true)
{
if (scanf(" %d %c", &message, &c) != 2 || (c != ',' && c != ']'))
{
if (*flightDataLength != 0)
free(flightData);
return NULL;
}
if (*flightDataLength >= *flightDataMemory)
flightData = reallocMemory(flightData, &flightDataMemory);
flightData[*flightDataLength] = message;
*flightDataSum += message;
(*flightDataLength)++;
if (c == ']')
break;
}
return flightData;
}
/*-----------------------------------------------------------------------------------------*/
/* A function reads the receiver's data from stdin.
* Example: 1689 : [50, 60, -10, 93 , 84 , 0, -2 ]
*
* Params:
* receiver - the data will be assigned to this receiver
* Returns:
* EOF
* 0 - An incorrect character
* 1 - OK
*/
int getReceiverData(Receiver * receiver)
{
int ID, res, flightDataSum = 0, flightDataLength = 0, flightDataMemory = 0;
int * flightData = NULL;
char c;
//Read the receiver's ID
if ((res = getID(&ID)) == EOF)
return EOF;
else if (res == 0)
return 0;
//Read the separator ':'
if (scanf(" %c", &c) != 1 || c != ':')
return 0;
//Read the flight data
if ((flightData = getFlightData(&flightDataSum, &flightDataLength, &flightDataMemory)) == NULL)
return 0;
receiver->ID = ID;
receiver->flightData = flightData;
receiver->flightDataLength = flightDataLength;
receiver->flightDataSum = flightDataSum;
receiver->flightDataMemory = flightDataMemory;
return 1;
}
/*-----------------------------------Red Black Tree part-----------------------------------*/
/*-----------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------------*/
/* Initialize the red black tree
*/
void initTree(RedBlackTree * tree)
{
tree->root = NULL;
}
/*-----------------------------------------------------------------------------------------*/
/* A function recursively searches for the receiver by the ID in the red black tree.
*
* Params:
* node - the root of tree
* ID - the receiver's ID
*
* Returns:
* NULL - a receiver with given ID does not exist
* node - a node which contains the receiver with given ID
*/
Node * searchForReceiver(Node * node, int ID)
{
if (node == NULL)
return NULL;
if (ID > node->receiver.ID)
return searchForReceiver(node->right, ID);
else if (ID < node->receiver.ID)
return searchForReceiver(node->left, ID);
else
return node;
}
/*--------------------------------------Insertion part-------------------------------------*/
/*-----------------------------------------------------------------------------------------*/
Node * createRedNode(Node * parent, Receiver receiver)
{
Node * node = (Node*) malloc(sizeof(Node));
node->receiver = receiver;
node->color = RED;
node->parent = parent;
node->left = NULL;
node->right = NULL;
return node;
}
/*-----------------------------------------------------------------------------------------*/
Node * createBlackNode(Receiver receiver)
{
Node * node = (Node*) malloc(sizeof(Node));
node->receiver = receiver;
node->parent = NULL;
node->color = BLACK;
node->left = NULL;
node->right = NULL;
return node;
}
/*-----------------------------------------------------------------------------------------*/
int isLeftChild(Node * root)
{
if (root == root->parent->left)
return true;
else
return false;
}
/*-----------------------------------------------------------------------------------------*/
Node * getSiblingNode(Node * root)
{
if (isLeftChild(root))
return root->parent->right;
else
return root->parent->left;
}
/*-----------------------------------------------------------------------------------------*/
void rightRotate(Node ** root, int changeColor)
{
Node * parent = (*root)->parent;
(*root)->parent = parent->parent;
if (parent->parent != NULL)
{
if (parent->parent->right == parent)
parent->parent->right = *root;
else
parent->parent->left = *root;
}
Node * right = (*root)->right;
(*root)->right = parent;
parent->parent = *root;
parent->left = right;
if (right != NULL)
right->parent = parent;
if (changeColor)
{
(*root)->color = BLACK;
parent->color = RED;
}
}
/*-----------------------------------------------------------------------------------------*/
void leftRotate(Node ** root, int changeColor)
{
Node * parent = (*root)->parent;
(*root)->parent = parent->parent;
if (parent->parent != NULL)
{
if (parent->parent->right == parent)
parent->parent->right = *root;
else
parent->parent->left = *root;
}
Node * left = (*root)->left;
(*root)->left = parent;
parent->parent = *root;
parent->right = left;
if (left != NULL)
left->parent = parent;
if (changeColor)
{
(*root)->color = BLACK;
parent->color = RED;
}
}
/*-----------------------------------------------------------------------------------------*/
/* A funtion recursively insterts the receiver to red black tree.
*/
Node * insert(Node * parent, Node * root, Receiver receiver)
{
if (root == NULL)
{
//If the parent exist the tree is not empty -> create a read leaf node
if (parent != NULL)
return createRedNode(parent, receiver);
//Otherwise the tree is empty -> create a black root node
else
return createBlackNode(receiver);
}
//If we go to the left node -> isLeft will be true, else false
int isLeft;
if (receiver.ID < root->receiver.ID )
{
Node * left = insert(root, root->left, receiver);
/* If true = A rotation has happened at lower level and "left" is now a parent of "root" so just return "left" node
* so nodes at upper level can set their child correctly.
*/
if (left == root->parent)
return left;
//Set left node to root's left node in tree
root->left = left;
isLeft = true;
}
else
{
Node * right = insert(root, root->right, receiver);
//Same case as above, just another direction
if (right == root->parent)
return right;
root->right = right;
isLeft = false;
}
if (isLeft)
{
//Chceck the Red-Red conflict on left side between root and its child
if (root->color == RED && root->left->color == RED)
{
//Get the sibling of the root
Node * sibling = getSiblingNode(root);
//A sibling does not exist or sibling's color is black
if (sibling == NULL || sibling->color == BLACK)
{
//Check if root is left child of its parent
if (isLeftChild(root))
//Left-Left case -> one right rotation
rightRotate(&root, true);
/* Root is not a left child:
* Left-Right case -> one right rotation (without changing a color) and then left rotation.
*/
else
{
//Right rotation
rightRotate(&root->left, false);
/* A root after the right rotation becomes right child of its left child
* so update this root.
*/
root = root->parent;
//Left rotation
leftRotate(&root, true);
}
}
/* A sibling does exist and its color is red -> change color of the root and its
* sibling to black and if their parent is not the root of the tree then change its
* color to red.
*/
else
{
root->color = BLACK;
sibling->color = BLACK;
if (root->parent->parent != NULL)
root->parent->color = RED;
}
}
}
//A mirror case of above.
else
{
if (root->color == RED && root->right->color == RED)
{
Node * sibling = getSiblingNode(root);
if (sibling == NULL || sibling->color == BLACK)
{
if (!isLeftChild(root))
leftRotate(&root, true);
else
{
leftRotate(&root->right, false);
root = root->parent;
rightRotate(&root, true);
}
}
else
{
root->color = BLACK;
sibling->color = BLACK;
if (root->parent->parent != NULL)
root->parent->color = RED;
}
}
}
return root;
}
void insertToTree(RedBlackTree ** tree, Receiver receiver)
{
(*tree)->root = insert(NULL, (*tree)->root, receiver);
}
/*-----------------------------------------------------------------------------------------*/
/* A function copies the array (flight data) from the receiver to the end of the receiver's array in node.
*/
void copyFlightData(Node * node, Receiver receiver)
{
//Check if the sum of array's length in node's receiver and in receiver is greater than the memory limit of an array in node's receiver
if (node->receiver.flightDataMemory <= node->receiver.flightDataLength + receiver.flightDataLength)
{
//Increase own memory limit
node->receiver.flightDataMemory += receiver.flightDataLength + node->receiver.flightDataMemory / 2;
//Ralloc
node->receiver.flightData = (int*) realloc (node->receiver.flightData, node->receiver.flightDataMemory * sizeof(int));
}
memcpy(node->receiver.flightData + node->receiver.flightDataLength, receiver.flightData, receiver.flightDataLength * sizeof(int));
node->receiver.flightDataLength += receiver.flightDataLength;
node->receiver.flightDataSum += receiver.flightDataSum;
free(receiver.flightData);
}
/*-----------------------------------------------------------------------------------------*/
/* A function inserts the receiver either to the new node in tree or copies receiver's flight data
* to already existing receiver(node) with same ID.
*/
void insertReceiver(RedBlackTree * tree, Receiver receiver)
{
Node * node = searchForReceiver(tree->root, receiver.ID);
//A receiver with its ID does not exist yet -> create new node (new receiver in tree)
if (node == NULL)
insertToTree(&tree, receiver);
//A receiver with its ID already exist in tree -> copy its flight data to receiver in node
else
copyFlightData(node, receiver);
}
/*-----------------------------------------------------------------------------------------*/
/* A function compares the integer arrays (flight data) with naive algorithm.
*
* Returns:
* true - the flight datas are identical
* false - the flight datas ate not identical
*/
int areIdentical(Receiver * receiver1, Receiver * receiver2)
{
if (receiver1->flightDataLength != receiver2->flightDataLength || receiver1->flightDataSum != receiver2->flightDataSum)
return false;
int j = 1;
for(int i = 0; i < receiver1->flightDataLength; ++i)
{
if (receiver1->flightData[0] == receiver2->flightData[i])
{
if (receiver1->flightData[receiver1->flightDataLength-1] == receiver2->flightData[(i + receiver1->flightDataLength - 1) % receiver1->flightDataLength])
{
j = 1;
while (j < receiver1->flightDataLength && receiver1->flightData[j] == receiver2->flightData[(i + j) % receiver2->flightDataLength])
++j;
if (j == receiver1->flightDataLength)
return true;
}
}
}
return false;
}
/*-----------------------------------------------------------------------------------------*/
/* A function recursively compares the node (receiver) passed from findDuplicate function with other
* nodes (receivers) in tree.
*
* Setting receiver's ID in function as "-1" means this receiver has been already compared and
* its flight data has been identical with the compared receiver.
*/
void compareFlightData(Node * receiverNode, Node * node)
{
if (node == NULL)
return;
compareFlightData(receiverNode, node->right);
compareFlightData(receiverNode, node->left);
//We want to avoid comparing the same nodes (receivers)
if (node == receiverNode)
return;
if (node->receiver.ID != -1)
{
//Check if the flight data in receivers are identical
if (areIdentical(&receiverNode->receiver, &node->receiver))
{
//Print the ID
printf("%d, ", node->receiver.ID);
node->receiver.ID = -1;
}
}
}
/*-----------------------------------------------------------------------------------------*/
/* A function recursively (bottom-up) passes the node (receiver) to "compareFlightData" function in which
* this node is compared with other nodes (receivers) in tree.
* After comparison the node will be freed.
*/
void findDuplicates(Node * root, Node * node)
{
if (node == NULL)
return;
findDuplicates(root, node->right);
findDuplicates(root, node->left);
if (node->receiver.ID != -1)
{
compareFlightData(node, root);
printf("%d\n", node->receiver.ID);
}
free(node->receiver.flightData);
free(node);
}
/*-----------------------------------------------------------------------------------------*/
/* A function frees the tree from memory.
*/
void destroy(Node * root)
{
if (root == NULL)
return;
destroy(root->right);
destroy(root->left);
free(root->receiver.flightData);
free(root);
}
/*-----------------------------------------------------------------------------------------*/
int main() {
Receiver receiver;
RedBlackTree tree;
int res;
initTree(&tree);
printf("Flight data:\n");
//Reading the data (receivers) until EOF
while (true)
{
//Get the receiver
res = getReceiverData(&receiver);
if (res == EOF)
break;
else if (res == 0)
{
//Error input -> free the entire tree
destroy(tree.root);
printf("Error input.\n");
return 0;
}
else
{
//OK - Insert the receiver
insertReceiver(&tree, receiver);
}
}
printf("Unique flight data:\n");
findDuplicates(tree.root, tree.root);
return 0;
}