From a85cf7ea179b27f23e09045d3cf51ac82888d88b Mon Sep 17 00:00:00 2001 From: Alec <alec.schmidt@hesge.ch> Date: Tue, 31 May 2022 15:53:57 +0200 Subject: [PATCH] =?UTF-8?q?derniers=20bugs=20corrig=C3=A9s,=20plus=20qu'?= =?UTF-8?q?=C3=A0=20refactorer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bp_tree.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++-------- bp_tree.h | 8 ++++ main.c | 13 ++++++- 3 files changed, 114 insertions(+), 18 deletions(-) diff --git a/bp_tree.c b/bp_tree.c index 493af2e..f7cbb7d 100644 --- a/bp_tree.c +++ b/bp_tree.c @@ -68,13 +68,47 @@ node *bp_split(node *nd) { 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; + // new->childs[i] = nd->childs[index + i]; + // nd->childs[index + i] = NULL; } return new; } +control bp_split_unleaf(node *nd) { + control new; + node *rhs = bp_create_node(); + + int index = (int)floor(M / 2); + int looper = index; + + nd->count = index; + rhs->count = index; + + if (M % 2 == 0) { + looper--; + rhs->count--; + } + + for (int i = 0; i <= looper; i++) { + rhs->childs[i] = nd->childs[index + i + 1]; + nd->childs[index + i + 1] = NULL; + } + + for (int i = 0; i < looper; i++) { + rhs->data[i] = nd->data[index + i + 1]; + nd->data[index + i + 1] = 0; + } + + new.lhs = nd; + new.rhs = rhs; + new.value = nd->data[index]; + + nd->data[index] = 0; + + return new; +} + 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) @@ -82,7 +116,8 @@ control bp_insert_into(node *nd, control val) { // if we plainly find a 0 if (nd->data[i] == 0) { nd->data[i] = val.value; - nd->childs[i] = val.lhs; + // nd->childs[i] = val.lhs; + nd->childs[i + 1] = val.rhs; break; } @@ -115,7 +150,7 @@ control bp_insert_into(node *nd, control val) { return CONST_CONTR; } -control bp_insert(node *nd, control val) { +control bp_insert(node *nd, control val, int depth) { control c; // will help us to return the pivot, and the two pointers of the // childs-to-be @@ -126,11 +161,59 @@ control bp_insert(node *nd, control val) { { for (int i = 0; i < M; i++) { if (nd->data[i] > val.value || nd->data[i] == 0) { - c = bp_insert(nd->childs[i], val); + c = bp_insert(nd->childs[i], val, depth + 1); break; } } + if (c.value != 0 && depth != 0) { + for (int i = 0; i < M; i++) { + // if we plainly find a 0 + if (nd->data[i] == 0) { + nd->data[i] = c.value; + // nd->childs[i] = val.lhs; + nd->childs[i + 1] = c.rhs; + break; + } + + // if we need to insert the value between two values, we need to + // shifter everything greater than our value one case further + if (nd->data[i] > val.value) { + bp_node_shift(nd, i); + nd->data[i] = val.value; + nd->childs[i] = val.rhs; + break; + } + } + 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;*/ + return bp_split_unleaf(nd); + } + + return CONST_CONTR; + } } + // if a split happened if (c.value != 0) { return c; @@ -139,16 +222,19 @@ control bp_insert(node *nd, control val) { } node *bp_split_root(node *root) { - // TEMPORARY FUNC (COPYRIGHT), HARD WRITTEN NEEDS TO BE SOFT 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++) { @@ -167,19 +253,12 @@ node *bp_split_root(node *root) { root->data[index] = 0; - new->count = 1; - root->count = index; - rhs->count = index; - if (M % 2 == 0) { - rhs->count--; - } - 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)); + control test = bp_insert(tree, value_to_insert(val), 0); /* depending on the control item, we act differently diff --git a/bp_tree.h b/bp_tree.h index 74c769d..606583e 100644 --- a/bp_tree.h +++ b/bp_tree.h @@ -10,7 +10,15 @@ 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 e2a7543..60bad1a 100644 --- a/main.c +++ b/main.c @@ -8,13 +8,13 @@ int main() { tree = bp_insert_val(tree, 3); 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, 5); 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, 7); @@ -44,6 +44,15 @@ int main() { tree = bp_insert_val(tree, 15); 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); + printf("\n|||||||||\n"); bp_print_as_ll(tree); bp_destroy(tree); return 0; -- GitLab