-
Notifications
You must be signed in to change notification settings - Fork 0
/
PartII_ABC_RB.cpp
1236 lines (1000 loc) · 30.4 KB
/
PartII_ABC_RB.cpp
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
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************************
* PART II.D: Consolidation of Binary Search Tree and Hashing operations *
************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define M 11 // Hash table size
// Data structures
FILE *fp; // Pointer to data file
// Binary search tree data structures
struct dateVolume // Data record stored in binary tree node / bucket list node
{
char Date[11];
int Volume;
};
typedef struct dateVolume dataItem;
struct binaryTreeNode // Binary Search Tree node implemented as RED-BLACK tree node
{
dataItem data;
struct binaryTreeNode *left;
struct binaryTreeNode *right;
struct binaryTreeNode *parent;
char color;
};
typedef struct binaryTreeNode btNode;
btNode *root = NULL; // Root of the tree initially empty
int (*cmpPtr)(dataItem, dataItem); // Pointer to compare functions
// Hashing with chaind linked lists data structures
struct listNode // Bucket list node
{
dataItem data;
struct listNode *next;
};
typedef struct listNode lNode;
lNode *hashTable[M] = {NULL}; // Hash table of M buckets initially empty
/*******************************************************************/
// Declaration of functions
void mainMenu();
// Binary search tree functions
void binaryTreeByDateMenu();
void binaryTreeByVolumeMenu();
void binaryTreeMenu();
void openFile(int argc, char *argv[]);
void readFileToBinTree();
btNode *createbtNode(dataItem x);
btNode *uncle(btNode *r);
int leftSon(btNode *r);
btNode *sibling(btNode *r);
int hasRedSon(btNode *r);
btNode *replacebtNode(btNode *r);
void rotateL(btNode *r);
void rotateR(btNode *r);
void swapColors(btNode *n1, btNode *n2) ;
void swapDataValues(btNode *n1, btNode *n2);
void fixRedRed(btNode *r);
void fixDoubleBlack(btNode *r);
btNode *searchBinTree(btNode *r, dataItem x);
void insertToBinTree(dataItem x);
void deleteFromBinTree(btNode *r);
void reportBinTree(btNode *r, int x);
btNode *minValuebtNode(btNode *r);
btNode *maxValuebtNode(btNode *r);
void inorderBinTree(btNode *r);
void printbtNodeInfo(btNode *r);
void btPathNodes(btNode *r, int *blackNodes, int *totalNodes);
void printBinTree(btNode *r, int k);
int cmpDate(dataItem a, dataItem b);
int cmpVolumeDate(dataItem a, dataItem b);
// Hashing with chained lists functions
void hashingMenu();
void readFileToHashTable();
lNode *createlNode(dataItem x);
int hashValue(char x[11]);
void insertToHashTable(dataItem x);
lNode *searchHashTable(char x[11]);
void deleteFromHashTable(char x[11]);
void displayHashTable();
/*******************************************************************/
int main(int argc, char *argv[])
{
openFile(argc, argv); // Open inputa data file before any other action
mainMenu();
return 0;
}
// Main user menu
void mainMenu()
{
int selection;
while (1)
{
printf("1. Read file to a Binary Search Tree");
printf("\n2. Read file to a Hash Table");
printf("\n\nEnter your choice (1 - 2): ");
scanf("%d",&selection);
switch (selection)
{
case 1 :
binaryTreeMenu();
return;
case 2 :
hashingMenu();
return;
default :
printf("\nWrong option, try again ...\n\n\n");
break;
}
}
}
/*******************************************************************/
// BINARY SEARCH TREE FUNCTIONS
// Binary tree menu
void binaryTreeMenu()
{
int selection;
while (1)
{
printf("\n\n1. Read file to a Binary Search Tree by Date");
printf("\n2. Read file to a Binary Search Tree by Volume");
printf("\n\nEnter your choice (1 - 2): ");
scanf("%d",&selection);
switch (selection)
{
case 1 :
binaryTreeByDateMenu();
return;
case 2 :
binaryTreeByVolumeMenu();
return;
default :
printf("\nWrong option, try again ...\n");
break;
}
}
}
// Binary tree by Date menu: Read file by Date and display user menu
void binaryTreeByDateMenu()
{
cmpPtr = &cmpDate; // The key of each data record is the field Date
readFileToBinTree();
int selection;
char x[11];
dataItem d;
btNode *r;
while (1)
{
printf("\n\n1. Inorder traversal of BST");
printf("\n2. Search volume for a given date");
printf("\n3. Modify volume for a given date");
printf("\n4. Delete BST node of a given date");
printf("\n5. Exit\n");
printf("\nEnter your choice (1 - 5): ");
scanf("%d",&selection);
switch (selection)
{
case 1 :
printf("\n\nDate Volume\tColor\tBNodes\tTNodes\n");
printf("----------------------------------------------");
inorderBinTree(root);
printf("\n\n");system("pause");
printf("\n\n\nTree structure:\n");
printf("---------------\n\n");
printBinTree(root, 1);
break;
case 2 :
printf("\n\nGive the date (yyyy-mm-dd): ");
scanf("%s", x);
strcpy(d.Date, x);
if (!root)
printf("\nTree is empty\n\n");
else
{
r = searchBinTree(root, d);
if ((*cmpPtr)(d, r->data) != 0)
printf("\nThis date does not exist in the tree\n");
else
printf("\nVolume for the given date is: %d\n", r->data.Volume);
}
break;
case 3 :
printf("\n\nGive the date (yyyy-mm-dd): ");
scanf("%s", x);
strcpy(d.Date, x);
if (!root)
printf("\nTree is empty\n\n");
else
{
r = searchBinTree(root, d);
if ((*cmpPtr)(d, r->data) != 0)
printf("\nThis date does not exist in the tree\n");
else
{
printf("\nCurrent record: %s | %d", r->data.Date, r->data.Volume);
printf("\n\nGive the new volume (>= 0): ");
scanf("%d", &r->data.Volume);
printf("\nVolume modified\n");
}
}
break;
case 4 :
printf("\n\nGive the date (yyyy-mm-dd): ");
scanf("%s", x);
strcpy(d.Date, x);
if (!root)
printf("\nTree is empty\n\n");
else
{
r = searchBinTree(root, d);
if ((*cmpPtr)(d, r->data) != 0)
printf("\nThis date does not exist in the tree\n");
else
{
deleteFromBinTree(r);
printf("\n\nDate found and deleted\n");
}
}
break;
case 5 :
return;
default :
printf("\nWrong option, try again ...\n");
break;
}
}
}
// Binary tree by Volume menu: Read file by Volume and display user menu
void binaryTreeByVolumeMenu()
{
cmpPtr = &cmpVolumeDate; // The key of each data record is the pair (Volume, Date)
readFileToBinTree();
int selection, v;
while (1)
{
printf("\n\n1. Find date(s) with MIN volume");
printf("\n2. Find date(s) with MAX volume");
printf("\n\nEnter your choice (1 - 2): ");
scanf("%d",&selection);
switch (selection)
{
case 1 :
/* // This code is used for verification: Display tree nodes info in inorder and the tree structure
printf("\n\nDate Volume\tColor\tBNodes\tTNodes\n");
printf("----------------------------------------------");
inorderBinTree(root);
printf("\n\n");system("pause");
printf("\n\n\nTree structure:\n");
printf("---------------\n\n");
printBinTree(root, 1); */
if (!root)
printf("\n\nTree is empty\n\n");
else
{
v = minValuebtNode(root)->data.Volume;
printf("\n\nDates with MIN volume: ");
reportBinTree(root, v);
printf("\n\nMIN volume: %d\n", v);
}
return;
case 2 :
if (!root)
printf("\n\nTree is empty\n\n");
else
{
v = maxValuebtNode(root)->data.Volume;
printf("\nDates with MAX volume: ");
reportBinTree(root, v);
printf("\nMAX volume: %d\n", v);
}
return;
default :
printf("\nWrong option, try again ...\n");
break;
}
}
}
// Open the input data file
void openFile(int argc, char *argv[])
{
char *fileName;
if (argc >= 2) // Data filename passed as a command line argument
fileName = strdup(argv[1]);
else
{
printf("Give the stock data filename: "); // Data filename asked by user
scanf("%ms", &fileName);
printf("\n\n");
}
// Check if the file exists
if (access(fileName, F_OK) == -1)
{
printf("\nERROR: File '%s' not found\n", fileName);
free(fileName);
exit(1);
}
fp = fopen(fileName, "r");
if (!fp) // fp == NULL
{
printf("\nERROR: can't open file\n");
free(fileName);
exit(1);
}
}
// Read the file and store data records to a binary tree implemented as a RED-BLACK tree
void readFileToBinTree()
{
char line[80];
dataItem dt;
float a, b, c, d;
int e;
fgets(line, 80, fp); // Get the first line
while (fgets(line, 80, fp))
{
sscanf(line, "%10s,%f,%f,%f,%f,%d,%d", dt.Date, &a, &b, &c, &d, &dt.Volume, &e); // a, b, c, d and e are dummy variables
insertToBinTree(dt);
}
fclose(fp);
}
// Allocate memory to a new tree node n, set left, right and parent pointers to NULL and color it RED
btNode *createbtNode(dataItem x)
{
btNode *n = (btNode *) malloc(sizeof(btNode));
if (!n)
{
printf("\nERROR: Memory failure\n\n");
exit(1);
}
n->data = x;
n->left = n->right = n->parent = NULL;
// Node is created during insertion and its color is RED
n->color = 'R';
return n;
}
// Return pointer to uncle of node r
btNode *uncle(btNode *r)
{
// If there is no parent or grandParent, then return NULL
if (!r->parent)
return NULL;
if (!r->parent->parent)
return NULL;
if (leftSon(r->parent))
// Uncle on the right
return r->parent->parent->right;
else
// Uncle on the left
return r->parent->parent->left;
}
// Check if node r is left son of its parent
int leftSon(btNode *r)
{
return r == r->parent->left;
}
// Returns pointer to sibling of node r
btNode *sibling(btNode *r)
{
// If r has no parent then sibling = NULL
if (!r->parent)
return NULL;
if (leftSon(r))
// Sibling on the right
return r->parent->right;
else
// Sibling on the left
return r->parent->left;
}
// Return 1 if node r has RED son or 0 otherwise
int hasRedSon(btNode *r)
{
if (r->left && r->left->color == 'R')
return 1;
if (r->right && r->right->color == 'R')
return 1;
return 0;
}
// Rotate right the subtree rooted at T1
void rotateR(btNode *T1)
{
btNode *T2 = T1->left;
btNode *T3 = T2->right;
// Perform rotation
T2->right = T1;
T1->left = T3;
// Update root and parent nodes
if (T1 == root)
{
root = T2;
root->parent = NULL;
}
else
{
T2->parent = T1->parent;
if (leftSon(T1))
T2->parent->left = T2;
else
T2->parent->right = T2;
}
T1->parent = T2;
if (T3)
T3->parent = T1;
}
// Rotate left the subtree rooted at T1
void rotateL(btNode *T1)
{
btNode *T2 = T1->right;
btNode *T3 = T2->left;
// Perform rotation
T2->left = T1;
T1->right = T3;
// Update root and parent nodes
if (T1 == root)
{
root = T2;
root->parent = NULL;
}
else
{
T2->parent = T1->parent;
if (leftSon(T1))
T2->parent->left = T2;
else
T2->parent->right = T2;
}
T1->parent = T2;
if (T3)
T3->parent = T1;
}
// Utility function
void swapColors(btNode *n1, btNode *n2)
{
char t;
t = n1->color;
n1->color = n2->color;
n2->color = t;
}
// Utility function
void swapDataValues(btNode *n1, btNode *n2)
{
dataItem t;
t = n1->data;
n1->data = n2->data;
n2->data = t;
}
// Locate tree node that replaces the deleted node r
btNode *replacebtNode(btNode *r)
{
// Node with two sons: Return the inorder successor (node with minimum value in the right subtree)
if (r->left && r->right)
return minValuebtNode(r->right);
// Leaf case
if (!r->left && !r->right)
return NULL;
// One son case
if (r->left)
return r->left;
else
return r->right;
}
// Search for a given date x in a non-empty tree rooted at r
// If found return the node (used in delete) else return the last node while traversing (used in insert)
btNode *searchBinTree(btNode *r, dataItem x)
{
if ((*cmpPtr)(r->data, x) == 0) // The key of each data record is the Date field (Exercise II.A) or the pair (Volume, Date) (Exercise II.B)
return r;
if ((*cmpPtr)(r->data, x) > 0)
{
if (!r->left)
return r;
else
return searchBinTree(r->left, x);
}
if ((*cmpPtr)(r->data, x) < 0)
{
if (!r->right)
return r;
else
return searchBinTree(r->right, x);
}
return nullptr;
}
// Insert a data record x in the tree
void insertToBinTree(dataItem x)
{
btNode *newNode = createbtNode(x);
if (!root)
{
// New root becomes BLACK
newNode->color = 'B';
root = newNode;
}
else
{
btNode *t = searchBinTree(root, x);
if ((*cmpPtr)(t->data, x) == 0) // The key of each data record is the Date field (Exercise II.A) or the pair (Volume, Date) (Exercise II.B)
return; // Duplicates are not allowed in a RED-BLACK tree. Actually, as the Date value of each data record is unique, we have no duplicates
// Connect new node to the right node
newNode->parent = t;
if ((*cmpPtr)(t->data, x) > 0)
t->left = newNode;
else
t->right = newNode;
// Fix RED RED voilation if exists
fixRedRed(newNode);
}
}
// Delete node r from the tree
void deleteFromBinTree(btNode *r)
{
btNode *p = replacebtNode(r); // p is the son that replaces the deleted node r
btNode *parent = r->parent;
if (!p)
{
// p is NULL therefore r is a leaf
if (r == root) // After deletion the tree becomes empty
root = NULL;
else
{
if (r->color == 'B')
// p and r are both BLACK (color of NULL is considered as BLACK), fix DOUBLE BLACK at r
fixDoubleBlack(r);
// Delete r from the tree
if (leftSon(r))
parent->left = NULL;
else
parent->right = NULL;
}
free(r);
return;
}
if (!r->left || !r->right) // r has only one son, p is the son of r and it is a RED leaf
{
if (r == root)
{
// r is the root, assign the data record of p to r and delete leaf p
r->data = p->data;
r->left = r->right = NULL;
free(p);
}
else
{
// Detach r from the tree and move p up
if (leftSon(r))
parent->left = p;
else
parent->right = p;
free(r);
p->parent = parent;
// Color p BLACK
p->color = 'B';
}
return;
}
// r has 2 sons, swap values with successor and recurse
swapDataValues(p, r);
deleteFromBinTree(p);
}
// Fix RED RED violation at given node r
void fixRedRed(btNode *r)
{
// 1. If r is the root, color it BLACK and return
if (r == root)
{
r->color = 'B';
return;
}
// Initialize parent, grandParent, uncle of r
btNode *parent = r->parent, *grandParent = parent->parent, *un = uncle(r);
if (parent->color == 'R')
{
if (un && un->color == 'R')
{
// 2. Uncle RED, perform color flip and recurse for grandParent of r
parent->color = 'B';
un->color = 'B';
grandParent->color = 'R';
fixRedRed(grandParent);
}
else
{
// 3. Uncle BLACK (color of NULL is considered as BLACK). There are 4 Cases for nodes r, parent and grandParent of r
if (leftSon(parent))
{
if (leftSon(r))
{
// Left Left Case - Swap colors and Rotate right
swapColors(parent, grandParent);
rotateR(grandParent);
}
else
{
// Left Right Case - Swap colors and Double rotate (Rotate left and then rotate right)
swapColors(r, grandParent);
rotateL(parent);
rotateR(grandParent);
}
}
else
{
if (leftSon(r))
{
// Right Left Case - Swap colors and Double rotate (Rotate right and then rotate left)
swapColors(r, grandParent);
rotateR(parent);
rotateL(grandParent);
}
else
{
// Right Right Case - Swap colors and Rotate left
swapColors(parent, grandParent);
rotateL(grandParent);
}
}
}
}
}
// Fix DOUBLE BLACK violation at given node r
void fixDoubleBlack(btNode *r)
{
if (r == root)
// Reached root
return;
btNode *sibl = sibling(r), *parent = r->parent;
if (!sibl)
// 1. No sibling, DOUBLE BLACK pushed up
fixDoubleBlack(parent);
else
{
// 2. Sibling RED
if (sibl->color == 'R') // There are 2 Cases when sibling of r is RED
{
// Swap colors and Rotate
swapColors(sibl, parent);
if (leftSon(sibl))
// left Case - Rotate right
rotateR(parent);
else
// Right Case - Rotate left
rotateL(parent);
fixDoubleBlack(r);
}
else
{
// 3. Sibling BLACK with at least one RED son
if (hasRedSon(sibl)) // There are 4 Cases when BLACK sibling has at least one RED son
{
if (sibl->left && sibl->left->color == 'R')
{
if (leftSon(sibl))
{
// Left Left Case - Update colors and Rotate right
sibl->left->color = sibl->color;
sibl->color = parent->color;
rotateR(parent);
}
else
{
// Right Left Case - Update color and Double rotate (Rotate right and then Rotate left)
sibl->left->color = parent->color;
rotateR(sibl);
rotateL(parent);
}
}
else
{
if (leftSon(sibl))
{
// Left Right Case - Update color and Double rotate (Rotate left and then Rotate right)
sibl->right->color = parent->color;
rotateL(sibl);
rotateR(parent);
}
else
{
// Right Right Case - Update colors and Rotate left
sibl->right->color = sibl->color;
sibl->color = parent->color;
rotateL(parent);
}
}
parent->color = 'B';
}
else
{
// 4. BLACK sibling has two BLACK sons: Perform color flip and recurse for parent of r if it is BLACK
sibl->color = 'R';
if (parent->color == 'B')
fixDoubleBlack(parent);
else
parent->color = 'B';
}
}
}
}
// Report the date(s) with Volume value == x
void reportBinTree(btNode *r, int x)
{
if (r)
{
if (r->data.Volume == x) // x can be stored in many tree nodes
{
printf("%s ", r->data.Date);
reportBinTree(r->left, x);
reportBinTree(r->right, x);
}
else if (x < r->data.Volume)
reportBinTree(r->left, x);
else
reportBinTree(r->right, x);
}
}
// Given a non-empty binary tree rooted at r, return the node with minimum value stored in the tree
btNode *minValuebtNode(btNode *r)
{
btNode *p = r;
// Loop down to find the leftmost node
while (p->left)
p = p->left;
return p;
}
/* // Given a non-empty binary tree rooted at r, return the node with minimum value stored in the tree
btNode *minValuebtNode(btNode *r)
{
if (r->left)
return minValuebtNode(r->left);
else
return r;
} */
// Given a non-empty binary tree rooted at r, return the node with maximun value stored in the tree
btNode *maxValuebtNode(btNode *r)
{
btNode *p = r;
// Loop down to find the rightmost node
while (p->right)
p = p->right;
return p;
}
/* // Given a non-empty binary tree rooted at r, return the node with maximum value stored in the tree
btNode *maxValuebtNode(btNode *r)
{
if (r->right)
return maxValuebtNode(r->right);
else
return r;
} */
// Inorder traversal of the tree rooted at r
void inorderBinTree(btNode *r)
{
if (r)
{
inorderBinTree(r->left);
printbtNodeInfo(r);
inorderBinTree(r->right);
}
}
// Print tree node info
void printbtNodeInfo(btNode *r)
{
// If node r has one son or is a leaf, print also the number of BLACK nodes and the total number of nodes on the path from r to tree root
if ((r->left == r->right) || (r->left && !r->right) || (!r->left && r->right))
{
int blackNodes, totalNodes;
btPathNodes(r, &blackNodes, &totalNodes);
printf("\n%s %d\t%c\t%d\t%d", r->data.Date, r->data.Volume, r->color, blackNodes, totalNodes);
}
else
printf("\n%s %d\t%c\t-\t-", r->data.Date, r->data.Volume, r->color);
}
// Compute the number of BLACK nodes / total nodes on the path from leaf r to tree root
void btPathNodes(btNode *r, int *blackNodes, int *totalNodes)
{
btNode *p = r;
*blackNodes = *totalNodes = 0;
while (p)
{
(*totalNodes)++;
if (p->color == 'B')
(*blackNodes)++;
p = p->parent;
}
}
// Print tree structure
void printBinTree(btNode *r, int k)
{
if (r)
{
printBinTree(r->right, k+1);
for (int i = 0; i < k; i++)
printf(" ");
printf("%d (%c)\n", r->data.Volume, r->color);
printBinTree(r->left, k+1);
}
}
// Utility function to compare Date fields
int cmpDate(dataItem a, dataItem b)
{
return strcmp(a.Date, b.Date);
}
// Utility function to compare pairs (Volume, Date)
int cmpVolumeDate(dataItem a, dataItem b)
{
if (a.Volume > b.Volume)
return 1;
if (a.Volume < b.Volume)
return -1;
if (strcmp(a.Date, b.Date) > 0)
return 1;
if (strcmp(a.Date, b.Date) < 0)
return -1;
return 0;
}
/*******************************************************************/