diff --git a/bp_tree.c b/bp_tree.c index f7cbb7d0d282bb5b8b365b5f51a064e063a72ade..4ada116cc3902aff2e0b0d1071d6361d9b890fa7 100644 --- a/bp_tree.c +++ b/bp_tree.c @@ -4,6 +4,8 @@ #include <stdio.h> #include <stdlib.h> +// UTILS ---------------------------------------------------------- + typedef struct control { int value; node *lhs; @@ -64,18 +66,59 @@ node *bp_split(node *nd) { upto++; new->count++; } - + // no need in this situation to reassign child pointers as there is no + // childs for leaves for (int i = 0; i < upto; i++) { new->data[i] = nd->data[index + i]; nd->data[index + i] = 0; - // new->childs[i] = nd->childs[index + i]; - // nd->childs[index + i] = NULL; } return new; } +node *bp_split_root(node *root) { + node *new = bp_create_node(); // will be the new root + node *rhs = bp_create_node(); // second child of the new root + + int index = (int)floor(M / 2); // index to the pivot + int looper = index; /* How many runs of our for loops we'll need to do + (different to index in the case our tree order is even)*/ + + new->count = 1; + root->count = index; + rhs->count = index; + + if (M % 2 == 0) { + looper--; + rhs->count--; + } + + // first assing the child pointers + for (int i = 0; i <= looper; i++) { + rhs->childs[i] = root->childs[index + i + 1]; + root->childs[index + i + 1] = NULL; + } + + // then assing the datas + for (int i = 0; i < looper; i++) { + rhs->data[i] = root->data[index + i + 1]; + root->data[index + i + 1] = 0; + } + + new->childs[0] = root; + new->childs[1] = rhs; + new->data[0] = root->data[index]; + + root->data[index] = 0; + + return new; +} + control bp_split_unleaf(node *nd) { + // This one is used to split a node that is neither a leaf or a root + // it works exactly like the split of a root except it doesn't return a node + // that becomes the new root + // it instead returns a control value to send to the parent control new; node *rhs = bp_create_node(); @@ -109,6 +152,8 @@ control bp_split_unleaf(node *nd) { return new; } +// INSERT ---------------------------------------------------------- + control bp_insert_into(node *nd, control val) { // look for a 0 in the node (it is guarenteed to exist in this version of // the algorithm) @@ -142,9 +187,12 @@ control bp_insert_into(node *nd, control val) { // if the new node we created is a leaf, we must add it to the // linked list - if (bp_is_leaf(new)) - nd->next = new; + if (bp_is_leaf(new)) { + if (nd->next != NULL) + new->next = nd->next; + nd->next = new; + } return v; } return CONST_CONTR; @@ -186,29 +234,9 @@ control bp_insert(node *nd, control val, int depth) { } nd->count++; - // YA PLUS QU'A SPLIT ICI SI BESOIN - if (nd->count == M) { - - /* - node *new = bp_create_node(); - new->count = 1; - new->childs[0] = nd->childs[3]; - new->childs[1] = nd->childs[4]; - new->data[0] = nd->data[3]; - - control test; - test.value = nd->data[2]; - test.lhs = nd; - test.rhs = new; - - nd->childs[3] = NULL; - nd->childs[4] = NULL; - nd->data[3] = 0; - nd->data[2] = 0; - - return test;*/ + // if after the child split, the node is full, split the node + if (nd->count == M) return bp_split_unleaf(nd); - } return CONST_CONTR; } @@ -221,41 +249,6 @@ control bp_insert(node *nd, control val, int depth) { return CONST_CONTR; } -node *bp_split_root(node *root) { - node *new = bp_create_node(); - node *rhs = bp_create_node(); - - int index = (int)floor(M / 2); - int looper = index; - - new->count = 1; - root->count = index; - rhs->count = index; - - if (M % 2 == 0) { - looper--; - rhs->count--; - } - - for (int i = 0; i <= looper; i++) { - rhs->childs[i] = root->childs[index + i + 1]; - root->childs[index + i + 1] = NULL; - } - - for (int i = 0; i < looper; i++) { - rhs->data[i] = root->data[index + i + 1]; - root->data[index + i + 1] = 0; - } - - new->childs[0] = root; - new->childs[1] = rhs; - new->data[0] = root->data[index]; - - root->data[index] = 0; - - return new; -} - node *bp_insert_val(node *tree, int val) { // Parse the val into a control struct then insert it in the tree control test = bp_insert(tree, value_to_insert(val), 0); @@ -276,11 +269,16 @@ node *bp_insert_val(node *tree, int val) { if (tree->data[i] == 0) { tree->childs[i + 1] = test.rhs; tree->data[i] = test.value; - tree->count++; + break; + } + if (tree->data[i] > test.value) { + bp_node_shift(tree, i); + tree->data[i] = test.value; + tree->childs[i + 1] = test.rhs; break; } } - + tree->count++; // If the root is full, we must split it if (tree->count == M) { return bp_split_root(tree); @@ -301,6 +299,8 @@ node *bp_insert_val(node *tree, int val) { return tree; } +// MEMORY ---------------------------------------------------------- + void bp_destroy(node *root) { if (root == NULL) return; @@ -316,6 +316,8 @@ void bp_destroy(node *root) { free(root); } +// PRINTING ---------------------------------------------------------- + void bp_print_as_ll(node *root) { // if we reached the end of the list if (root == NULL) { diff --git a/bp_tree.h b/bp_tree.h index 606583ed2977f99bcb23f041eff4029950262f40..74c769d5d8b94e3c9ef6e5c294b3f9ee8143fa3a 100644 --- a/bp_tree.h +++ b/bp_tree.h @@ -10,15 +10,7 @@ typedef struct node { int count; struct node *next; } node; -/* -typedef struct cell { -} cell; - -typedef struct page { - struct -} page; -*/ /** * @brief Print in CLI a B+ Tree * diff --git a/main.c b/main.c index 60bad1abfed069fb1254aaa6c09f1dc71013514f..acbcdcdeef79380c70866eee9bd349d05e9ae902 100644 --- a/main.c +++ b/main.c @@ -7,48 +7,63 @@ int main() { tree = bp_insert_val(tree, 3); bp_print(tree, 0); + printf("\n|||||||||\n"); - tree = bp_insert_val(tree, 4); + tree = bp_insert_val(tree, 6); bp_print(tree, 0); + printf("\n|||||||||\n"); tree = bp_insert_val(tree, 5); bp_print(tree, 0); + printf("\n|||||||||\n"); - tree = bp_insert_val(tree, 6); + tree = bp_insert_val(tree, 4); bp_print(tree, 0); + printf("\n|||||||||\n"); - tree = bp_insert_val(tree, 7); + tree = bp_insert_val(tree, 8); bp_print(tree, 0); + printf("\n|||||||||\n"); - tree = bp_insert_val(tree, 8); + tree = bp_insert_val(tree, 13); bp_print(tree, 0); + printf("\n|||||||||\n"); tree = bp_insert_val(tree, 9); bp_print(tree, 0); + printf("\n|||||||||\n"); - tree = bp_insert_val(tree, 10); + tree = bp_insert_val(tree, 15); bp_print(tree, 0); + printf("\n|||||||||\n"); tree = bp_insert_val(tree, 11); bp_print(tree, 0); + printf("\n|||||||||\n"); tree = bp_insert_val(tree, 12); bp_print(tree, 0); + printf("\n|||||||||\n"); - tree = bp_insert_val(tree, 13); + tree = bp_insert_val(tree, 7); bp_print(tree, 0); + printf("\n|||||||||\n"); tree = bp_insert_val(tree, 14); bp_print(tree, 0); + printf("\n|||||||||\n"); - tree = bp_insert_val(tree, 15); + tree = bp_insert_val(tree, 10); bp_print(tree, 0); + printf("\n|||||||||\n"); tree = bp_insert_val(tree, 16); bp_print(tree, 0); + printf("\n|||||||||\n"); tree = bp_insert_val(tree, 17); bp_print(tree, 0); + printf("\n|||||||||\n"); tree = bp_insert_val(tree, 18); bp_print(tree, 0);