-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
831 lines (753 loc) · 23.2 KB
/
main.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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DEFAULT_STRING_LENGTH 35
#define RED 0
#define BLACK 1
struct str_node {
char str[DEFAULT_STRING_LENGTH];
unsigned char size;
};
struct rb_node {
char *key;
unsigned int size;
int color;
struct rb_node *nested;
struct rb_node *parent;
struct rb_node *left;
struct rb_node *right;
};
/*
* Global variables
*/
struct rb_node leaf;
struct rb_node *t_nil;
int first_print;
int need_report_update;
struct rb_node *ent_rb_root; /* Store all entities. */
struct rb_node *rel_rb_root; /* Store all relationships. */
struct rb_node *report_rb_root; /* Store report. */
struct str_node tokens[4]; /* Store input lines command and strings
* 0: command, 1: id_orig, 2: id_dest, 3: id_rel.
*/
/*
* Default functions
*/
void addent(char *id_ent);
void delent(char *id_ent);
void addrel(char *id_orig, char *id_dest, char *id_rel);
void delrel(char *id_orig, char *id_dest, char *id_rel);
void report();
/*
* Utility functions
*/
void build_report(struct rb_node *rb_root);
void build_report_nested(struct rb_node *node_rep, struct rb_node *node_dest);
void update_report(struct rb_node *node_rep, char *id_dest, unsigned int size);
void rb_delete_ent_from_rel(struct rb_node *curr_rb_root, char *id_ent);
void rb_delete_ent_from_rel_nested(struct rb_node *curr_rb_root, char *id_ent);
void print_report(struct rb_node *rb_root);
void print_report_nested(struct rb_node *rb_root);
void fputui(unsigned int num);
/*
* Input functions
*/
int readline();
/*
* RB Tree functions
*/
int rb_insert(struct rb_node **rb_root, struct rb_node *new);
void rb_insert_fixup(struct rb_node **rb_root, struct rb_node *z);
struct rb_node *rb_delete(struct rb_node **rb_root, struct rb_node *node);
void rb_delete_fixup(struct rb_node **rb_root, struct rb_node *x);
struct rb_node *rb_search(struct rb_node *rb_root, char *key);
void rotate_left(struct rb_node **rb_root, struct rb_node *x);
void rotate_right(struct rb_node **rb_root, struct rb_node *x);
struct rb_node *tree_minimum(struct rb_node *node);
struct rb_node *tree_successor(struct rb_node *node);
struct rb_node *rb_create_insert_node(struct rb_node **rb_root, char *key);
void rb_free(struct rb_node **rb_root, int clear_id_ent);
int main(void)
{
char *id_ent = NULL;
char *id_rel = NULL;
char *tmp_id_orig = NULL;
char *tmp_id_dest = NULL;
/* Initialize global vars */
t_nil = &leaf;
t_nil->color = BLACK; /* Set t_nil (leaf) color to BLACK */
first_print = 1;
need_report_update = 0;
ent_rb_root = t_nil;
rel_rb_root = t_nil;
rel_rb_root->nested = t_nil;
rel_rb_root->nested->nested = t_nil;
report_rb_root = t_nil;
for (unsigned int i = 0; i < 4; i++)
tokens[i].size = 0;
do {
readline();
if (strcmp(tokens[0].str, "addent") == 0) {
id_ent = malloc(sizeof(char) * tokens[1].size);
strcpy(id_ent, tokens[1].str);
addent(id_ent);
} else if (strcmp(tokens[0].str, "delent") == 0) {
delent(tokens[1].str);
} else if (strcmp(tokens[0].str, "addrel") == 0) {
id_rel = malloc(sizeof(char) * tokens[3].size);
strcpy(id_rel, tokens[3].str);
/* Checking that the entities of the relationship are monitored. */
tmp_id_orig = rb_search(ent_rb_root, tokens[1].str)->key;
tmp_id_dest = rb_search(ent_rb_root, tokens[2].str)->key;
if (tmp_id_orig != NULL && tmp_id_dest != NULL) {
addrel(tmp_id_orig, tmp_id_dest, id_rel);
} else {
free(id_rel);
}
} else if (strcmp(tokens[0].str, "delrel") == 0) {
delrel(tokens[1].str, tokens[2].str, tokens[3].str);
} else if (strcmp(tokens[0].str, "report") == 0) {
report();
}
} while (strcmp(tokens[0].str, "end") != 0);
rb_free(&ent_rb_root, 1);
rb_free(&rel_rb_root, 1);
rb_free(&report_rb_root, 0);
return 0;
}
/*
* Adds an entity to the entity tree.
* Entity names are unique.
*
* @param id_ent name of the entity to be added
*/
void addent(char *id_ent)
{
if (rb_create_insert_node(&ent_rb_root, id_ent) == t_nil)
free(id_ent); /* No entry, key already existent */
}
/*
* Deletes an entity from the entity tree.
* If an entity is deleted, a full report update is required
* and the need_report_update flag is set.
*
* @param id_ent name of the entity to be deleted
*/
void delent(char *id_ent)
{
struct rb_node *node_tmp;
if (rel_rb_root != t_nil) {
rb_delete_ent_from_rel(rel_rb_root, id_ent);
need_report_update = 1; /* Setting flag for report update */
}
node_tmp = rb_search(ent_rb_root, id_ent);
if (node_tmp != t_nil) {
free(node_tmp->key);
node_tmp = rb_delete(&ent_rb_root, node_tmp);
free(node_tmp);
}
}
/*
* Adds a relation in the relationship tree.
* If the node relation is not existent, a new one is created.
* If a relation is added, the report tree gets updated.
*
* @param id_orig name of the origin entity
* @param id_dest name of the destination entity
* @param id_rel name of the relationship to be added
*/
void addrel(char *id_orig, char *id_dest, char *id_rel)
{
struct rb_node *node_ent;
struct rb_node *node_rel;
struct rb_node *node_rep;
node_rel = rb_search(rel_rb_root, id_rel);
if (node_rel == t_nil) {
node_rel = rb_create_insert_node(&rel_rb_root, id_rel);
/* The relation did not exist before. Have to create rb_dest and rb_orig. */
node_ent = rb_create_insert_node(&node_rel->nested, id_dest);
++node_ent->size;
rb_create_insert_node(&node_rel->nested->nested, id_orig);
if (need_report_update == 0) {
/* Report update */
node_rep = rb_create_insert_node(&report_rb_root, node_rel->key);
node_rep->size = node_ent->size;
rb_create_insert_node(&node_rep->nested, node_ent->key);
}
} else {
/*
* The relation already exists.
* Checking if rb_dest exists, otherwise a new one is created.
*/
free(id_rel); /* No more needed, relationship already exists. */
node_ent = rb_search(node_rel->nested, id_dest);
if (node_ent == t_nil) {
/* Inserting new node for entity id_dest and increasing rb_orig size. */
node_ent = rb_create_insert_node(&node_rel->nested, id_dest);
++node_ent->size;
/* Since there is no rb_dest, the node in rb_orig will not be present, so it must be created. */
rb_create_insert_node(&node_ent->nested, id_orig);
if (need_report_update == 0) {
/* Report update */
node_rep = rb_search(report_rb_root, node_rel->key);
update_report(node_rep, node_ent->key, node_ent->size);
}
} else {
/*
* Checking if the id_orig node exists:
* - if it does not exist, it is added to the rb_orig tree.
* - else, id_orig string is freed.
*/
if (rb_create_insert_node(&node_ent->nested, id_orig) != t_nil) {
++node_ent->size;
if (need_report_update == 0) {
/* Report update */
node_rep = rb_search(report_rb_root, node_rel->key);
update_report(node_rep, node_ent->key, node_ent->size);
}
}
}
}
}
/*
* Deletes a relation from the relationship tree.
* If a relation is deleted, a full report update is required
* and the need_report_update flag is set.
*
* @param id_orig name of the origin entity
* @param id_dest name of the destination entity
* @param id_rel name of the relationship to be added
*/
void delrel(char *id_orig, char *id_dest, char *id_rel)
{
struct rb_node *node_rel, *node_dest, *node_orig;
node_rel = rb_search(rel_rb_root, id_rel);
if (node_rel != t_nil) {
node_dest = rb_search(node_rel->nested, id_dest);
if (node_dest != t_nil) {
node_orig = rb_search(node_dest->nested, id_orig);
if (node_orig != t_nil) {
node_orig = rb_delete(&node_dest->nested, node_orig);
node_dest->size = node_dest->size - 1;
free(node_orig);
need_report_update = 1; /* Setting flag for report update */
/* If rb_orig tree is empty, rb_dest node is deleted */
if (node_dest->nested == t_nil) {
node_dest = rb_delete(&node_rel->nested, node_dest);
free(node_dest);
/* If rb_dest tree is empty, rb_rel node is deleted */
if (node_rel->nested == t_nil) {
free(node_rel->key);
node_rel = rb_delete(&rel_rb_root, node_rel);
free(node_rel);
}
}
}
}
}
}
/*
* Updates the report tree if the need_report_update flag is set.
* Prints the report tree.
*/
void report()
{
if (rel_rb_root == t_nil) {
fputs("none", stdout);
} else {
if (need_report_update == 1) {
rb_free(&report_rb_root, 0);
build_report(rel_rb_root);
need_report_update = 0;
}
print_report(report_rb_root);
if (first_print == 1) {
fputs("none", stdout);
}
first_print = 1;
}
fputc('\n', stdout);
}
/*
* Builds a new report tree.
* Calls a function to build nested rb_orig tree.
*
* @param rb_root root of the tree to be builded
*/
void build_report(struct rb_node *rb_root)
{
struct rb_node *node_rep;
if (rb_root == t_nil)
return;
build_report(rb_root->left);
if (rb_root != t_nil) {
node_rep = rb_create_insert_node(&report_rb_root, rb_root->key);
build_report_nested(node_rep, rb_root->nested);
}
build_report(rb_root->right);
}
/*
* Builds rb_orig report tree.
*
* @param rb_root root of the rb_orig tree to be builded
*/
void build_report_nested(struct rb_node *node_rep, struct rb_node *node_dest)
{
if (node_dest == t_nil)
return;
build_report_nested(node_rep, node_dest->right);
if (node_dest->size != 0) {
if (node_dest->size == node_rep->size) {
rb_create_insert_node(&node_rep->nested, node_dest->key);
} else if (node_dest->size > node_rep->size) {
rb_free(&node_rep->nested, 0);
rb_create_insert_node(&node_rep->nested, node_dest->key);
node_rep->size = node_dest->size;
}
}
build_report_nested(node_rep, node_dest->left);
}
/*
* Updates report tree without rebuilding the whole tree.
*
* @param node_rep node of the report tree to be updated
* @param id_dest name of the new entity destination to be added
* @param size new size of the report node
*/
void update_report(struct rb_node *node_rep, char *id_dest, unsigned int size)
{
if (size == node_rep->size) {
rb_create_insert_node(&node_rep->nested, id_dest);
} else if (size > node_rep->size) {
rb_free(&node_rep->nested, 0);
rb_create_insert_node(&node_rep->nested, id_dest);
node_rep->size = size;
}
}
/*
* Deletes an entity from relation rb_dest tree.
* Calls a function to deletes entity in the nested rb_orig tree.
*
* @param curr_rb_root node of the relation tree to be searched
* @param id_ent name of the entity to be deleted
*/
void rb_delete_ent_from_rel(struct rb_node *curr_rb_root, char *id_ent)
{
struct rb_node *node_tmp;
if (curr_rb_root == t_nil)
return;
rb_delete_ent_from_rel(curr_rb_root->left, id_ent);
rb_delete_ent_from_rel(curr_rb_root->right, id_ent);
/*
* Searches for entity in rb_dest of every relation,
* on hit, nested rb_orig are deleted.
*/
node_tmp = rb_search(curr_rb_root->nested, id_ent);
if (node_tmp != t_nil) {
rb_free(&node_tmp->nested, 0);
node_tmp = rb_delete(&curr_rb_root->nested, node_tmp);
free(node_tmp);
}
rb_delete_ent_from_rel_nested(curr_rb_root->nested, id_ent);
}
/*
* Deletes an entity in rb_orig of every rb_dest (relation) nodes,
*
* @param curr_rb_root node of the rb_dest tree to be searched
* @param id_ent name of the entity to be deleted
*/
void rb_delete_ent_from_rel_nested(struct rb_node *curr_rb_root, char *id_ent)
{
struct rb_node *node_tmp;
if (curr_rb_root == t_nil)
return;
rb_delete_ent_from_rel_nested(curr_rb_root->left, id_ent);
/* Searches for entity in rb_dest of every relation,
* on hit, the node is deleted.
*/
node_tmp = rb_search(curr_rb_root->nested, id_ent);
if (node_tmp != t_nil) {
node_tmp = rb_delete(&curr_rb_root->nested, node_tmp);
curr_rb_root->size = curr_rb_root->size - 1;
free(node_tmp);
}
rb_delete_ent_from_rel_nested(curr_rb_root->right, id_ent);
}
/*
* Prints the report tree as follows:
* id_rel1 id_ent1_1 id_ent1_2 n_rel1; id_rel2 id_ent2_1 id_ent2_2 n_rel2; ...
*
* Calls a function to print the nested rb_orig tree.
*
* id_rel name of the relation
* id_ent name of the entity receiving the relation
* n_rel number of relations received
*
* @param rb_root root of the tree to be printed
*/
void print_report(struct rb_node *rb_root)
{
if (rb_root == t_nil)
return;
print_report(rb_root->left);
if (rb_root->size != 0) {
if (first_print == 1) {
fputs(rb_root->key, stdout);
first_print = 0;
} else {
fputc(' ', stdout);
fputs(rb_root->key, stdout);
}
print_report_nested(rb_root->nested);
fputc(' ', stdout);
fputui(rb_root->size);
fputc(';', stdout);
}
print_report(rb_root->right);
}
/*
* Print the rb_orig report tree.
*
* @param rb_root root of the rb_orig tree to be printed
*/
void print_report_nested(struct rb_node *rb_root)
{
if (rb_root == t_nil)
return;
print_report_nested(rb_root->left);
fputc(' ', stdout);
fputs(rb_root->key, stdout);
print_report_nested(rb_root->right);
}
/*
* Prints the given number via fputc() method.
* This results in a faster printing method than
* the common printf().
*
* @param num number to be printed
*/
void fputui(unsigned int num)
{
if (num >= 10)
fputui(num / 10);
fputc('0' + num % 10, stdout);
}
/*
* Reads input lines from stdin.
* A single whitespace ' ' is used as string separator.
* This allows to save string sizes (for a later dynamic memory allocation)
* without calling strlen().
*/
int readline()
{
int ch, i = 0, j = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (ch == ' ') {
tokens[i].str[j++] = '\0';
tokens[i++].size = j;
j = 0;
} else {
tokens[i].str[j++] = ch;
}
}
tokens[i].str[j++] = '\0';
tokens[i].size = j;
return i;
}
/*
* Inserts a new node into the given RBT.
* @return int 0 no entry, key already existing
* 1 new entry
*/
int rb_insert(struct rb_node **rb_root, struct rb_node *new)
{
struct rb_node *x = *rb_root;
struct rb_node *y = t_nil;
while (x != t_nil) {
y = x;
if (strcmp(new->key, x->key) < 0)
x = x->left;
else if (strcmp(new->key, x->key) > 0)
x = x->right;
else
return 0; /* Avoid key duplicates */
}
new->parent = y;
if (y == t_nil)
*rb_root = new; /* Empty tree */
else if (strcmp(new->key, y->key) < 0)
y->left = new;
else
y->right = new;
new->left = t_nil;
new->right = t_nil;
new->color = RED;
rb_insert_fixup(rb_root, new);
return 1;
}
/*
* Fixes the tree so that RBT properties are preserved.
* @param rb_root
* @param z
*/
void rb_insert_fixup(struct rb_node **rb_root, struct rb_node *z)
{
struct rb_node *x, *y;
if (z == *rb_root) {
(*rb_root)->color = BLACK;
} else {
x = z->parent;
if (x->color == RED) {
if (x == x->parent->left) {
y = x->parent->right;
if (y->color == RED) {
x->color = BLACK;
y->color = BLACK;
x->parent->color = RED;
rb_insert_fixup(rb_root, x->parent);
} else {
if (z == x->right) {
z = x;
rotate_left(rb_root, z);
x = z->parent;
}
x->color = BLACK;
x->parent->color = RED;
rotate_right(rb_root, x->parent);
}
} else {
y = x->parent->left;
if (y->color == RED) {
x->color = BLACK;
y->color = BLACK;
x->parent->color = RED;
rb_insert_fixup(rb_root, x->parent);
} else {
if (z == x->left) {
z = x;
rotate_right(rb_root, z);
x = z->parent;
}
x->color = BLACK;
x->parent->color = RED;
rotate_left(rb_root, x->parent);
}
}
}
}
}
/*
* Deletes a node from the given RBT.
* @param rb_root
* @param node
* @return rb_node the deleted node
*/
struct rb_node *rb_delete(struct rb_node **rb_root, struct rb_node *node)
{
struct rb_node *x, *y;
if (node->left == t_nil || node->right == t_nil)
y = node;
else
y = tree_successor(node);
if (y->left != t_nil)
x = y->left;
else
x = y->right;
x->parent = y->parent;
if (y->parent == t_nil)
*rb_root = x;
else if (y == y->parent->left)
y->parent->left = x;
else
y->parent->right = x;
if (y != node) {
node->key = y->key;
node->size = y->size;
node->nested = y->nested;
}
if (y->color == BLACK)
rb_delete_fixup(rb_root, x);
return y;
}
/*
* Fixes the tree so that RBT properties are preserved.
* @param rb_root
* @param x
*/
void rb_delete_fixup(struct rb_node **rb_root, struct rb_node *x)
{
struct rb_node *w;
if (x->color == RED || x->parent == t_nil) {
x->color = BLACK;
} else if (x == x->parent->left) {
w = x->parent->right;
if (w->color == RED) {
w->color = BLACK;
x->parent->color = RED;
rotate_left(rb_root, x->parent);
w = x->parent->right;
}
if (w->left->color == BLACK && w->right->color == BLACK) {
w->color = RED;
rb_delete_fixup(rb_root, x->parent);
} else {
if (w->right->color == BLACK) {
w->left->color = BLACK;
w->color = RED;
rotate_right(rb_root, w);
w = x->parent->right;
}
w->color = x->parent->color;
x->parent->color = BLACK;
w->right->color = BLACK;
rotate_left(rb_root, x->parent);
}
} else {
w = x->parent->left;
if (w->color == RED) {
w->color = BLACK;
x->parent->color = RED;
rotate_right(rb_root, x->parent);
w = x->parent->left;
}
if (w->right->color == BLACK && w->left->color == BLACK) {
w->color = RED;
rb_delete_fixup(rb_root, x->parent);
} else {
if (w->left->color == BLACK) {
w->right->color = BLACK;
w->color = RED;
rotate_left(rb_root, w);
w = x->parent->left;
}
w->color = x->parent->color;
x->parent->color = BLACK;
w->left->color = BLACK;
rotate_right(rb_root, x->parent);
}
}
}
/*
* Searches the key in the given RBT.
* @param rb_root
* @param node
* @return rb_node the found node (with the corresponding key)
*/
struct rb_node *rb_search(struct rb_node *rb_root, char *key)
{
while (rb_root != t_nil) {
if (strcmp(key, rb_root->key) == 0)
return rb_root;
else if (strcmp(key, rb_root->key) < 0)
rb_root = rb_root->left;
else
rb_root = rb_root->right;
}
return t_nil;
}
/*
* Rotates node x to left.
* @param rb_root
* @param x
*/
void rotate_left(struct rb_node **rb_root, struct rb_node *x)
{
struct rb_node *y = x->right;
x->right = y->left;
if (y->left != t_nil)
y->left->parent = x;
y->parent = x->parent;
if (x->parent == t_nil)
*rb_root = y;
else if (x == x->parent->left)
x->parent->left = y;
else
x->parent->right = y;
y->left = x;
x->parent = y;
}
/*
* Rotates node x to right.
* @param rb_root
* @param x
*/
void rotate_right(struct rb_node **rb_root, struct rb_node *x)
{
struct rb_node *y = x->left;
x->left = y->right;
if (y->right != t_nil)
y->right->parent = x;
y->parent = x->parent;
if (x->parent == t_nil)
*rb_root = y;
else if (x == x->parent->right)
x->parent->right = y;
else
x->parent->left = y;
y->right = x;
x->parent = y;
}
/*
* Finds the tree minimum
* @param node
* @return struct rb_node
*/
struct rb_node *tree_minimum(struct rb_node *node)
{
while (node->left != t_nil)
node = node->left;
return node;
}
/*
* Finds the given node successor
* @param node
* @return struct rb_node
*/
struct rb_node *tree_successor(struct rb_node *node)
{
struct rb_node *y;
if (node->right != t_nil)
return tree_minimum(node->right);
y = node->parent;
while (y != t_nil && node == y->right) {
node = y;
y = y->parent;
}
return y;
}
/*
* Creates a new node with the given key and inserts it in the given RBT.
* @param rb_root
* @param key
* @return struct rb_node the created node on success, t_nil on fail
*/
struct rb_node *rb_create_insert_node(struct rb_node **rb_root, char *key)
{
struct rb_node *node_ent = malloc(sizeof(struct rb_node));
node_ent->key = key;
node_ent->size = 0;
node_ent->nested = t_nil;
if (rb_insert(rb_root, node_ent) == 1)
return node_ent;
free(node_ent);
return t_nil;
}
/*
* Frees the given tree and the nested trees. If clear_id_int flag is set
* also strings (keys) of the root tree are freed. Nested keys will not be freed.
* @param rb_root
* @param clear_id_ent set to 1 to free strings, 0 otherwise
*/
void rb_free(struct rb_node **rb_root, int clear_id_ent)
{
if (*rb_root == t_nil)
return;
rb_free(&(*rb_root)->left, clear_id_ent);
rb_free(&(*rb_root)->right, clear_id_ent);
if ((*rb_root)->nested != t_nil)
rb_free(&(*rb_root)->nested, 0);
if (clear_id_ent == 1 && (*rb_root)->key != NULL)
free((*rb_root)->key);
free(*rb_root);
*rb_root = t_nil;
}