-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb_tree.c
More file actions
628 lines (540 loc) · 15.1 KB
/
b_tree.c
File metadata and controls
628 lines (540 loc) · 15.1 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
/**
* Dynamic tree structure. Binds a key to a value(int or string).
* The tree has N number of nodes, the initial size will be of size 10. Will be decreased or increased dynamically.
* Every level of the tree is kept sorted.
* A node can be of type:
* folder : Holds no value but a variable number of child nodes,
* numeric : Holds an integer value,
* string : Holds a string value.
*
* @author Joakim Söderstrand
*/
#include "b_tree.h"
void *MallocSafe(size_t size) {
void *result;
result = malloc(size);
if (result != NULL) {
return result;
}
printf("error: malloc failed\n");
exit(-1);
}
/**
* Setting the initial size of the child-array to 10
* @return
*/
bTNode *CreateNewNode(bTree *bt, int valueType, char* name) {
bTNode *newNode = (bTNode*) MallocSafe(sizeof(bTNode));
if(valueType==IS_FOLDER) {
newNode->childNodeCapacity = INITIAL_CHILD_CAPACITY;
newNode->childNodes = (bTNode **) MallocSafe(newNode->childNodeCapacity * sizeof(bTNode *));
}
newNode->nrOfChildNodes = 0;
newNode->name = (char*) MallocSafe(strlen(name)+1 * sizeof(char));
strcpy(newNode->name, name);
newNode->type = valueType;
return newNode;
}
/**
* Create a new bTree and return the pointer.
* Will set root when initializing since it will be fixed without values(only child nodes).
* @return
*/
bTree *CreateBTree() {
bTree *tree = (bTree *) malloc(sizeof(bTree));
tree->root = CreateNewNode(tree, IS_FOLDER, "root");
tree->root->parent = NULL;
return tree;
}
/**
* Insert new node into parent-folder child array
* @param new
*/
void Insert(bTNode *current, bTNode *new) {
if(current->nrOfChildNodes == 0){
current->childNodes[0] = new;
new->parent = current;
current->nrOfChildNodes++;
return;
}
// Need to increase size when capacity is reached
if(current->nrOfChildNodes==current->childNodeCapacity){
current->childNodes = realloc(current->childNodes, (current->childNodeCapacity*2) * sizeof(bTNode*));
current->childNodeCapacity = current->childNodeCapacity*2;
if(current->childNodes == NULL){
exit(EXIT_FAILURE);
}
}
int s = current->nrOfChildNodes;
int m = current->nrOfChildNodes / 2;
// The sorting mechanism
if (Compare(new, current->childNodes[m]) > 0) {
for (int i = m; i < s; i++) {
// update key with new node
if (Compare(new, current->childNodes[i]) == 0) {
// Do nothing if folder
if(new->type == IS_FOLDER){
FreeNode(new);
return;
}
// Replace the value
else{
ReplaceValue(current->childNodes[i], new);
FreeNode(new);
return;
}
}
if (Compare(new, current->childNodes[i]) < 0) {
memmove(¤t->childNodes[i + 1], ¤t->childNodes[i], (s - i) * sizeof(bTNode *));
new->parent = current;
current->childNodes[i] = new;
current->nrOfChildNodes++;
return;
}
}
// if this point is reached the new node key is the biggest and just gets appended
new->parent = current;
current->childNodes[s] = new;
current->nrOfChildNodes++;
return;
}
else {
for (int i = 0; i < s; i++) {
bTNode *child = current->childNodes[i];
// if the node already exists
if (strcmp(new->name, child->name) == 0) {
// Do nothing if folder
if (new->type == IS_FOLDER) {
FreeNode(new);
return;
}
// Replace the value
else {
ReplaceValue(current->childNodes[i], new);
FreeNode(new);
return;
}
}
if (Compare(new, current->childNodes[i]) < 0) {
memmove(¤t->childNodes[i + 1], ¤t->childNodes[i], (s - i) * sizeof(bTNode *));
new->parent = current;
current->childNodes[i] = new;
current->nrOfChildNodes++;
return;
}
}
}
}
/**
* Insert a new node on the given path
* @param bt
* @param dots
* @param path
* @param type
* @param name
* @param val
*/
void BTreeInsert(bTree *bt, char **path, int type, char *name, void *val) {
bTNode *new = CreateNewNode(bt, type, name);
new->type = type;
if (new->type == IS_STRING) {
new->stringVal = (char*) malloc(strlen(val)+1 * sizeof(char));
strcpy(new->stringVal, val);
}
else {
new->value = (int) val;
}
// If there is no path, insert at root.
if (path==NULL) {
if (bt->root->nrOfChildNodes == 0) {
bt->root->childNodes[0] = new;
bt->root->nrOfChildNodes++;
new->parent = bt->root;
return;
}
Insert(bt->root, new);
}
else {
// Check if the node exist, will replace value if it does.
bTNode *tmp = FindWithPath(bt, path);
if (tmp != NULL) {
Insert(tmp->parent, new);
return;
}
// First check if the sub-path exists, if not, create the folder missing.
bTNode* parent = bt->root;
while (strcmp(*path, name)!=0){
tmp = Find(parent, *path);
if(tmp==NULL){
tmp = CreateNewNode(bt, IS_FOLDER, *path);
Insert(parent, tmp);
}
parent = tmp;
path++;
}
// insert the value in path (path[0].path[1]....path[N].key-value)
Insert(parent, new);
}
}
int Compare(bTNode *n1, bTNode *n2) {
return strcmp(n1->name, n2->name);
}
/**
* Recursively print the tree from current node to leaf-nodes.
* @param node
*/
void PrintNode(bTNode *node) {
printf("%s ", node->name);
if(node->type==IS_STRING){
printf(": %s\n", node->stringVal);
}
else if(node->type==IS_NUMERIC){
printf(": %d\n", node->value);
}
else{
printf(" folder: ");
}
if (node->nrOfChildNodes == 0) {
return;
}
printf("\n");
for (int i = 0; i < node->nrOfChildNodes; i++) {
PrintNode(node->childNodes[i]);
}
}
/**
* Print the whole tree
* @param bt
*/
void PrintBTree(bTree *bt) {
PrintNode(bt->root);
}
void ReplaceValue(bTNode *old, bTNode *new){
// If the new value is of a different type print error message
if(old->type != new->type){
return;
}
if(new->type==IS_STRING){
old->stringVal = realloc(old->stringVal, strlen(new->stringVal)+1);
strcpy(old->stringVal, new->stringVal);
}
else{
old->value = new->value;
}
}
/**
* Recursively search search a folder for the node with the given key
* @param node
* @param key
* @return
*/
bTNode* SearchForText(bTNode* node, char* key){
if(node==NULL){
return NULL;
}
if(node->type == IS_FOLDER){
bTNode* child;
for(int i=0; i<node->nrOfChildNodes; i++){
child = node->childNodes[i];
if(strcmp(child->name, key)==0){
return child;
}
return SearchForText(child, key);
}
}
return NULL;
}
/**
* This function requires strings to be placed in a folder called strings.. If the folder does not exist
* NULL is returned.
* If the key with the given language prefix does not exist, a recursive search will be started from /string.
* @param bt
* @param key
* @param lan
* @return
*/
char* GetText(bTree* bt, char *key, char *lan){
if(Find(bt->root, "strings")==NULL){
puts("could not locate 'strings'-folder..");
return NULL;
}
char *path[4] = {"strings", lan, key, END_OF_PATH};
bTNode* tmp = FindWithPath(bt, path);
if(tmp==NULL) {
tmp = SearchForText(Find(bt->root, "strings"), key);
}
if(tmp!=NULL){
return tmp->stringVal;
}
else{
return NULL;
}
}
/**
* Returns the string value of a node on the given path.
* Returns null if it does not exist, or if the node is of the wrong type.
* @param bt
* @param path
* @param depth
* @return
*/
char* GetString(bTree* bt, char** path){
bTNode* tmp = FindWithPath(bt ,path);
if(tmp==NULL){
printf("node does not exist on this path..\n");
return NULL;
} else if(tmp->type != IS_STRING){
printf("node does not hold a string value.. \n");
return NULL;
}
else {
return tmp->stringVal;
}
}
int GetInt(bTree *bt, char** path){
bTNode* tmp = FindWithPath(bt ,path);
if(tmp==NULL){
printf("node does not exist on this path..\n");
return NULL;
}
else if(tmp->type != IS_NUMERIC){
printf("node does not hold a numeric value.. \n");
return NULL;
}
else {
return tmp->value;
}
}
/**
* Print the nodes in the current folder that holds a value.(skip folders)
* @param bt
* @param path
* @param depth
*/
void Enumerate(bTree *bt, char** path){
bTNode *tmp = FindWithPath(bt, path);
if(tmp == NULL){
printf("node does not exist.\n");
return;
} else if(tmp->type != IS_FOLDER){
if(tmp->type == IS_STRING){
printf("%s : %s\n", tmp->name, tmp->stringVal);
} else{
printf("%s : %d\n", tmp->name, tmp->value);
}
return;
}
bTNode** children = tmp->childNodes;
for(int i=0; i<tmp->nrOfChildNodes; i++) {
if(children[i]->type == IS_STRING){
printf("%s : %s\n", children[i]->name, children[i]->stringVal);
} else if(children[i]->type==IS_NUMERIC){
printf("%s : %d\n", children[i]->name, children[i]->value);
}
}
}
/**
* Return the type of the node or -1 if it does not exist.
* @param bt
* @param path
* @return
*/
int GetType(bTree *bt, char** path){
bTNode* node = FindWithPath(bt, path);
if(node == NULL){
return -1;
}
return node->type;
}
/**
* Generic function that returns the value of a given node. Need to specify type!
* Null is return if error.
* @param bt
* @param path
* @param type
* @return
*/
void* GetValue(bTree *bt, char** path, int type){
bTNode* tmp = FindWithPath(bt, path);
if(tmp==NULL){
return NULL;
}
if(type!=tmp->type||tmp->type==IS_FOLDER){
return NULL;
}
if(tmp->type == IS_NUMERIC){
return tmp->value;
} else {
return tmp->stringVal;
}
}
/**
* Generic method to set the value of a node on the given path.
* @param bt
* @param path
* @param type
* @param value
*/
void SetValue(bTree *bt, char** path, int type, void* value){
bTNode* tmp = FindWithPath(bt, path);
if(tmp == NULL){
printf("node does not exist\n");
return;
}
if(tmp->type != type) {
printf("node is of wrong type\n");
return;
}
if(type==IS_STRING) {
char* new = (char*)value;
tmp->stringVal = realloc(tmp->stringVal, strlen(new)+1);
strcpy(tmp->stringVal, new);
puts(tmp->stringVal);
return;
}
tmp->value = value;
}
/**
* Search for the given node, from the given folder.
* @return
*/
bTNode *Find(bTNode* node, char *name) {
if (node->nrOfChildNodes != 0) {
int s = node->nrOfChildNodes;
int m = node->nrOfChildNodes / 2;
bTNode* tmp = node->childNodes[m];
int diff = strcmp(name, tmp->name);
if (diff == 0) {
return node->childNodes[m];
}
else if (diff > 0) {
for (int i = m; i < s; i++) {
tmp = node->childNodes[i];
if(strcmp(tmp->name, name) == 0){
return node->childNodes[i];
}
}
}
for (int i = 0; i < (m + 1); i++) {
tmp = node->childNodes[i];
if (strcmp(tmp->name, name) == 0) {
return node->childNodes[i];
}
}
}
return NULL;
}
/**
* Find with path
* @param bt
* @param path
* @return
*/
bTNode* FindWithPath(bTree* bt, char **path){
bTNode* tmp = bt->root;
if(strcmp(path[1], END_OF_PATH)==0&&strcmp(path[0], "root")==0){
return tmp;
}
while(strcmp(*path, END_OF_PATH)!=0){
tmp = Find(tmp, *path);
if(tmp == NULL){
return NULL;
}
path++;
}
return tmp;
}
/**
* Delete node on the given path if it exists
* If the node to delete is at root level use ["nameOfNode", "END_OF_PATH"]
* @param path
* @param depth
*/
void BTreeDelete(bTree* bt ,char** path){
bTNode *tmp;
// If path[1] == END_OF_PATH path[0] = name of node
if(strcmp(path[1], END_OF_PATH)==0){
tmp = Find(bt->root, path[0]);
}
else{
tmp = FindWithPath(bt, path);
}
if(tmp == NULL) {
printf("node does not exist\n");
return;
}
else{
DeleteNode(tmp->parent, tmp->name);
}
}
/**
* Recursive method, delete the given node from its parent, if parent(folder) becomes empty delete it.
* @param parent
* @param key
*/
void DeleteNode(bTNode *parent, char* key){
bTNode** childNodes = parent->childNodes;
int s = parent->nrOfChildNodes;
// find the node to delete, and fix the possible gap in the sorted array
int i = 0;
for(; i<s; i++){
if(strcmp(childNodes[i]->name, key)==0){
bTNode* tmp = childNodes[i];
childNodes[i] = NULL;
if(tmp->type==IS_FOLDER) {
FreeSubTree(tmp);
}
else{
FreeNode(tmp);
}
parent->nrOfChildNodes--;
break;
}
}
// If the deletion resulted in a gap, move the section between (i+1) and s one step back
if(i<(s-1)){
memmove(&parent->childNodes[i], &parent->childNodes[i + 1], (s - i) * sizeof(bTNode *));
}
// shrink the size of the array if N<SIZE/2, capacity>10
if(parent->childNodeCapacity > 10 && (s*2) < parent->childNodeCapacity){
parent->childNodes = realloc(parent->childNodes, (parent->childNodeCapacity/2) * sizeof(bTNode*));
parent->childNodeCapacity = parent->childNodeCapacity/2;
if(parent->childNodes == NULL){
exit(EXIT_FAILURE);
}
}
if(parent->nrOfChildNodes==0){
// Don't delete the root node..
if(parent->parent != NULL) {
DeleteNode(parent->parent, parent->name);
}
}
}
/**
* Recursively free current subtree
* @return
*/
void FreeSubTree(bTNode* current){
if(current->type==IS_FOLDER){
if(current->nrOfChildNodes != 0){
for(int i=0; i<current->nrOfChildNodes; i++){
FreeSubTree(current->childNodes[i]);
}
}
}
FreeNode(current);
}
void FreeNode(bTNode *node){
if(node->type==IS_STRING){
free(node->stringVal);
}
if(node->type == IS_FOLDER){
free(node->childNodes);
}
free(node->name);
free(node);
}
void FreeBTree(bTree *bt){
FreeSubTree(bt->root);
free(bt);
}