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

refactor

parent 71cbafd4
No related branches found
No related tags found
No related merge requests found
......@@ -152,31 +152,33 @@ control bp_split_unleaf(node *nd) {
return new;
}
// INSERT ----------------------------------------------------------
control bp_insert_into(node *nd, control val) {
void bp_do_the_insert(node *nd, control val) {
// look for a 0 in the node (it is guarenteed to exist in this version of
// the algorithm)
for (int i = 0; i < M; i++) {
// if we plainly find a 0
if (nd->data[i] == 0) {
nd->data[i] = val.value;
// nd->childs[i] = val.lhs;
nd->childs[i + 1] = val.rhs;
break;
}
// if we need to insert the value between two values, we need to
// shifter everything greater than our value one case further
// shift 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;
nd->childs[i + 1] = val.rhs;
break;
}
}
nd->count++;
}
// INSERT ----------------------------------------------------------
control bp_insert_into(node *nd, control val) {
bp_do_the_insert(nd, val);
// if the node is now full, we split
if (nd->count == M) {
control v;
......@@ -213,26 +215,10 @@ control bp_insert(node *nd, control val, int depth) {
break;
}
}
// treat the control return here
// if a split happened and we are not the root
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++;
bp_do_the_insert(nd, c);
// if after the child split, the node is full, split the node
if (nd->count == M)
......@@ -252,33 +238,16 @@ control bp_insert(node *nd, control val, int depth) {
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);
/*
depending on the control item, we act differently
if the control has pointers to node, then we had a split in the direct
childs of the root. We must add the values upringed to the root
childs of the root. We must add the values upringed to the root
*/
if (test.rhs != NULL) {
/*
if the root isn't a leaf, first we add the value into the node
we can't use bp_insert_into() as if we need a split, the way we
do it is slightly different
*/
// if the root isn't a leaf, first we add the value into the node
if (!bp_is_leaf(tree)) {
for (int i = 0; i < M; i++) {
if (tree->data[i] == 0) {
tree->childs[i + 1] = test.rhs;
tree->data[i] = test.value;
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++;
bp_do_the_insert(tree, test);
// If the root is full, we must split it
if (tree->count == M) {
return bp_split_root(tree);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment