-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhilbertRTreeUpdated.c
More file actions
1408 lines (1180 loc) · 41.8 KB
/
hilbertRTreeUpdated.c
File metadata and controls
1408 lines (1180 loc) · 41.8 KB
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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
//--------------------------- MACRO DEFINITIONS ----------------------------//
// EVERY NODE HAS BETWEEN m and M entries and children unless it is root
#define M 4
#define m 2
#define MAX_CHILDREN M // M = 4; MAXIMUM NUMBER OF CHILDREN/ENTRIES
#define MIN_CHILDREN m // m = 2; MINIMUM NUMBER OF CHILDREN/ENTRIES
#define MAX_POINTS 21 // SPECIFY NUMBER OF POINTS TO TAKE AS INPUT
#define MAX_NO_SIBLINGS 4
//--------------------------- STRUCTURE DEFINITIONS ----------------------------//
typedef struct Point Point;
struct Point
{
int x, y; // COORDINATES OF 2D POINT
};
typedef struct Rectangle Rectangle;
struct Rectangle
{
Point bottom_left;
Point top_right; // coordinates of the rectangle
uint64_t h; // Hilbert value of the rectangle's center
};
typedef struct LeafEntry LeafEntry;
struct LeafEntry
{
Rectangle mbr; // Minimum bounding rectangle
int obj_id; // Pointer to the object description record
};
typedef struct LeafNode LeafNode;
struct LeafNode
{
int num_entries; // NUMBER OF LEAF ENTRIES IN NODE
struct LeafEntry entries[MAX_CHILDREN]; // ARRAY OF LEAF ENTRIES
};
typedef struct NonLeafEntry NonLeafEntry;
struct NonLeafEntry
{
Rectangle mbr; // Minimum bounding rectangle
struct Node *child_ptr; // Pointer to the child node
uint64_t largest_hilbert_value; // Largest hilbert value among the data records enclosed by the MBR
};
typedef struct NonLeafNode NonLeafNode;
struct NonLeafNode
{
int num_entries; // NUMBER OF NON LEAF ENTRIES IN NODE
struct NonLeafEntry entries[MAX_CHILDREN]; // ARRAY OF NON LEAF ENTRIES
};
typedef struct Node *NODE;
struct Node
{
int is_leaf;
NODE parent_ptr;
struct NonLeafNode non_leaf_node; // Non-leaf node
struct LeafNode leaf_node; // Leaf node
};
typedef struct HilbertRTree HilbertRTree;
struct HilbertRTree
{
struct Node *root; // Root node of the tree
};
//--------------------------- GLOBAL VARIABLE DECLARATIONS ----------------------------//
int CURRENT_ID = 0;
int num_results = 0;
bool root_split = false;
NODE root1 = NULL;
NODE root2 = NULL;
Point points[MAX_POINTS];
Rectangle rectangles[MAX_POINTS];
int hilbert_curve_order = 5; //DEFAULT; GETS CALCULATED AT RUN-TIME
int max_entries = 0;
// ---------------------------- FUNCTION DECLERATIONS ------------------------------ //
HilbertRTree *new_hilbertRTree();
NODE new_node(int is_leaf);
void InsertNode(NODE parent, NODE newNode);
void preOrderTraverse_Rtree(HilbertRTree *tree);
NODE Insert(NODE root, Rectangle rectangle);
NODE ChooseLeaf(NODE n, Rectangle r, int h);
void AdjustTree(NODE N, NODE NN, NODE *S, int s_size);
NODE HandleOverFlow(NODE n, Rectangle rectangle);
void preOrderTraverse(NODE n);
void createRectangles();
int calculateOrder();
void readFile(char *filename);
void custom_rotate(uint64_t n, uint64_t *x_coord, uint64_t *y_coord, uint64_t quadrant_x, uint64_t quadrant_y);
uint64_t calculate_hilbert_value(uint64_t n, uint64_t x_coord, uint64_t y_coord);
void store_all_nonleaf_entries(NonLeafEntry *E, NODE *S, int *num_entries, int numSiblings);
void printMBR(Rectangle rect);
void store_all_leaf_entries(LeafEntry *E, int *num_entries, NODE *S, int numSiblings);
void distribute_nonleaf_entries_evenly(NODE *S, int numSiblings, NonLeafEntry *E, int *num_entries);
void adjustLHV(NODE parentNode);
void adjustMBRLHV(NODE parentNode);
void distribute_leaf_entries_evenly(NODE *S, int numSiblings, LeafEntry *E, int *num_entries);
void searchGetResults(NODE root, Rectangle rectangle, LeafEntry *results);
void print_mbr(Rectangle r);
Rectangle new_rectangle(int bottomLeft_X, int bottomLeft_y, int topRight_x, int topRight_y);
Rectangle calculateMBR(Rectangle r1, Rectangle r2);
Rectangle calculateEntryMBR(NonLeafEntry entry);
bool allNodesFull(NODE *S, int numSiblings);
bool rectangles_equal(Rectangle *rect1, Rectangle *rect2);
bool nodes_equal(NODE node1, NODE node2);
bool intersects(Rectangle r1, Rectangle r2);
LeafEntry new_leafentry(Rectangle rectangle);
LeafEntry new_leafentry(Rectangle rectangle);
NonLeafEntry new_nonleafentry(NODE newNode);
NonLeafEntry new_nonleafentry(NODE newNode);
int calculateLHV(NonLeafEntry entry);
int numberOfSiblings(NODE *S);
int find_entry_index(NODE n, Rectangle rectangle);
int compareNonLeafEntry(const void *a, const void *b);
int compare(const void *a, const void *b);
NODE *cooperatingSiblings(NODE n);
NODE findLeaf(NODE root, Rectangle rectangle);
NODE HandleOverFlowNode(NODE parentNode, NODE new_node1);
//--------------------------- FUNCTIONS TO CREATE NEW TREE AND NODE ----------------------------//
// CREATE A NEW HILBERT R TREE STRUCTURE
HilbertRTree *new_hilbertRTree()
{
HilbertRTree *tree = (HilbertRTree *)calloc(1, sizeof(HilbertRTree));
// INITIALLY THE ROOT OF TREE IS A LEAF;
tree->root = new_node(1);
return tree;
}
// CREATE A NEW NODE
// IS_LEAF = 1 IF LEAF NODE. M = MAXIMUM NUMBER OF CHILDREN THAT NODE CAN HAVE
NODE new_node(int is_leaf)
{
// ALLOCATE MEMORY
NODE node = (NODE)calloc(1, sizeof(struct Node));
// LEAF OR NON LEAF DEPENDS ON ARGUMENT
node->is_leaf = is_leaf;
node->parent_ptr = NULL;
node->leaf_node.num_entries = 0;
node->non_leaf_node.num_entries = 0;
return node;
}
// COMPUTE THE LHV OF A LEAF NODE'S PARENT NON LEAF ENTRY
int computeLeafLHV(NODE a)
{
// SET LHV TO NULL INITIALLY
int LHV = 0;
// IF CHILD POINTER NODE IS NULL
if (a == NULL)
{
return LHV;
}
for (int i = 0; i < a->leaf_node.num_entries; i++)
{
// SET LHV AS MAXMIMUM H VALUE OF LEAF ENTRIES
if (a->leaf_node.entries[i].mbr.h > LHV)
{
LHV = a->leaf_node.entries[i].mbr.h;
}
}
// RETURN LHV
return LHV;
}
// HELPER FUNCTION TO INSERT THE NEWNODE AS THE CHILD NODE OF A NONLEAF ENTRY INTO NODE PARENT
void InsertNode(NODE parent, NODE newNode)
{
// SET THE PARENT POINTER OF ADDED NODE
newNode->parent_ptr = parent;
// NON LEAF ENTRY CREATED WITH CHILD NODE AS NEWNODE
NonLeafEntry entry = new_nonleafentry(newNode);
// INSERT THE NEW NON LEAF ENTRY ACCORDING TO LHV
int i = 0;
while (i < parent->non_leaf_node.num_entries && parent->non_leaf_node.entries[i].largest_hilbert_value <= entry.largest_hilbert_value)
{
i++;
}
// SHIFT VALUES IN PARENT NODE
for (int j = parent->non_leaf_node.num_entries; j > i; j--)
{
parent->non_leaf_node.entries[j] = parent->non_leaf_node.entries[j - 1];
}
// INSERT ACCORDING TO HILBERT VALUE
parent->non_leaf_node.entries[i] = entry;
parent->non_leaf_node.num_entries++;
}
// -----------------------Functions to calculate Hilbert value-----------------------
// Function to calculate the minimum bounding rectangle that contains two given rectangles
void custom_rotate(uint64_t n, uint64_t *x_coord, uint64_t *y_coord, uint64_t quadrant_x, uint64_t quadrant_y)
{
if (quadrant_y == 0 && quadrant_x == 1)
{
*y_coord = n - *y_coord - 1;
*x_coord = n - *x_coord - 1;
uint64_t temp = *x_coord;
*x_coord = *y_coord;
*y_coord = temp;
}
if (quadrant_y == 0 && quadrant_x != 1)
{
uint64_t temp = *x_coord;
*x_coord = *y_coord;
*y_coord = temp;
}
}
uint64_t calculate_hilbert_value(uint64_t n, uint64_t x_coord, uint64_t y_coord)
{
uint64_t quadrant_x, quadrant_y, bit, hilbert_dist = 0;
// Initialize bit
bit = 1 << (n - 1);
// While loop conversion
while (bit > 0)
{
quadrant_y = (y_coord & bit) > 0;
quadrant_x = (x_coord & bit) > 0;
hilbert_dist = hilbert_dist + bit * bit * ((3 * quadrant_x) ^ quadrant_y);
custom_rotate(bit, &x_coord, &y_coord, quadrant_x, quadrant_y);
// Update bit
bit = bit / 2;
}
return hilbert_dist;
}
// ----------------------- Functions to operate on the Hilbert Tree-------------------
// INSERTION: INSERT A RECTANGLE INTO THE RTREE BY PASSING ITS ROOT NODE AND THE RECTANGLE
NODE Insert(NODE root, Rectangle rectangle)
{
// I1. GET THE SUITABLE LEAF NODE WHERE RECTANGLE SHOULD BE INSERTED
NODE leafNode = ChooseLeaf(root, rectangle, rectangle.h);
// SET THE POSSIBLE NODE UPON SPLITTING TO NULL
NODE newLeafNode = NULL;
// SET OF COOPERATING SIBLINGS AND NUMBER OF SIBLING NDOES
NODE *S = cooperatingSiblings(leafNode);
int numSiblings = numberOfSiblings(S);
// I2. INSERT R IN A LEAF NODE L: IF L HAS AN EMPTY SLOT
if (leafNode->leaf_node.num_entries < M)
{
// FIND INDEX WHERE RECTAGNGLE SHOULD BE INSERTED AS PER HILBERT VALUE
int i = 0;
while (i < leafNode->leaf_node.num_entries && leafNode->leaf_node.entries[i].mbr.h < rectangle.h)
{
i++;
}
// SHIFT ENTRIES TO ADD THE NEW LEAF ENTRY
for (int j = leafNode->leaf_node.num_entries; j > i; j--)
{
leafNode->leaf_node.entries[j] = leafNode->leaf_node.entries[j - 1];
}
// CREATE THE LEAF ENTRY WITH NEW RECTANGLE
LeafEntry entry = new_leafentry(rectangle);
// INSERT THE LEAF ENTRY INTO NODE
leafNode->leaf_node.entries[i] = entry;
leafNode->leaf_node.num_entries++;
}
// I2. INSERT R IN A LEAF NODE L; IF L IS FULL
else if (leafNode->leaf_node.num_entries >= M)
{
// INVOKE HANDLEOVERFLOW; RETURNING A NEW NODE IF SPLIT HAPPENED
newLeafNode = HandleOverFlow(leafNode, rectangle);
// IF HANDLEOVERFLOW RETURNED A NODE
if (newLeafNode)
{
// NEW NODE IS A LEAF NODE
newLeafNode->is_leaf = 1;
// I3. SET S SHOULD CONTAIN L, COOPERATING SIBLINGS AND THE NEW NODE
S[numSiblings++] = newLeafNode;
}
}
// I3. PROPOGATE CHANGES UPWARD: INVOKE ADJUSTTREE
root_split = false;
AdjustTree(leafNode, newLeafNode, S, numSiblings);
// I4. GROW TREE TALLER: IF ROOT WAS SPLIT (AND ROOT WAS THE ONLY NODE -> LEAF NODE)
if (leafNode->parent_ptr == NULL && newLeafNode != NULL)
{
// printf("NEW ROOT FORMED");
root_split = true;
root1 = leafNode;
root2 = newLeafNode;
}
// I4. GROW TREE TALLER; IF ADJUSTTREE AND NODE SPLIT PROPOGATION CAUSED THE ROOT TO SPLIT
if (root_split)
{
// FORM NEW ROOT
NODE newRoot = new_node(0);
newRoot->non_leaf_node.num_entries = 2;
// CREATE THE NEW NON LEAF ENTRIES
NonLeafEntry entry1 = new_nonleafentry(root1);
NonLeafEntry entry2 = new_nonleafentry(root2);
// ADD THE NON LEAF ENTRIES TO THE NEWLY CREATED ROOT
newRoot->non_leaf_node.entries[0] = entry1;
newRoot->non_leaf_node.entries[1] = entry2;
// SET THE PARENT POINTERS OF ROOT1 AND ROOT2
root1->parent_ptr = newRoot;
root2->parent_ptr = newRoot;
// FREE DYNAMICALLY ALLOCATTED MEMORY
free(S);
// RETURN NEW ROOT NODE
return newRoot;
}
// FREE DYNAMICALLY ALLOCATTED MEMORY
free(S);
// RETURN ROOT NODE
return root;
}
// FUNCTION TO FIND A LEAF WHERE RECTANGLE R SHOULD BE INSERTED
NODE ChooseLeaf(NODE n, Rectangle r, int h) // PARAMETERS: NODE N, RECTANGLE R, HILBERT VALUE FO CENTRE OF RECTANGLE: H
{
/* RETURNS THE LEAF NODE IN WHICH TO PLACE A NEW RECTANGE*/
// C2. LEAF CHECK: IF N IS A LEAF NODE; RETURN N
if (n->is_leaf == 1)
{
return n;
}
// INITIALISE VARIABLES TO STORE CURRENT NODE SELECTED AND MINIMUM LHV (GREATER THAN H OF RECTANGLE)
float min_LHV = n->non_leaf_node.entries[0].largest_hilbert_value;
NODE next_node = NULL;
// C3. CHOOSE SUBTREE: IF N IS A NON-LEAF NODE, CHOOSE ENTRY (R, PTR, LHV) WITH MINIMUM LHV GREATER THAN H
for (int i = 0; i < n->non_leaf_node.num_entries; i++)
{
if (n->non_leaf_node.entries[i].largest_hilbert_value >= h && n->non_leaf_node.entries[i].largest_hilbert_value <= min_LHV)
{
min_LHV = n->non_leaf_node.entries[i].largest_hilbert_value;
next_node = n->non_leaf_node.entries[i].child_ptr;
}
}
/* IF ALL CHILDREN HAVE LHV LESS THAN H */
// NOTE: NOW MIN_LHV STORES THE LARGEST LHV ENCOUNTERED YET AS ALL ENTRIES HAVE LHV LESS THAN H
if (next_node == NULL)
{
min_LHV = n->non_leaf_node.entries[0].largest_hilbert_value;
// CHOOSE THE CHILD NODE WITH LARGEST LHV (AS ALL HAVE LHV LESS THAN HILBERT VALUE OF RECTANGLE)
for (int i = 0; i < n->non_leaf_node.num_entries; i++)
{
if (n->non_leaf_node.entries[i].largest_hilbert_value >= min_LHV)
{
min_LHV = n->non_leaf_node.entries[i].largest_hilbert_value;
next_node = n->non_leaf_node.entries[i].child_ptr;
}
}
}
// C4. DESCEND UNTIL A LEAF NODE IS REACHED
return ChooseLeaf(next_node, r, h);
}
// RETURNS TRUE IF ROOT NODE WAS SPLIT
void AdjustTree(NODE N, NODE NN, NODE *S, int s_size)
{
// STOP IF ROOT LEVEL REACHED
NODE Np = N->parent_ptr;
NODE new_node = NULL;
// PARENT = NULL; ROOT LEVEL
// A1. IF ROOT LEVEL IS REACHED; STOP
if (Np == NULL)
{
// printf("ROOT LEVEL REACHED IN ADJUST TREE\n");
return;
}
// INSERT SPLIT NODE INTO PARENT
// A2. PROPOGATE NODE SPLIT UPWARD
if (NN != NULL)
{
// INSERT IN CORRECT ORDER IF ROOM IN PARENT NODE
if (Np->non_leaf_node.num_entries < MAX_CHILDREN)
{
InsertNode(Np, NN);
}
// OTHERWISE INNVOKE HANDLEOVERFLOWNODE
else
{
// HANDLEOVERFLOWNODE: WHEN PARENT NODE MUST BE SPLIT; NEW_NODE IS RETURNED
new_node = HandleOverFlowNode(Np, NN);
// IF ROOT NODE WAS SPLIT BY HANDLEOVERFLOW
if (Np->parent_ptr == NULL && new_node != NULL)
{
root_split = true;
root1 = Np;
root2 = new_node;
}
}
}
// A3. ADJUST MBR AND LHV IN PARENT LEVEL
// P = SET OF PARENT NODES FOR NODES IN S
NODE *P = (NODE *)calloc(1, sizeof(NODE));
// NUMBER OF NODES IN P
int numParents = 1;
// FOR EVERY SIBLING NODE; PARENT NODE IS SAME
P[0] = S[0]->parent_ptr;
// A3. ADJUST CORRESPONDINNG MBRs AND LHVs OF NODES IN P
adjustMBRLHV(P[0]);
// adjustLHV(P[0]);
// A4. MOVE UP TO NEXT LEVEL: NN = NEW_NODE, S = P, NUMSIBLINGS = NUMPARENTS
AdjustTree(Np, new_node, P, numParents);
}
NODE HandleOverFlow(NODE n, Rectangle rectangle)
{
// CONTAINS COOPERATING SIBLINGS
NODE *S = cooperatingSiblings(n);
// SIZE OF SET S
int numSiblings = numberOfSiblings(S);
// RETURNED NODE
NODE NN = NULL;
// CHECK IF ANY NODE IS NOT FULL
bool allFull = true;
max_entries = 0;
allFull = allNodesFull(S, numSiblings);
// H1: SET OF ALL ENTRIES FROM SIBLINGS
// E = SET OF ALL ENTRIES FROM N AND S-1 COOPERATING SIBLINGS
LeafEntry *E = (LeafEntry *)calloc(max_entries + 1, sizeof(LeafEntry));
int *num_entries = (int *)malloc(sizeof(int));
*num_entries = 0;
// STORE ALL LEAF ENTRIES
store_all_leaf_entries(E, num_entries, S, numSiblings);
// CREATE NEW LEAF ENTRY
LeafEntry entry = new_leafentry(rectangle);
// H2. ADD R TO E; ADD NEW LEAF ENTRY TO SET E
E[(*num_entries)++] = entry;
// SORT THE ENTRIES IN E
qsort(E, *(num_entries), sizeof(LeafEntry), compare);
// H4. IF ALL SIBLINGS ARE FULL: CREATE A NEW NODE
if (allFull)
{
NN = new_node(1); // CREATE A NEW LEAF NODE;
// IF ROOT WAS SPLIT TO CREATE NEW NODE
if (n->parent_ptr == NULL)
{
// ROOT NODE IS SPLIT
root_split = true;
root1 = n;
root2 = NN;
}
// ADD NN TO SIBLINGS
S[numSiblings++] = NN; // ADD THE NEW NODE TO THE SET OF SIBLINGS
}
// DISTRIBUTE THE LEAF ENTRIES AMONGST NODES IN S [MAY CONTAIN NN IF SPLIT HAPPENED]
distribute_leaf_entries_evenly(S, numSiblings, E, num_entries);
// FREE ALLOCATTED MEMORY
free(E);
free(num_entries);
free(S);
// RETURN THE NODE ON SPLIT (NULL IF NO SPLIT)
return NN;
}
// PRE-ORDER TRAVERSAL OF A NODE; PRINTING A INTERNAL NODE AND THEN
void preOrderTraverse(NODE n)
{
// IF ALL NODES TRAVERSED
if (!n)
{
return;
}
// IF NODE IS A LEAF: PRINT ALL ENTRIES
if (n->is_leaf == 1)
{
for (int i = 0; i < n->leaf_node.num_entries; i++)
{
printf("Leaf Node Entry: %d\n", i + 1); //
printf("Object_ID = %d: ", n->leaf_node.entries[i].obj_id);
printf("Data Point = (%d, %d) ", n->leaf_node.entries[i].mbr.bottom_left.x, n->leaf_node.entries[i].mbr.bottom_left.y);
printf("Hilbert Value = %llu\n\n", n->leaf_node.entries[i].mbr.h);
}
}
// IF NODE IS NON-LEAF
else
{
for (int i = 0; i < n->non_leaf_node.num_entries; i++)
{
printf("Internal Node \n");
printMBR(n->non_leaf_node.entries[i].mbr);
printf("Largest Hilbert Value = %llu\n\n", n->non_leaf_node.entries[i].largest_hilbert_value);
preOrderTraverse(n->non_leaf_node.entries[i].child_ptr);
}
}
}
void preOrderTraverse_Rtree(HilbertRTree *tree)
{
printf("\n\nRE-ORDER TRAVERSAL OF RTREE\n\n");
// INVOKE FUNCTION USING NODE AS PARAMETER
preOrderTraverse(tree->root);
}
// ------------------------ HELPER FUNCTIONS ------------------------
// HELPER FUNCTION: Creates a new MBR with given coordinates
Rectangle new_rectangle(int bottomLeft_X, int bottomLeft_y, int topRight_x, int topRight_y)
{
Rectangle mbr;
// SET THE BOTTOM_LEFT COORDINATES
mbr.bottom_left.x = bottomLeft_X;
mbr.bottom_left.y = bottomLeft_y;
// SET THE TOP_RIGHT COORDINATES
mbr.top_right.x = topRight_x;
mbr.top_right.y = topRight_y;
// SET THE HILBERT VALUE USING CENTER'S COORDINATES
mbr.h = calculate_hilbert_value(hilbert_curve_order, (mbr.bottom_left.x + mbr.top_right.x) / 2, (mbr.bottom_left.y + mbr.top_right.y) / 2);
return mbr;
}
// HELPER FUNCTION: RETURNS TRUE IF ALL NODES IN S HAVE M NUMBER OF ENTRIES;
// FALSE IF ATLEAST ONE OF THE NDOES IN S HAS LESS THAN M ENTRIES
bool allNodesFull(NODE *S, int numSiblings)
{
// SET BOOLEAN TO TRUE INITIALLY
bool allFull = true;
max_entries = 0;
// FOR EACH SIBLING NODE
for (int i = 0; i < numSiblings; i++)
{
// IF NODE IS LEAF; CHECK LEAF ENTRIES
if (S[i]->is_leaf == 1)
{
max_entries += S[i]->leaf_node.num_entries;
// IF LESS THAN MAXMIMUM
if (S[i]->leaf_node.num_entries < M)
{
// SET B0OLEAN TO FALSE AND BREAK
allFull = false;
}
}
// IF NODE IS NON LEAF; CHECK NON LEAF ENTRIES
else if (S[i]->is_leaf == 0)
{
max_entries += S[i]->non_leaf_node.num_entries;
// IF LESS THAN MAXIMUM
if (S[i]->non_leaf_node.num_entries < M)
{
// SET BOOLEAN TO FALSE AND BREAK
allFull = false;
}
}
}
return allFull;
}
// HELPER FUNCTION: Returns the MBR of two rectangles r1 and r2
Rectangle calculateMBR(Rectangle r1, Rectangle r2)
{
Point bottom_leftNew;
Point top_rightNew;
if (r1.bottom_left.x <= r2.bottom_left.x)
{
bottom_leftNew.x = r1.bottom_left.x;
}
else
{
bottom_leftNew.x = r2.bottom_left.x;
}
if (r1.bottom_left.y <= r2.bottom_left.y)
{
bottom_leftNew.y = r1.bottom_left.y;
}
else
{
bottom_leftNew.y = r2.bottom_left.y;
}
if (r1.top_right.x <= r2.top_right.x)
{
top_rightNew.x = r2.top_right.x;
}
else
{
top_rightNew.x = r1.top_right.x;
}
if (r1.top_right.y <= r2.top_right.y)
{
top_rightNew.y = r2.top_right.y;
}
else
{
top_rightNew.y = r1.top_right.y;
}
Rectangle new_rect;
new_rect.bottom_left = bottom_leftNew;
new_rect.top_right = top_rightNew;
return new_rect;
}
// CALCULATE THE MBR FOR A NON LEAF ENTRY BASED ON ENTRIES IN ITS CHILD NODE
Rectangle calculateEntryMBR(NonLeafEntry entry)
{
// FOR EACH NON LEAF ENTRY; CALCULATE MBR FROM CHILD NODES
// FIND -> LOWEST X, LOWEST Y AND HIGHEST X, HIGHEST Y
Rectangle mbr;
NODE next_node = entry.child_ptr;
int low_x = 0;
int low_y = 0;
int high_x = 0;
int high_y = 0;
// IF CHILD NODE IS A LEAF; GET MBR FOR ALL ITS ENTRIES
if (next_node != NULL && next_node->is_leaf == 1)
{
high_x = next_node->leaf_node.entries[0].mbr.top_right.x;
high_y = next_node->leaf_node.entries[0].mbr.top_right.y;
low_x = next_node->leaf_node.entries[0].mbr.top_right.x;
low_y = next_node->leaf_node.entries[0].mbr.top_right.y;
for (int i = 0; i < next_node->leaf_node.num_entries; i++)
{
Rectangle obj_mbr = next_node->leaf_node.entries[i].mbr;
low_x = (obj_mbr.bottom_left.x <= low_x) ? obj_mbr.bottom_left.x : low_x;
low_y = (obj_mbr.bottom_left.y <= low_y) ? obj_mbr.bottom_left.y : low_y;
high_x = (obj_mbr.top_right.x >= high_x) ? obj_mbr.top_right.x : high_x;
high_y = (obj_mbr.top_right.y >= high_y) ? obj_mbr.top_right.y : high_y;
}
}
// IF CHILD NODE IS NON-LEAF;
else if (next_node != NULL && next_node->is_leaf == 0)
{
// NON LEAF NODE: ASSUMING IT HAS BEEN RECURSED UPON IN ADJUSTMBR; CALCULATE MBR FROM ITS ENTRIES
low_x = next_node->non_leaf_node.entries[0].mbr.top_right.x;
low_y = next_node->non_leaf_node.entries[0].mbr.top_right.y;
high_x = next_node->non_leaf_node.entries[0].mbr.top_right.x;
high_y = next_node->non_leaf_node.entries[0].mbr.top_right.y;
for (int i = 0; i < next_node->non_leaf_node.num_entries; i++)
{
Rectangle child_mbr = next_node->non_leaf_node.entries[i].mbr;
low_x = (child_mbr.bottom_left.x <= low_x) ? child_mbr.bottom_left.x : low_x;
low_y = (child_mbr.bottom_left.y <= low_y) ? child_mbr.bottom_left.y : low_y;
high_x = (child_mbr.top_right.x >= high_x) ? child_mbr.top_right.x : high_x;
high_y = (child_mbr.top_right.y >= high_y) ? child_mbr.top_right.y : high_y;
}
}
// SET THE COORDINATES FOR THE MBR
mbr = new_rectangle(low_x, low_y, high_x, high_y);
return mbr;
}
// PRINT THE MBR - TOP RIGHT POINT AND BOTTOM LEFT POINT: POTENTIAL HELPER FUNCTION
void printMBR(Rectangle rect)
{
// BOTTOM_LEFT POINT TO TOP_RIGHT POINT
printf("MBR = (%d, %d) to (%d, %d) ", rect.bottom_left.x, rect.bottom_left.y, rect.top_right.x, rect.top_right.y);
return;
}
// STORE ALL ELAF ENTRIES OF LEAF NODES IN S INTO E
void store_all_leaf_entries(LeafEntry *E, int *num_entries, NODE *S, int numSiblings)
{
// FOR EVERY SIBLING NODE
for (int i = 0; i < numSiblings; i++)
{
// SIBLING NODE IS A LEAF
if (S[i]->is_leaf == 1)
{
// STORES THE MBR OF ENTRIES INTO THE E ARRAY
for (int j = 0; j < S[i]->leaf_node.num_entries; j++)
{
E[(*num_entries)++] = S[i]->leaf_node.entries[j];
}
}
}
}
// FUNCTION TO CREATE A NEW LEAF ENTRY WITH THE RECTANGLE PASSED AND A NEW OBJECT ID
LeafEntry new_leafentry(Rectangle rectangle)
{
LeafEntry le;
le.mbr = rectangle;
le.obj_id = ++CURRENT_ID;
return le;
}
// CREATE A NEW NON LEAF-ENTRY GIVEN THE CHILD NODE
NonLeafEntry new_nonleafentry(NODE newNode)
{
NonLeafEntry nle;
nle.child_ptr = newNode;
nle.mbr = calculateEntryMBR(nle);
nle.largest_hilbert_value = calculateLHV(nle);
return nle;
}
// STORE ALL NON-LEAF ENTRIES IN THE NODES IN S INTO SET E (FOR HANDLEOVERFLOWNODE)
void store_all_nonleaf_entries(NonLeafEntry *E, NODE *S, int *num_entries, int numSiblings)
{
// FOR EACH SIBLING NODE
for (int i = 0; i < numSiblings; i++)
{
// SIBLING NODE IS NON-LEAF
if (S[i]->is_leaf == 0)
{
// STORE ALL THE NON LEAF ENTRIES OF THE NODE
for (int j = 0; j < S[i]->non_leaf_node.num_entries; j++)
{
E[(*num_entries)++] = S[i]->non_leaf_node.entries[j];
}
}
}
}
void distribute_nonleaf_entries_evenly(NODE *S, int numSiblings, NonLeafEntry *E, int *num_entries)
{
// SET THE NUMBER OF ENTRIES PER NODE [FOR EVEN DISTRIBUTION]
int num_entries_per_node = (*num_entries) / numSiblings;
// IF ODD TOTAL ENTRIES; COMPUTE REMAINDER ENTRIES
int remainder_entries = (*num_entries) % numSiblings;
// FOR EACH SIBLING NODE
// DISTRIBUTION LIST[I] = NUMBER OF ENTRIES FOR THE ITH SIBLINGS
int *distributionList = (int *)calloc(numSiblings, sizeof(int));
// EACH SIBLING HAS ATLEAST NUM_ENTRIES_PER_NODE ENTRIES
for (int i = 0; i < numSiblings; i++)
{
distributionList[i] = num_entries_per_node;
}
// ADD THE REMAINDER ENTRIES
for (int j = 0; j < remainder_entries; j++)
{
distributionList[j]++;
}
int done = 0;
// ADD THE ENTRIES TO THE SIBLINGS
for (int j = 0; j < numSiblings; j++)
{
// SET THE NON LEAF ENTRIES TO 0
S[j]->non_leaf_node.num_entries = 0;
for (int l = 0; l < distributionList[j]; l++)
{
// ADD THE NON LEAF ENTRY
S[j]->non_leaf_node.entries[l] = E[done];
E[done].child_ptr->parent_ptr = S[j];
S[j]->non_leaf_node.num_entries++;
done++;
}
// SET REMAINING ENTRIES TO 0
for (int l = distributionList[j]; l < M; l++)
{
S[j]->non_leaf_node.entries[l].mbr.bottom_left.x = 0;
S[j]->non_leaf_node.entries[l].mbr.bottom_left.y = 0;
S[j]->non_leaf_node.entries[l].mbr.top_right.x = 0;
S[j]->non_leaf_node.entries[l].mbr.top_right.y = 0;
S[j]->non_leaf_node.entries[l].mbr.h = 0;
S[j]->non_leaf_node.entries[l].child_ptr = NULL;
S[j]->non_leaf_node.entries[l].largest_hilbert_value = 0;
}
}
// FREE DYNAMICALLY ALLOCATTED MEMORY
free(distributionList);
return;
}
//FUNCTION TO HANDLE FULL PARENT NODES
NODE HandleOverFlowNode(NODE parentNode, NODE new_node1)
{
// TO INSERT NEW_NODE AS A CHILD POINTER IN A NON LEAF ENTRY
NonLeafEntry entry = new_nonleafentry(new_node1);
// SET OF COOPERATING SIBLINGS FOR THE PARENTNODE
NODE *S = cooperatingSiblings(parentNode);
// NUMBER OF SIBLINGS
int numSiblings = numberOfSiblings(S);
// SET THE POSSIBLE NODE UPON SLITTING OF PARENTNODE TO NULL
NODE NN = NULL;
// SET OF ALL NON LEAF ENTRIES IN PARENTNODE AND SIBLINGS
int *num_entries = (int *)malloc(sizeof(int));
*num_entries = 0;
// IF ALL SIBLINGS ARE FULL OR NOT
bool allFull = true;
allFull = allNodesFull(S, numSiblings); // True if all nodes in S are full
// printf("MAX ENTRIES = %d\n", max_entries);
NonLeafEntry *E = (NonLeafEntry *)calloc(max_entries + 1, sizeof(NonLeafEntry));
// ADD ALL ENTRIES TO E FROM THE SIBLINGS
store_all_nonleaf_entries(E, S, num_entries, numSiblings);
// ADD NEW NON LEAF ENTRY TO E
E[(*num_entries)++] = entry;
// SORT THE SET OF NON LEAF ENTRIES BASED ON LHV OF NON LEAF ENTRIES [FOR THE NEWEST ENTRY]
qsort(E, *num_entries, sizeof(NonLeafEntry), compareNonLeafEntry);
// IF ALL SIBLINGS ARE FULL
if (allFull)
{
// printf("ALL PARENT NODE'S %d SIBLINGS ARE FULL\n", numSiblings);
// CREATE A NEW NODE
NN = new_node(0);
if (parentNode->parent_ptr == NULL)
{
// PARENT NODE IS ROOT: ROOT WAS SPLIT
root_split = true;
root1 = parentNode;
root2 = NN;
}
// ADD NN TO SIBLINGS
S[numSiblings++] = NN; // ADD THE NEW NODE TO THE SET OF SIBLINGS
}
distribute_nonleaf_entries_evenly(S, numSiblings, E, num_entries);
// FREE DYNAMICALLY ALLOCATTED MEMORY
free(E);
free(num_entries);
free(S);
// RETURN NODE FORMED ON SPLIT [NULL IF NO SPLIT]
return NN;
}
// HELPER FUNCTION TO RETURN TRUE IF TWO RECTANGLES HAVE THE SAME BOTTOM LEFT AND TOP RIGHT POINTS ARE SAME
bool rectangles_equal(Rectangle *rect1, Rectangle *rect2)
{
if (rect1->bottom_left.x != rect2->bottom_left.x ||
rect1->bottom_left.y != rect2->bottom_left.y ||
rect1->top_right.x != rect2->top_right.x ||
rect1->top_right.y != rect2->top_right.y)
{
return false;
}
return true;
}
// CALCULATE LHV FOR A NON LEAF ENTRY: BY EVALUATING ALL CHILD PTR ETRIES
int calculateLHV(NonLeafEntry entry)
{
int max_h = 0;
NODE node = entry.child_ptr;
// CALCULATE MAXIMUM H OF NODE
if (node->is_leaf == 1)
{
max_h = node->leaf_node.entries[0].mbr.h;
for (int i = 0; i < node->leaf_node.num_entries; i++)
{
if (node->leaf_node.entries[i].mbr.h > max_h)
{
max_h = node->leaf_node.entries[i].mbr.h;
}
}
return max_h;
}
else if (node->is_leaf == 0)
{
max_h = 0;
// NON LEAF CHILD NODE
for (int i = 0; i < node->non_leaf_node.num_entries; i++)
{
node->non_leaf_node.entries[i].largest_hilbert_value = calculateLHV(node->non_leaf_node.entries[i]);
// ASSUMING LHV OF A NON LEAF ENTRY WITH A NON LEAF CHILD NODE IS THE MAXIMUM H OF THE NON LEAF ENTRIES IN THE CHILD NODE
if (node->non_leaf_node.entries[i].largest_hilbert_value > max_h)
{
max_h = node->non_leaf_node.entries[i].largest_hilbert_value;
}
}
}
return max_h;
}
/*ADJUST TREE ASCEND FROM LEAF TOWARDS ROOT AND ADJUST MBR AND LHV VALUES*/
void adjustLHV(NODE parentNode)
{
if (parentNode == NULL)
{
return;
}
// FOR NON-NULL NODES; FOR EACH ENTRY [NON-LEAF SINCE PARENTNODES ARE ALWAYS NON-LEAF]
for (int i = 0; i < parentNode->non_leaf_node.num_entries; i++)
{
// CALCULATE LHV FOR EACH ENTRY
parentNode->non_leaf_node.entries[i].largest_hilbert_value = calculateLHV(parentNode->non_leaf_node.entries[i]);
}
// INVOKE ADJUSTLHV ON ITS PARENTNODE
adjustLHV(parentNode->parent_ptr);
}
//ASCEND FROM PARENT NODE; ADJUSTING MBR AT ALL LEVELS UNTIL ROOT
void adjustMBRLHV(NODE parentNode)
{
if (parentNode == NULL)
{
return;
}
// FOR NON-NULL NODES; FOR EACH ENTRY [NON-LEAF SINCE PARENTNODES ARE ALWAYS NON-LEAF]