feat: implement binary search tree for student records with insertion and display functions#1
Conversation
… and display functions
There was a problem hiding this comment.
Pull request overview
This PR adds a binary search tree (BST) structure for student records (sorted by moyenne) and integrates BST-based sorted display into the application and test suite.
Changes:
- Added a new
arbres_binairemodule implementing BST node/root creation, insertion, and node cleanup. - Added BST-based sorted display (
afficher_arbres_trie) and wired menu option #6 to build a temporary BST and print students in descending order. - Extended
test_student.cwith multiple BST-focused tests.
Reviewed changes
Copilot reviewed 7 out of 8 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
arbres_binaire.h |
Declares BST node/root types and BST API for students. |
arbres_binaire.c |
Implements BST node/root allocation, recursive insertion, and node freeing. |
student.c |
Adds display_student() and implements sorted BST traversal/printing (afficher_arbres_trie). |
student.h |
Exposes display_student() in the public header. |
main.c |
Replaces list merge-sort call with temporary BST build + sorted display in menu option #6. |
test_student.c |
Adds multiple BST unit/integration-style tests. |
CMakeLists.txt |
Adds arbres_binaire.* to the main executable sources. |
myApp |
Adds a compiled binary artifact to the repository. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| while (courent != NULL && strcmp(courent->CNE, cne) != 0) { | ||
| trouver = 1; | ||
| printf("\n"); | ||
| printf("+--------------------------------------------+\n"); | ||
| printf("| INFORMATION ETUDIANT |\n"); // J'ai ajouté %-2d pour l'alignement | ||
| printf("+--------------------------------------------+\n"); | ||
| printf("| CNE : %-25.25s |\n", courent->CNE); | ||
| printf("| Nom : %-25.25s |\n", courent->nom); | ||
| printf("| Prenom : %-25.25s |\n", courent->prenom); | ||
| printf("| Date Naissance : %02d/%02d/%-19d |\n", courent->date_naissance.jour, | ||
| courent->date_naissance.mois, courent->date_naissance.annee); | ||
| printf("| Filiere : %-25.25s |\n", courent->filiere); | ||
| printf("| Moyenne : %-25.2f |\n", courent->moyenne); | ||
| printf("+--------------------------------------------+\n"); | ||
| courent = courent->next; | ||
| } | ||
| if (trouver == 0) { | ||
| printf("etudiant n'est pas trouver!!!\n"); | ||
| } |
There was a problem hiding this comment.
display_student(): the search loop is inverted—it's printing students while strcmp(...) != 0 and sets trouver=1 for non-matches, so it prints the wrong records and reports "not found" when the first element matches. Iterate through the list until a match is found; only print when strcmp(...)==0 and set trouver when a match is found (or break and handle "not found" after the loop).
| ab_racine_student *temp_tree = create_ab_racine(); | ||
| // temp_tree->racine = NULL; | ||
|
|
||
| student *current_s = my_db->tete; | ||
| while (current_s != NULL) { | ||
| ab_node_student *new_node = create_ab_node(current_s); | ||
| insert_bst(temp_tree, new_node); | ||
| current_s = current_s->next; | ||
| } | ||
|
|
||
| afficher_arbres_trie(temp_tree); | ||
| free_bst_nodes(temp_tree->racine); | ||
| break; |
There was a problem hiding this comment.
case 6: temp_tree is never freed, so each sort operation leaks memory. Also create_ab_racine() can return NULL; in that case free_bst_nodes(temp_tree->racine) will dereference NULL. Check temp_tree != NULL before use, and free(temp_tree) after freeing its nodes (or add a helper that frees both root and wrapper).
| @@ -0,0 +1,25 @@ | |||
| #ifndef MINI_DATABASE_ARBRES_BINAIRE_H | |||
| #define MINI_DATABASE_ARBRES_BINAIRE_H | |||
| #include "undo_stack.h" | |||
There was a problem hiding this comment.
arbres_binaire.h includes undo_stack.h, but the tree API only needs the student type. Including undo_stack.h creates unnecessary coupling and can introduce circular dependencies; include student.h (or forward-declare struct student) instead and keep undo stack concerns separate.
| #include "undo_stack.h" | |
| #include "student.h" |
| @@ -0,0 +1,57 @@ | |||
| #include <stdlib.h> | |||
There was a problem hiding this comment.
arbres_binaire.c starts with a UTF-8 BOM before the #include ("#include..."). This can break compilation on some toolchains; remove the BOM so the file begins directly with #include.
| #include <stdlib.h> | |
| #include <stdlib.h> |
| ab_node_student *insert_recursive_worker(ab_node_student *currant, ab_node_student *new_node) { | ||
| if (currant == NULL) return new_node; | ||
|
|
||
| if (new_node->std->moyenne < currant->std->moyenne) { | ||
| currant->filsg = insert_recursive_worker(currant->filsg, new_node); | ||
| } else { | ||
| currant->filsd = insert_recursive_worker(currant->filsd, new_node); | ||
| } | ||
| return currant; | ||
| } |
There was a problem hiding this comment.
insert_recursive_worker() is not declared in the header but is currently a global symbol. Since it is only used inside arbres_binaire.c, mark it static to avoid exporting an unintended API surface and reduce the risk of symbol collisions.
| void display_student(list_student *list_student, char *cne) { | ||
| if (list_student == NULL || list_student->tete == NULL) { | ||
| printf("la Base de donnee et vide!!!\n"); | ||
| } | ||
| int trouver = 0; | ||
| student *courent = list_student->tete; |
There was a problem hiding this comment.
display_student(): after printing that the database is empty, the function continues and dereferences list_student->tete, which will crash when list_student==NULL. Add an early return when list_student==NULL or list_student->tete==NULL (and consider validating cne as well).
No description provided.