-
Notifications
You must be signed in to change notification settings - Fork 0
/
btree.c
863 lines (719 loc) · 23.1 KB
/
btree.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
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
#include "btree.h"
typedef enum {left = -1,right = 1} position_t;
typedef struct {
bt_node * node;
unsigned int index;
}node_pos;
static void print_single_node(btree *btree, bt_node * node);
static bt_node * allocate_btree_node (unsigned int order);
static int free_btree_node (bt_node * node);
static node_pos get_btree_node(btree * btree,uint64_t key);
static int delete_key_from_node(btree * btree, node_pos * node_pos);
static bt_node * merge_nodes(btree * btree, bt_node * n1, bt_key_val kv ,bt_node * n2);
static void move_key(btree * btree, bt_node * node, unsigned int index, position_t pos);
static node_pos get_max_key_pos(btree * btree, bt_node * subtree);
static node_pos get_min_key_pos(btree * btree, bt_node * subtree);
static bt_node * merge_siblings(btree * btree, bt_node * parent,unsigned int index,
position_t pos);
static void copy_key_val(btree * btree,bt_key_val *src, bt_key_val *dst);
/**
* Used to create a btree with just the root node
* @param order The order of the B-tree
* @return The an empty B-tree
*/
btree * btree_create(unsigned int order) {
btree * btree;
btree = mem_alloc(sizeof(*btree));
btree->order = order;
btree->root = allocate_btree_node(order);
btree->root->leaf = True;
btree->root->nr_active = 0;
btree->root->next = NULL;
btree->root->level = 0;
return btree;
}
/**
* Function used to allocate memory for the btree node
* @param order Order of the B-Tree
* @param leaf boolean set true for a leaf node
* @return The allocated B-tree node
*/
static bt_node * allocate_btree_node (unsigned int order) {
bt_node * node;
// Allocate memory for the node
node = (bt_node *)mem_alloc(sizeof(bt_node));
// Initialize the number of active nodes
node->nr_active = 0;
// Initialize the keys
node->key_vals = (bt_key_val *)mem_alloc(2*order*sizeof(bt_key_val) - 1);
// Initialize the child pointers
node->children = (bt_node **)mem_alloc(2*order*sizeof(bt_node*));
// Use to determine whether it is a leaf
node->leaf = True;
// Use to determine the level in the tree
node->level = 0;
//Initialize the linked list pointer to NULL
node->next = NULL;
return node;
}
/**
* Function used to free the memory allocated to the b-tree
* @param node The node to be freed
* @param order Order of the B-Tree
* @return The allocated B-tree node
*/
static int free_btree_node (bt_node * node) {
mem_free(node->children);
mem_free(node->key_vals);
mem_free(node);
return 0;
}
/**
* Used to split the child node and adjust the parent so that
* it has two children
* @param parent Parent Node
* @param index Index of the child node
* @param child Full child node
*
*/
static void btree_split_child(btree * btree, bt_node * parent,
unsigned int index,
bt_node * child) {
int i = 0;
unsigned int order = btree->order;
bt_node * new_child = allocate_btree_node(btree->order);
new_child->leaf = child->leaf;
new_child->level = child->level;
new_child->nr_active = btree->order - 1;
// Copy the higher order keys to the new child
for(i=0;i<order - 1;i++) {
new_child->key_vals[i] = child->key_vals[i + order];
if(!child->leaf) {
new_child->children[i] =
child->children[i + order];
}
}
// Copy the last child pointer
if(!child->leaf) {
new_child->children[i] =
child->children[i + order];
}
child->nr_active = order - 1;
for(i = parent->nr_active + 1;i > index + 1;i--) {
parent->children[i] = parent->children[i - 1];
}
parent->children[index + 1] = new_child;
for(i = parent->nr_active;i > index;i--) {
parent->key_vals[i] = parent->key_vals[i - 1];
}
parent->key_vals[index] = child->key_vals[order - 1];
parent->nr_active++;
}
/**
* Used to insert a key in the non-full node
* @param btree The btree
* @param node The node to which the key will be added
* @param the key value pair
* @return void
*/
static void btree_insert_nonfull (btree * btree, bt_node * parent_node,
bt_key_val key_val) {
uint64_t key = key_val.key;
int i ;
bt_node * child;
bt_node * node = parent_node;
insert: i = node->nr_active - 1;
if(node->leaf) {
while(i >= 0 && key < node->key_vals[i].key) {
node->key_vals[i + 1] = node->key_vals[i];
i--;
}
node->key_vals[i + 1] = key_val;
node->nr_active++;
} else {
while (i >= 0 && key < node->key_vals[i].key) {
i--;
}
i++;
child = node->children[i];
if(child->nr_active == 2*btree->order - 1) {
btree_split_child(btree,node,i,child);
if(key_val.key >
node->key_vals[i].key) {
i++;
}
}
node = node->children[i];
goto insert;
}
}
/**
* Function used to insert node into a B-Tree
* @param root Root of the B-Tree
* @param node The node to be inserted
* @param compare Function used to compare the two nodes of the tree
* @return success or failure
*/
int btree_insert_key(btree * btree, bt_key_val key_val) {
bt_node * rnode;
rnode = btree->root;
if(rnode->nr_active == (2*btree->order - 1)) {
bt_node * new_root;
new_root = allocate_btree_node(btree->order);
new_root->level = btree->root->level + 1;
btree->root = new_root;
new_root->leaf = False;
new_root->nr_active = 0;
new_root->children[0] = rnode;
btree_split_child(btree,new_root,0,rnode);
btree_insert_nonfull(btree,new_root,key_val);
} else
btree_insert_nonfull(btree,rnode,key_val);
return 0;
}
/**
* Used to get the position of the MAX key within the subtree
* @param btree The btree
* @param subtree The subtree to be searched
* @return The node containing the key and position of the key
*/
static node_pos get_max_key_pos(btree * btree, bt_node * subtree) {
node_pos node_pos;
bt_node * node = subtree;
while(True) {
if(node == NULL) {
break;
}
if(node->leaf) {
node_pos.node = node;
node_pos.index = node->nr_active - 1;
return node_pos;
} else {
node_pos.node = node;
node_pos.index = node->nr_active - 1;
node = node->children[node->nr_active];
}
}
return node_pos;
}
/**
* Used to get the position of the MAX key within the subtree
* @param btree The btree
* @param subtree The subtree to be searched
* @return The node containing the key and position of the key
*/
static node_pos get_min_key_pos(btree * btree, bt_node * subtree) {
node_pos node_pos;
bt_node * node = subtree;
while(True) {
if(node == NULL) {
break;
}
if(node->leaf) {
node_pos.node = node;
node_pos.index = 0;
return node_pos;
} else {
node_pos.node = node;
node_pos.index = 0;
node = node->children[0];
}
}
return node_pos;
}
/**
* Merge nodes n1 and n2 (case 3b from Cormen)
* @param btree The btree
* @param node The parent node
* @param index of the child
* @param pos left or right
* @return none
*/
static bt_node * merge_siblings(btree * btree, bt_node * parent, unsigned int index ,
position_t pos) {
unsigned int i,j;
bt_node * new_node;
bt_node * n1, * n2;
if (index == (parent->nr_active)) {
index--;
n1 = parent->children[parent->nr_active - 1];
n2 = parent->children[parent->nr_active];
} else {
n1 = parent->children[index];
n2 = parent->children[index + 1];
}
//Merge the current node with the left node
new_node = allocate_btree_node(btree->order);
new_node->level = n1->level;
new_node->leaf = n1->leaf;
for(j=0;j<btree->order - 1; j++) {
new_node->key_vals[j] = n1->key_vals[j];
new_node->children[j] = n1->children[j];
}
new_node->key_vals[btree->order - 1] = parent->key_vals[index];
new_node->children[btree->order - 1] = n1->children[btree->order - 1];
for(j=0;j<btree->order - 1; j++) {
new_node->key_vals[j + btree->order] = n2->key_vals[j];
new_node->children[j + btree->order] = n2->children[j];
}
new_node->children[2*btree->order - 1] = n2->children[btree->order - 1];
parent->children[index] = new_node;
for(j = index;j<parent->nr_active;j++) {
parent->key_vals[j] = parent->key_vals[j + 1];
parent->children[j + 1] = parent->children[j + 2];
}
new_node->nr_active = n1->nr_active + n2->nr_active + 1;
parent->nr_active--;
// for(i=parent->nr_active;i < 2*btree->order - 1; i++) {
// parent->key_vals[i] = (bt_key_val)0;
// }
free_btree_node(n1);
free_btree_node(n2);
if (parent->nr_active == 0 && btree->root == parent) {
free_btree_node(parent);
btree->root = new_node;
if(new_node->level)
new_node->leaf = False;
else
new_node->leaf = True;
}
return new_node;
}
/**
* Move the key from node to another
* @param btree The B-Tree
* @param node The parent node
* @param index of the key to be moved done
* @param pos the position of the child to receive the key
* @return none
*/
static void move_key(btree * btree, bt_node * node, unsigned int index, position_t pos) {
bt_node * lchild;
bt_node * rchild;
unsigned int i;
if(pos == right) {
index--;
}
lchild = node->children[index];
rchild = node->children[index + 1];
// Move the key from the parent to the left child
if(pos == left) {
lchild->key_vals[lchild->nr_active] = node->key_vals[index];
lchild->children[lchild->nr_active + 1] = rchild->children[0];
rchild->children[0] = NULL;
lchild->nr_active++;
node->key_vals[index] = rchild->key_vals[0];
//rchild->key_vals[0] = NULL;
for(i=0;i<rchild->nr_active - 1;i++) {
rchild->key_vals[i] = rchild->key_vals[i + 1];
rchild->children[i] = rchild->children[i + 1];
}
rchild->children[rchild->nr_active - 1] =
rchild->children[rchild->nr_active];
rchild->nr_active--;
} else {
// Move the key from the parent to the right child
for(i=rchild->nr_active;i > 0 ; i--) {
rchild->key_vals[i] = rchild->key_vals[i - 1];
rchild->children[i + 1] = rchild->children[i];
}
rchild->children[1] = rchild->children[0];
rchild->children[0] = NULL;
rchild->key_vals[0] = node->key_vals[index];
rchild->children[0] = lchild->children[lchild->nr_active];
lchild->children[lchild->nr_active] = NULL;
node->key_vals[index] = lchild->key_vals[lchild->nr_active - 1];
//lchild->key_vals[lchild->nr_active - 1] = NULL;
lchild->nr_active--;
rchild->nr_active++;
}
}
/**
* Merge nodes n1 and n2
* @param n1 First node
* @param n2 Second node
* @return combined node
*/
static bt_node * merge_nodes(btree * btree, bt_node * n1, bt_key_val kv,
bt_node * n2) {
bt_node * new_node;
unsigned int i;
new_node = allocate_btree_node(btree->order);
new_node->leaf = True;
for(i=0;i<n1->nr_active;i++) {
new_node->key_vals[i] = n1->key_vals[i];
new_node->children[i] = n1->children[i];
}
new_node->children[n1->nr_active] = n1->children[n1->nr_active];
new_node->key_vals[n1->nr_active] = kv;
for(i=0;i<n2->nr_active;i++) {
new_node->key_vals[i + n1->nr_active + 1] = n2->key_vals[i];
new_node->children[i + n1->nr_active + 1] = n2->children[i];
}
new_node->children[2*btree->order - 1] = n2->children[n2->nr_active];
new_node->nr_active = n1->nr_active + n2->nr_active + 1;
new_node->leaf = n1->leaf;
new_node->level = n1->level;
free_btree_node(n1);
free_btree_node(n2);
return new_node;
}
/**
* Used to delete a key from the B-tree node
* @param btree The btree
* @param node The node from which the key is to be deleted
* @param key The key to be deleted
* @return 0 on success -1 on error
*/
int delete_key_from_node(btree * btree, node_pos * node_pos) {
unsigned int keys_max = 2*btree->order - 1;
unsigned int i;
bt_key_val key_val;
bt_node * node = node_pos->node;
if(node->leaf == False) {
return -1;
}
key_val = node->key_vals[node_pos->index];
for(i=node_pos->index;i< keys_max - 1;i++) {
node->key_vals[i] = node->key_vals[i + 1];
}
/* if(key_val->key) {
mem_free(key_val->key);
key_val->key = NULL;
}
if(key_val->val) {
mem_free(key_val->val);
key_val->val = NULL;
}*/
node->nr_active--;
if(node->nr_active == 0 ) {
free_btree_node(node);
}
return 0;
}
/**
* Function used to delete a node from a B-Tree
* @param btree The B-Tree
* @param key Key of the node to be deleted
* @param value function to map the key to an unique integer value
* @param compare Function used to compare the two nodes of the tree
* @return success or failure
*/
int btree_delete_key(btree * btree,bt_node * subtree, uint64_t key) {
unsigned int i,index;
bt_node * node = NULL, * rsibling, *lsibling;
bt_node * comb_node, * parent;
node_pos sub_node_pos;
node_pos node_pos;
bt_key_val key_val,new_key_val;
uint64_t kv = key;
node = subtree;
parent = NULL;
del_loop:for (i = 0;;i = 0) {
//If there are no keys simply return
if(!node->nr_active)
return -1;
// Fix the index of the key greater than or equal
// to the key that we would like to search
while (i < node->nr_active && kv >
node->key_vals[i].key) {
i++;
}
index = i;
// If we find such key break
if(i < node->nr_active &&
kv == node->key_vals[i].key) {
break;
}
if(node->leaf)
return -1;
//Store the parent node
parent = node;
// To get a child node
node = node->children[i];
//If NULL not found
if (node == NULL)
return -1;
if (index == (parent->nr_active)) {
lsibling = parent->children[parent->nr_active - 1];
rsibling = NULL;
} else if (index == 0) {
lsibling = NULL;
rsibling = parent->children[1];
} else {
lsibling = parent->children[i - 1];
rsibling = parent->children[i + 1];
}
if (node->nr_active == btree->order - 1 && parent) {
// The current node has (t - 1) keys but the right sibling has > (t - 1)
// keys
if (rsibling && (rsibling->nr_active > btree->order - 1)) {
move_key(btree,parent,i,left);
} else
// The current node has (t - 1) keys but the left sibling has (t - 1)
// keys
if (lsibling && (lsibling->nr_active > btree->order - 1)) {
move_key(btree,parent,i,right);
} else
// Left sibling has (t - 1) keys
if(lsibling && (lsibling->nr_active == btree->order - 1)) {
node = merge_siblings(btree,parent,i,left);
} else
// Right sibling has (t - 1) keys
if(rsibling && (rsibling->nr_active == btree->order - 1)) {
node = merge_siblings(btree,parent,i,right);
}
}
}
//Case 1 : The node containing the key is found and is the leaf node.
//Also the leaf node has keys greater than the minimum required.
//Simply remove the key
if(node->leaf && (node->nr_active > btree->order - 1)) {
node_pos.node = node;
node_pos.index = index;
delete_key_from_node(btree,&node_pos);
return 0;
}
//If the leaf node is the root permit deletion even if the number of keys is
//less than (t - 1)
if(node->leaf && (node == btree->root)) {
node_pos.node = node;
node_pos.index = index;
delete_key_from_node(btree,&node_pos);
return 0;
}
//Case 2: The node containing the key is found and is an internal node
if(node->leaf == False) {
if(node->children[index]->nr_active > btree->order - 1 ) {
sub_node_pos = get_max_key_pos(btree,node->children[index]);
key_val = sub_node_pos.node->key_vals[sub_node_pos.index];
//new_key_val = (bt_key_val *)mem_alloc(sizeof(bt_key_val));
copy_key_val(btree,&key_val,&new_key_val);
node->key_vals[index] = new_key_val;
btree_delete_key(btree,node->children[index],key_val.key);
if(sub_node_pos.node->leaf == False) {
print("Not leaf\n");
}
} else if ((node->children[index + 1]->nr_active > btree->order - 1) ) {
sub_node_pos =
get_min_key_pos(btree,node->children[index + 1]);
key_val = sub_node_pos.node->key_vals[sub_node_pos.index];
//new_key_val = (bt_key_val *)mem_alloc(sizeof(bt_key_val));
copy_key_val(btree,&key_val,&new_key_val);
node->key_vals[index] = new_key_val;
btree_delete_key(btree,node->children[index + 1],key_val.key);
if(sub_node_pos.node->leaf == False) {
print("Not leaf\n");
}
} else if (
node->children[index]->nr_active == btree->order - 1 &&
node->children[index + 1]->nr_active == btree->order - 1) {
comb_node = merge_nodes(btree,node->children[index],
node->key_vals[index],
node->children[index + 1]);
node->children[index] = comb_node;
for(i=index + 1;i<node->nr_active;i++) {
node->children[i] = node->children[i + 1];
node->key_vals[i - 1] = node->key_vals[i];
}
node->nr_active--;
if (node->nr_active == 0 && btree->root == node) {
free_btree_node(node);
btree->root = comb_node;
}
node = comb_node;
goto del_loop;
}
}
// Case 3:
// In this case start from the top of the tree and continue
// moving to the leaf node making sure that each node that
// we encounter on the way has atleast 't' (order of the tree)
// keys
if(node->leaf && (node->nr_active > btree->order - 1)) {
node_pos.node = node;
node_pos.index = index;
delete_key_from_node(btree,&node_pos);
}
return 0;
}
/**
* Function used to get the node containing the given key
* @param btree The btree to be searched
* @param key The the key to be searched
* @return The node and position of the key within the node
*/
node_pos get_btree_node(btree * btree, uint64_t key) {
node_pos kp;
uint64_t key_val = key;
bt_node * node;
unsigned int i = 0;
static k=0,j=0;
k++;
kp.node = NULL;
node = btree->root;
for (;;i = 0) {
// Fix the index of the key greater than or equal
// to the key that we would like to search
while (i < node->nr_active && key_val >
node->key_vals[i].key) {
i++; j++;
}
// If we find such key return the key-value pair
if(i < node->nr_active &&
key_val == node->key_vals[i].key) {
kp.node = node;
kp.index = i;
j++;
#ifdef DEBUG
if ((k+1)%100 == 0)
fprintf(stdout, "get_btree_node: avg search calls = %d\n", j/k);
#endif
return kp;
}
// If the node is leaf and if we did not find the key
// return NULL
if(node->leaf) {
return kp;
}
// To got a child node
node = node->children[i];
}
return kp;
}
/**
* Used to destory btree
* @param btree The B-tree
* @return none
*/
void btree_destroy(btree * btree) {
int i = 0;
unsigned int current_level;
bt_node * head, * tail, * node;
bt_node * child, * del_node;
node = btree->root;
current_level = node->level;
head = node;
tail = node;
while(True) {
if(head == NULL) {
break;
}
if (head->level < current_level) {
current_level = head->level;
}
if(head->leaf == False) {
for(i = 0 ; i < head->nr_active + 1; i++) {
child = head->children[i];
tail->next = child;
tail = child;
child->next = NULL;
}
}
del_node = head;
head = head->next;
free_btree_node(del_node);
}
}
/**
* Function used to search a node in a B-Tree
* @param btree The B-tree to be searched
* @param key Key of the node to be search
* @return The key-value pair
*/
bt_key_val btree_search(btree * btree, uint64_t key) {
bt_key_val key_val;
key_val.key = 0;
key_val.val = 0;
key_val.pt = NULL;
node_pos kp = get_btree_node(btree,key);
if(kp.node) {
key_val = kp.node->key_vals[kp.index];
}
return key_val;
}
/**
* Used to copy key value from source to destination
* @param src The source key value
* @param dst The dest key value
* @return none
*/
static void copy_key_val(btree * btree, bt_key_val *src, bt_key_val *dst) {
unsigned int keysize;
unsigned int datasize;
dst->key = src->key;
dst->val = src->val;
dst->pt = src->pt;
}
/**
* Get the max key in the btree
* @param btree The btree
* @return The max key
*/
uint64_t btree_get_max_key(btree * btree) {
node_pos node_pos;
node_pos = get_max_key_pos(btree,btree->root);
return node_pos.node->key_vals[node_pos.index].key;
}
/**
* Get the min key in the btree
* @param btree The btree
* @return The max key
*/
uint64_t btree_get_min_key(btree * btree) {
node_pos node_pos;
node_pos = get_min_key_pos(btree,btree->root);
return node_pos.node->key_vals[node_pos.index].key;
}
#ifdef DEBUG
/**
* Used to print the keys of the bt_node
* @param node The node whose keys are to be printed
* @return none
*/
static void print_single_node(btree *btree, bt_node * node) {
int i = 0;
print(" { ");
while(i < node->nr_active) {
print("k %llx v %llx (%d) ", node->key_vals[i].key, node->key_vals[i].val,
node->level);
i++;
}
print("} (0x%x,%d) ", node,node->leaf);
}
/**
* Function used to print the B-tree
* @param root Root of the B-Tree
* @param print_key Function used to print the key value
* @return none
*/
void print_subtree(btree *btree,bt_node * node) {
int i = 0;
unsigned int current_level;
bt_node * head, * tail;
bt_node * child;
current_level = node->level;
head = node;
tail = node;
while(True) {
if(head == NULL) {
break;
}
if (head->level < current_level) {
current_level = head->level;
print("\n");
}
print_single_node(btree,head);
if(head->leaf == False) {
for(i = 0 ; i < head->nr_active + 1; i++) {
child = head->children[i];
tail->next = child;
tail = child;
child->next = NULL;
}
}
head = head->next;
}
print("\n");
}
#endif