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

INSERT WORKS

parent ff26e3fb
Branches
No related tags found
No related merge requests found
......@@ -119,12 +119,42 @@ control bp_insert(node *nd, control val){
return CONST_CONTR;
}
node* bp_split_root(node *root)
{
node *new = bp_create_node();
node *rhs = bp_create_node();
rhs->childs[0] = root->childs[3];
rhs->childs[1] = root->childs[4];
root->childs[3] = NULL;
root->childs[4] = NULL;
rhs->data[0] = root->data[3];
root->data[3] = 0;
new->childs[0] = root;
new->childs[1] = rhs;
new->data[0] = root->data[2];
root->data[2] = 0;
new->count = 1;
root->count = M/2;
rhs->count = M/2;
return new;
}
node* bp_insert_val(node* tree, int val)
{
control test = bp_insert(tree, value_to_insert(val));
if ( test.rhs != NULL)
{
/*
if (!bp_is_leaf(tree))
{
for (int i = 0; i < M; i++)
{
if (tree->data[i] == 0)
......@@ -132,21 +162,36 @@ node* bp_insert_val(node* tree, int val)
tree->childs[i+1] = test.rhs;
tree->data[i] = test.value;
tree->count++;
break;
}
}
/* GROS ICI SI LA RACINE N'EST PAS UNE FEUILLE ET QU'APRES AVOIR REMONTE UNE VALEUR LA RACINE EST PLEINE, IL FAUT SPLIT LA RACINE */
printf("%d\n", tree->count);
if (tree->count == M)
{
printf("prout\n");
return bp_split_root(tree);
}
return tree;
}
}*/
tree = bp_create_node();
tree->childs[0] = test.lhs;
tree->childs[1] = test.rhs;
tree->data[0] = test.value;
tree->count = M/2;
tree->count = 1;
}
return tree;
}
void bp_destroy(node *nd)
{
if (nd == NULL)
return;
if (!bp_is_leaf(nd))
{
for (int i = 0; i < M; i++)
......@@ -158,25 +203,62 @@ void bp_destroy(node *nd)
free(nd);
}
void bp_print(node *nd, int depth)
void bp_print_as_ll(node *nd)
{
if (nd == NULL)
{
printf("NULL\n");
return;
}
if (!bp_is_leaf(nd))
{
for(int i = 0; i < nd->count; i++){
//printf("i = %d\n", i);
if(!bp_is_leaf(nd)){
//printf("is not leaf = %d\n", d);
return bp_print_as_ll(nd->childs[0]);
}
for (int i = 0; i < M; i++)
{
printf(" %d |", nd->data[i]);
if (nd->data[i+1] == 0)
{
printf("->");
return bp_print_as_ll(nd->next);
}
}
}
void bp_print(node *nd, int depth)
{
if (bp_is_leaf(nd))
{
for (int i = 0; i < depth; i++)
printf(" ");
for (int i = 0; i < M; i++)
{
printf(" %d |", nd->data[i]);
if (nd->data[i+1] == 0)
break;
}
printf("\n");
return;
}
for (int i = 0; i < M; i++)
{
printf("\n");
bp_print(nd->childs[i], depth + 1);
printf(" \n");
printf(" \n");
}
for(int k = 0; k < depth; ++k){
for (int i = 0; i < depth; i++)
printf(" ");
}
printf(" %d |", nd->data[i]);
printf("| %d |",nd->data[i]);
}
if (nd->data[i+1] == 0)
{
printf("\n");
return bp_print(nd->childs[i+1], depth + 1);
}
}
}
\ No newline at end of file
......@@ -25,4 +25,5 @@ void bp_destroy(node *nd);
control bp_insert(node *nd, control val);
node* bp_create_node();
node* bp_insert_val(node* tree, int val);
void bp_print_as_ll(node *nd);
#endif
\ No newline at end of file
......@@ -4,6 +4,31 @@
int main(){
/*
node *test = bp_create_node();
test->count = 1;
test->data[0] = 3;
for (int i = 0; i < 2; i++)
{
test->childs[i] = bp_create_node();
test->childs[i]->count = 1;
test->childs[i]->data[0] = 3;
for (int y = 0; y < 2; y++)
{
test->childs[i]->childs[y] = bp_create_node();
for (int z = 0; z < 2; z++)
{
test->childs[i]->childs[y]->data[z] = 3;
}
test->childs[i]->childs[y]->count = 2;
}
}
bp_print(test, 0);
bp_destroy(test);*/
node *tree = bp_create_node();
tree = bp_insert_val(tree, 3);
......@@ -23,7 +48,19 @@ int main(){
printf("|||||||||\n");
tree = bp_insert_val(tree, 8);
bp_print(tree, 0);
printf("|||||||||\n");
tree = bp_insert_val(tree, 9);
bp_print(tree, 0);
printf("|||||||||\n");
tree = bp_insert_val(tree, 10);
bp_print(tree, 0);
printf("|||||||||\n");
tree = bp_insert_val(tree, 11);
bp_print(tree, 0);
printf("|||||||||\n");
tree = bp_insert_val(tree, 12);
bp_print(tree, 0);
bp_print_as_ll(tree);
bp_destroy(tree);
return 0;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment