-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcategorytree.c
executable file
·571 lines (469 loc) · 14 KB
/
categorytree.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAME_LEN 50
#define LINE_LEN 150
#define OP_LEN 40
#define MAX_OPS 3
typedef struct Node
{
char *categoryName;
int depth;
struct Node *parentNode;
struct Node *childNode;
struct Node *siblingNode;
} Node;
/* checkPointer: checks if passed void pointer has NULL as it's value.
* If pointer has value NULL, message is printed and program is immediately
* terminated. This function is used to check whether an function has failed
* or not.
*
* It takes void pointer, which was casted in the calling fuction, and string,
* represents function which has returned that pointer, as parameters */
void checkPointer( void *pointer, char *fname )
{
if( pointer == NULL )
{
fprintf( stderr, "%s: failed\n", fname );
exit(EXIT_FAILURE);
}
}
/* createNode: creates new a node
*
* It takes name of category and it's depth in the tree
* as parameters and returns pointer to that node */
Node *createNode( char *categoryName )
{
Node *newNode = (Node *) malloc( sizeof( Node ) );
checkPointer( (void *) newNode, "malloc" );
newNode->categoryName = (char *) malloc( NAME_LEN + 1 );
checkPointer( (void *) newNode->categoryName, "malloc" );
strcpy( newNode->categoryName, categoryName );
newNode->depth = 0;
newNode->parentNode = NULL;
newNode->childNode = NULL;
newNode->siblingNode = NULL;
return newNode;
}
/* addChild: adds child node to the parent node.
* If parent is NULL, child node is set as a root
*
* Function takes pointer to the parent node pointer and
* pointer to child node as parameters*/
void addChild( Node **parentNode, Node *childNode )
{
childNode->parentNode = *parentNode;
if( *parentNode == NULL)
*parentNode = childNode;
else if( (*parentNode)->childNode == NULL )
(*parentNode)->childNode = childNode;
else
{
Node *prevNode, *currNode;
prevNode = NULL;
currNode = (*parentNode)->childNode;
/* find place where node, lexicographicly, belongs */
while( currNode && strcmp( currNode->categoryName, childNode->categoryName ) <= 0 )
{
prevNode = currNode;
currNode = currNode->siblingNode;;
}
if( prevNode == NULL )
{ /* New child node should be placed as a first node in the list */
childNode->siblingNode = currNode;
(*parentNode)->childNode = childNode;
}
else
{ /* New child node should be placed between previous and current node */
childNode->siblingNode = currNode;
prevNode->siblingNode = childNode;
}
}
/* Child's depth has value 1 more then value of parent's depth */
if( childNode->parentNode != NULL )
childNode->depth = childNode->parentNode->depth + 1;
}
/* findNode: finds node which has the same categoryName as passed string
* Takes pointer to the node from which search begins and category which
* is searched as parameters, and returns pointer to that node. If node
* is not found, NULL is returned */
Node *findNode( Node *root, char *categoryName )
{
if( root == NULL )
return NULL;
if( !strcmp( root->categoryName, categoryName ) )
return root;
Node *temp;
root = root->childNode;
while( root != NULL )
{
if( !strcmp( root->categoryName, categoryName ) )
return root;
temp = findNode( root, categoryName );
if( temp != NULL )
return temp;
root = root->siblingNode;
}
return NULL;
}
/* loadData: reads the data which is contained in the file
* whose name is passed as a parameter. Returns pointer to
* the top node; it's root of the tree */
Node *loadData( char *dataFilename )
{
FILE *dataFile;
Node *head, *parentNode, *newNode;
char *currLinePosition, categoryName[NAME_LEN+1], line[LINE_LEN+1];
head = parentNode = newNode = NULL;
dataFile = fopen( dataFilename, "r" );
checkPointer( (void *) dataFile, "fopen" );
/* Add top category into the tree */
fgets( line, LINE_LEN, dataFile );
sscanf( line, "%s", categoryName );
newNode = createNode( categoryName);
addChild( &head, newNode );
currLinePosition = line + strlen( categoryName ) + 1;
/* Add direct subcategories of the top category to the tree */
while( sscanf( currLinePosition, "%s", categoryName ) == 1 )
{
newNode = createNode( categoryName );
addChild( &head, newNode );
currLinePosition += strlen( categoryName ) + 1;
}
/* Read rest of the file and add categories from it to the tree */
while( fgets( line, LINE_LEN, dataFile ) != 0 )
{
sscanf( line, "%s", categoryName );
parentNode = findNode( head, categoryName );
if( parentNode == NULL )
{
fprintf( stderr, "Error: invalid category\n");
return NULL;
}
currLinePosition = line + strlen( categoryName ) + 1;
while( sscanf( currLinePosition, "%s", categoryName ) == 1 )
{
newNode = createNode( categoryName );
addChild( &parentNode, newNode );
currLinePosition += strlen( categoryName ) + 1;
}
}
fclose( dataFile );
return head;
}
/* getParent: returns pointer to the parten node, if given node
* isn't top node (root of the tree), otherwise, it returnes NULL */
Node *getParent( Node *n )
{
if( n == NULL )
return NULL;
return n->parentNode;
}
/* getChildren: returns linked list of child nodes for given parent node,
* if that node has any child node, if not, NULL is returned */
Node *getChildren( Node *n )
{
if( n == NULL )
return NULL;
return n->childNode;
}
void DirectSupercategory( Node *tree, char (*buffers)[OP_LEN+1] )
{
Node *tempNode = findNode( tree, buffers[1] );
if( tempNode == NULL )
{
fprintf( stderr, "DirectSupercategory: given category (%s) doesn't exist\n", buffers[1] );
return;
}
tempNode = getParent( tempNode );
if( tempNode )
printf( "%s %s %s\n", buffers[0], buffers[1], tempNode->categoryName );
else
printf( "%s %s\n", buffers[0], buffers[1] );
}
void AllSupercategories( Node *tree, char (*buffers)[OP_LEN+1] )
{
Node *tempNode = findNode( tree, buffers[1] );
if( tempNode == NULL )
{
fprintf( stderr, "AllSupercategories: given category (%s) doesn't exist\n", buffers[1] );
return;
}
printf( "%s %s", buffers[0], buffers[1] );
tempNode = getParent( tempNode );
while( tempNode != NULL )
{
printf( " %s", tempNode->categoryName );
tempNode = getParent( tempNode );
}
printf( "\n" );
}
void NumberOfAllSupercategories( Node *tree, char (*buffers)[OP_LEN+1] )
{
int i = 0;
Node *tempNode = findNode( tree, buffers[1] );
if( tempNode == NULL )
{
fprintf( stderr, "NumberOfAllSupercategories: given category (%s) doesn't exist\n", buffers[1] );
return;
}
tempNode = getParent( tempNode );
while( tempNode != NULL )
{
i++;
tempNode = getParent( tempNode );
}
printf( "%s %s %d\n", buffers[0], buffers[1], i );
}
/* IsSupercategory: checks if the second category is supercategory of the first category
*
* Query syntax: IsSupercategory cat1 cat2
* Meaning: cat2 IsSupercategory (of) cat1 ? */
void IsSupercategory( Node *tree, char (*buffers)[OP_LEN+1] )
{
Node *tempNode = findNode( tree, buffers[1] );
if( tempNode == NULL )
{
fprintf( stderr, "IsSupercategory: given category (%s) doesn't exist\n", buffers[1] );
return;
}
while( tempNode != NULL )
{
if( !strcmp( tempNode->categoryName, buffers[2] ) )
break;
tempNode = getParent( tempNode );
}
if( tempNode )
printf( "%s %s %s %s\n", buffers[0], buffers[1], buffers[2], "yes" );
else
printf( "%s %s %s %s\n", buffers[0], buffers[1], buffers[2], "no" );
}
/* IsSubcategory: checks if the first category is subcategory of the second category
*
* Query syntax: IsSubcategory cat1 cat2
* Meaning: cat1 IsSubcategory (of) cat2? */
void IsSubcategory( Node *tree, char (*buffers)[OP_LEN+1] )
{
Node *tempNode = findNode( tree, buffers[2] );
if( tempNode == NULL )
{
fprintf( stderr, "IsSubcategory: given category (%s) doesn't exist\n", buffers[1] );
return;
}
/* search tree of second category name (it's subcategories ) */
tempNode = findNode( tempNode, buffers[1] );
if( tempNode )
printf( "%s %s %s %s\n", buffers[0], buffers[1], buffers[2], "yes" );
else
printf( "%s %s %s %s\n", buffers[0], buffers[1], buffers[2], "no" );
}
void ClosestCommonSupercategory( Node *tree, char (*buffers)[OP_LEN+1] )
{
Node *tempNode1, *tempNode2;
tempNode1 = findNode( tree, buffers[1] );
if( tempNode1 == NULL )
{
fprintf( stderr, "ClosestCommonSupercategory: given category (%s) doesn't exist\n", buffers[1] );
return;
}
tempNode2 = findNode( tree, buffers[2] );
if( tempNode2 == NULL )
{
fprintf( stderr, "ClosestCommonSupercategory: given category (%s) doesn't exist\n", buffers[2] );
return;
}
/* Move pointer of deeper node to the same level (depth)
* as another node, so they can move together towards
* the same "ancestor" */
while( tempNode1->depth != tempNode2->depth )
{
if( tempNode1->depth > tempNode2->depth )
{
tempNode1 = getParent( tempNode1 );
if( !tempNode1 )
break;
}
if( tempNode1->depth < tempNode2->depth )
{
tempNode2 = getParent( tempNode2 );
if( !tempNode2 )
break;
}
}
/* Move pointers until closest "ancestor" is found */
while( tempNode1 != tempNode2 )
{
tempNode1 = getParent( tempNode1 );
tempNode2 = getParent( tempNode2 );
}
printf( "%s %s %s %s\n", buffers[0], buffers[1], buffers[2], tempNode1->categoryName );
}
void DirectSubcategories( Node *tree, char (*buffers)[OP_LEN+1] )
{
Node *tempNode = findNode( tree, buffers[1] );
if( tempNode == NULL )
{
fprintf( stderr, "DirectSubcategories: given category (%s) doesn't exist\n", buffers[1] );
return;
}
printf( "%s %s", buffers[0], buffers[1] );
tempNode = getChildren( tempNode );
if( tempNode == NULL )
printf("\n");
else
{
while( tempNode != NULL )
{
printf(" %s", tempNode->categoryName );
tempNode = tempNode->siblingNode;
}
printf("\n");
}
}
/* preOrderPrint: prints category names of passed tree in pre-order */
void preOrderPrint( Node *tree )
{
if( tree == NULL )
return;
printf( " %s", tree->categoryName );
if( tree->childNode != NULL )
preOrderPrint( tree->childNode );
tree = tree->childNode;
while( tree && tree->siblingNode != NULL )
{
preOrderPrint( tree->siblingNode );
tree = tree->siblingNode;
}
}
void AllSubcategories( Node *tree, char (*buffers)[OP_LEN+1] )
{
Node *tempNode = findNode( tree, buffers[1] );
if( tempNode == NULL )
{
fprintf( stderr, "AllSubcategories: given category (%s) doesn't exist\n", buffers[1] );
return;
}
printf("%s %s", buffers[0], buffers[1] );
tempNode = tempNode->childNode;
while( tempNode )
{
preOrderPrint( tempNode );
tempNode = tempNode->siblingNode;
}
printf("\n");
}
/* countSubcategories: counts number of subcategories of passed node */
int countSubcategories( Node *tree )
{
if( tree == NULL )
return 0;
int num = 0;
Node *tempNode;
if( tree->childNode )
{
tempNode = tree->childNode ;
while( tempNode )
{
num += 1 + countSubcategories( tempNode );
tempNode = tempNode->siblingNode ;
}
}
return num;
}
void NumberOfAllSubcategories( Node *tree, char (*buffers)[OP_LEN+1] )
{
Node *tempNode = findNode( tree, buffers[1] );
if( tempNode == NULL )
{
fprintf( stderr, "NumberOfAllSubcategories: given category (%s) doesn't exist\n", buffers[1] );
return;
}
printf("%s %s %d\n", buffers[0], buffers[1], countSubcategories(tempNode) );
}
void processQueries( Node *tree, char (*buffers)[OP_LEN+1] )
{
if( !strcmp( buffers[0], "DirectSupercategory" ) )
DirectSupercategory( tree, buffers );
else if( !strcmp( buffers[0], "AllSupercategories" ) )
AllSupercategories( tree, buffers );
else if( !strcmp( buffers[0], "NumberOfAllSupercategories" ) )
NumberOfAllSupercategories( tree, buffers );
else if( !strcmp( buffers[0], "IsSupercategory" ) )
IsSupercategory( tree, buffers );
else if( !strcmp( buffers[0], "IsSubcategory" ) )
IsSubcategory( tree, buffers );
else if( !strcmp( buffers[0], "ClosestCommonSupercategory" ) )
ClosestCommonSupercategory( tree, buffers );
else if( !strcmp( buffers[0], "DirectSubcategories" ) )
DirectSubcategories( tree, buffers );
else if( !strcmp( buffers[0], "DirectSubcategories" ) )
DirectSubcategories( tree, buffers );
else if( !strcmp( buffers[0], "AllSubcategories" ) )
AllSubcategories( tree, buffers );
else if( !strcmp( buffers[0], "NumberOfAllSubcategories" ) )
NumberOfAllSubcategories( tree, buffers );
}
/* answerQueries: prints queries and answers to the screen
* Takes tree, array which contains names of the files with queries and
* how many of them are passed, as parameters*/
void answerQueries( Node *tree, char **queryFilenames, int nquery )
{
int i, j;
FILE *inputFile;
char buffers[MAX_OPS][OP_LEN+1];
char line[LINE_LEN+1];
char *currLinePosition;
for(i = 0; i < nquery; i++)
{
inputFile = fopen( queryFilenames[i], "r" );
if( !inputFile )
printf("fopen: failed\n");
while( fgets( line, LINE_LEN, inputFile ) != 0 )
{
j = 0;
/* Get operation */
if( sscanf( line, "%s", buffers[j] ) != 1 )
continue;
currLinePosition = line + strlen( buffers[j] ) + 1;
/* Get operands */
while( sscanf( currLinePosition, "%s", buffers[++j] ) == 1 )
currLinePosition += strlen( buffers[j] ) + 1;
processQueries( tree, buffers );
}
fclose( inputFile );
}
}
int main( int argc, char **argv )
{
/* Check if user passed filenames for data and, at least, one query file */
if( argc < 3 )
{
printf("Usage: categorytree data_filename query_filename [query_filename...]\n");
return EXIT_FAILURE;
}
Node *root = NULL;
char *dataFilename = argv[1];
/* Allocate memory for the array of query file names */
char **queryFilenames = (char**) malloc( (argc-2) * sizeof(char *) );
checkPointer( (void *) queryFilenames, "malloc" );
int i;
for(i = 2; i < argc; i++)
{
/* Check if filename length is greater then allowed lenght */
if( strlen( argv[i] ) > NAME_LEN )
{
fprintf( stderr, "categorytree: filename of %d. query file is too long\n", i-1 );
return EXIT_FAILURE;
}
else
{
/* Allocate memory for a name of the query file */
queryFilenames[i - 2] = (char *) malloc( NAME_LEN + 1 );
checkPointer( (void *) queryFilenames[i - 2], "malloc" );
strcpy( queryFilenames[i - 2], argv[i] );
}
}
root = loadData( dataFilename );
answerQueries( root, queryFilenames, argc-2 );
return EXIT_SUCCESS;
}