diff --git a/bp_tree.c b/bp_tree.c index 3684fcfa42f8341ef5cfbf7ae33a7f0a8d991e3e..376658c9137204db9d0ed8801b37e82f70ee7262 100644 --- a/bp_tree.c +++ b/bp_tree.c @@ -15,6 +15,7 @@ typedef struct control { } control; const control CONST_CONTR = {.value = 0, .lhs = NULL, .rhs = NULL}; +const entry CONST_ENTR = {.phone = "ERROR\0"}; node *bp_create_node() { node *nd = malloc(sizeof(node)); @@ -25,7 +26,6 @@ node *bp_create_node() { nd->data[i] = 0; nd->count = 0; nd->next = NULL; - nd->user = NULL; return nd; } @@ -155,13 +155,20 @@ control bp_split_unleaf(node *nd) { return new; } -void bp_do_the_insert(node *nd, control val) { +void bp_do_the_insert(node *nd, control val, entry person) { // 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 (nd->data[i] == val.value) { + printf("Cette entrée existe déjà\n"); + return; + } + // if we plainly find a 0 if (nd->data[i] == 0) { nd->data[i] = val.value; + nd->user[i] = person; nd->childs[i + 1] = val.rhs; break; } @@ -171,6 +178,7 @@ void bp_do_the_insert(node *nd, control val) { if (nd->data[i] > val.value) { bp_node_shift(nd, i); nd->data[i] = val.value; + nd->user[i] = person; nd->childs[i + 1] = val.rhs; break; } @@ -180,8 +188,8 @@ void bp_do_the_insert(node *nd, control val) { // INSERT ---------------------------------------------------------- -control bp_insert_into(node *nd, control val) { - bp_do_the_insert(nd, val); +control bp_insert_into(node *nd, control val, entry person) { + bp_do_the_insert(nd, val, person); // if the node is now full, we split if (nd->count == M) { control v; @@ -203,25 +211,25 @@ control bp_insert_into(node *nd, control val) { return CONST_CONTR; } -control bp_insert(node *nd, control val, int depth) { +control bp_insert(node *nd, control val, int depth, entry person) { control c; // will help us to return the pivot, and the two pointers of the // childs-to-be // if we are in a leaf, we found where to insert the value, so let's do it if (bp_is_leaf(nd)) - c = bp_insert_into(nd, val); + c = bp_insert_into(nd, val, person); else // otherwise let's keep looking { for (int i = 0; i < M; i++) { if (nd->data[i] > val.value || nd->data[i] == 0) { - c = bp_insert(nd->childs[i], val, depth + 1); + c = bp_insert(nd->childs[i], val, depth + 1, person); break; } } // treat the control return here // if a split happened and we are not the root if (c.value != 0 && depth != 0) { - bp_do_the_insert(nd, c); + bp_do_the_insert(nd, c, person); // if after the child split, the node is full, split the node if (nd->count == M) @@ -242,7 +250,7 @@ node *bp_insert_val(node *tree, entry person) { uint64_t hash_i = generate_key(person.phone); // Parse the val into a control struct then insert it in the tree - control test = bp_insert(tree, value_to_insert(hash_i), 0); + control test = bp_insert(tree, value_to_insert(hash_i), 0, person); /* depending on the control item, we act differently if the control has pointers to node, then we had a split in the direct @@ -252,7 +260,7 @@ node *bp_insert_val(node *tree, entry person) { // if the root isn't a leaf, first we add the value into the node if (!bp_is_leaf(tree)) { - bp_do_the_insert(tree, test); + bp_do_the_insert(tree, test, person); // If the root is full, we must split it if (tree->count == M) { return bp_split_root(tree); @@ -273,6 +281,33 @@ node *bp_insert_val(node *tree, entry person) { return tree; } +// SEARCH ---------------------------------------------------------- + +entry bp_search_node(node *tree, uint64_t hash) { + if (tree == NULL) { + return CONST_ENTR; + } + + if (bp_is_leaf(tree)) { + for (int i = 0; i < M; i++) { + if (hash == tree->data[i]) + return tree->user[i]; + } + } else { + for (int i = 0; i <= M; i++) { + if (tree->data[i] > hash) { + return bp_search_node(tree->childs[i], hash); + } + } + } + return CONST_ENTR; +} + +void bp_search(node *tree, char phone[11]) { + uint64_t hash_i = generate_key(phone); + bp_print_entry(bp_search_node(tree, hash_i)); +} + // MEMORY ---------------------------------------------------------- void bp_destroy(node *root) { @@ -292,6 +327,67 @@ void bp_destroy(node *root) { // PRINTING ---------------------------------------------------------- +void print_date(date item) { + printf("%d - ", item.day); + + switch (item.month) { + case 1: + printf("JANVIER"); + break; + case 2: + printf("FEVRIER"); + break; + case 3: + printf("MARS"); + break; + case 4: + printf("AVRIL"); + break; + case 5: + printf("MAI"); + break; + case 6: + printf("JUIN"); + break; + case 7: + printf("JUILLET"); + break; + case 8: + printf("AOUT"); + break; + case 9: + printf("SEPTEMBRE"); + break; + case 10: + printf("OCTOBRE"); + break; + case 11: + printf("NOVEMBRE"); + break; + case 12: + printf("DECEMBRE"); + break; + default: + break; + } + + printf(" - %d\n", item.year + 1900); +} + +void bp_print_entry(entry person) { + + if (person.phone == CONST_ENTR.phone) { + printf("Cette personne n'existe pas\n"); + return; + } + + printf("NOM : %s\n", person.l_name); + printf("PRENOM : %s\n", person.f_name); + printf("TELEPHONE : %s\n", person.phone); + printf("DATE : "); + print_date(person.birth); +} + void bp_print_as_ll(node *root) { // if we reached the end of the list if (root == NULL) { @@ -342,4 +438,4 @@ void bp_print(node *root, int depth) { if (root->data[i + 1] == 0) return bp_print(root->childs[i + 1], depth + 1); } -} \ No newline at end of file +} diff --git a/bp_tree.h b/bp_tree.h index 3ef45d416c9ab2f62e6947d4192d2516f3687006..f04e9b97c953b95f919b44d39f9b38364a70f305 100644 --- a/bp_tree.h +++ b/bp_tree.h @@ -10,7 +10,7 @@ typedef struct date { uint8_t day; - uint8_t month; // 0 = January, 11 = December + uint8_t month; // 1 = January, 12 = December uint8_t year; // value stored is year - 1900 } date; @@ -26,7 +26,7 @@ typedef struct node { uint64_t data[M]; int count; struct node *next; - struct entry *user; + struct entry user[M]; } node; /** @@ -37,6 +37,10 @@ typedef struct node { */ void bp_print(node *root, int depth); +void bp_print_entry(entry person); + +void bp_search(node *tree, char phone[11]); + /** * @brief Free an entire B+ Tree * diff --git a/main.c b/main.c index 4648af83bd2c3660a2297340502e18e0f164a1a0..3d2cafb4f30d312ec3ff9989f8e450892eb81557 100644 --- a/main.c +++ b/main.c @@ -38,10 +38,25 @@ int main() { person.phone[i] = '4'; } + date test = {.day = 26, .month = 5, .year = 101}; + + person.l_name[0] = 'S'; + person.f_name[0] = 'A'; + person.f_name[1] = 'l'; + person.f_name[2] = 'e'; + person.f_name[3] = 'c'; + person.f_name[4] = '\0'; + person.birth = test; tree = bp_insert_val(tree, person); bp_print(tree, 0); + for (int i = 0; i < 10; i++) { + person.phone[i] = '5'; + } printf("\n|||||||||\n"); + printf("Recherche par n° de téléphone : \n"); + bp_search(tree, person.phone); + /* tree = bp_insert_val(tree, 6); bp_print(tree, 0);