Skip to content
Snippets Groups Projects
Commit 71cbafd4 authored by Alec's avatar Alec
Browse files

encore des bugfix

parent a85cf7ea
Branches
No related tags found
No related merge requests found
......@@ -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) {
......
......@@ -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
*
......
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment