-
Notifications
You must be signed in to change notification settings - Fork 0
feat: implement binary search tree for student records with insertion and display functions #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <stdio.h> | ||
| #include "arbres_binaire.h" | ||
|
|
||
| ab_node_student *create_ab_node(student *student) { | ||
| if (student == NULL) return NULL; | ||
|
|
||
| ab_node_student *node = (ab_node_student *) malloc(sizeof(ab_node_student)); | ||
| if (node == NULL) { | ||
| printf("Erreur d'allocation memoire!\n"); | ||
| return NULL; | ||
| } | ||
|
|
||
| node->std = student; | ||
|
|
||
| node->filsd = NULL; | ||
| node->filsg = NULL; | ||
|
|
||
| return node; | ||
| } | ||
|
|
||
| ab_racine_student *create_ab_racine() { | ||
| ab_racine_student *racine = (ab_racine_student *) malloc(sizeof(ab_racine_student)); | ||
| if (racine == NULL) { | ||
| printf("Erreur d'allocation memoire!\n"); | ||
| return NULL; | ||
| } | ||
| racine->racine = NULL; | ||
| return racine; | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
|
Comment on lines
+33
to
+42
|
||
|
|
||
| void insert_bst(ab_racine_student *racine, ab_node_student *node) { | ||
| if (racine == NULL || node == NULL) return; | ||
|
|
||
| racine->racine = insert_recursive_worker(racine->racine, node); | ||
| } | ||
|
|
||
| void free_bst_nodes(ab_node_student *node) { | ||
| if (node == NULL) return; | ||
|
|
||
| free_bst_nodes(node->filsg); | ||
| free_bst_nodes(node->filsd); | ||
|
|
||
| free(node); | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,25 @@ | ||||||
| #ifndef MINI_DATABASE_ARBRES_BINAIRE_H | ||||||
| #define MINI_DATABASE_ARBRES_BINAIRE_H | ||||||
| #include "undo_stack.h" | ||||||
|
||||||
| #include "undo_stack.h" | |
| #include "student.h" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
| #include <stdlib.h> | ||
| #include "arbres_binaire.h" | ||
| #include "undo_stack.h" | ||
| // #include <windows.h> // pour les Accents | ||
| /** | ||
|
|
@@ -15,11 +17,9 @@ int main() { | |
| list_student *my_db = creat_list_student(); | ||
| UndoStack *my_stack = create_undo_stack(); | ||
| load_database(my_db, "my_data.db"); | ||
|
|
||
| int choice = 0; | ||
| char cne_buffer[15]; | ||
|
|
||
|
|
||
| do { | ||
| printf("\n=== MINI DATABASE MENU ===\n"); | ||
| printf("1. Ajouter un etudiant\n"); | ||
|
|
@@ -87,11 +87,28 @@ int main() { | |
| search_student_by_cne(my_db, cne_buffer); | ||
| break; | ||
| case 6: | ||
| sort_students_by_grade(my_db); | ||
| // sort_students_by_grade(my_db); | ||
| if (my_db->tete == NULL) { | ||
| printf("La Base de donnee est vide, rien a trier.\n"); | ||
| break; | ||
| } | ||
|
|
||
| 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; | ||
|
Comment on lines
+96
to
108
|
||
| case 7: | ||
| printf("\nATTENTION: Cette action supprimera tous les etudiants!\n"); | ||
| printf("NB : les etudiants apres la suppression n'ajoute pas dans Historique!!!"); | ||
| printf("NB : les etudiants apres la suppression n'ajoute pas dans Historique!!!\n"); | ||
| printf("Etes-vous sur? (1=Oui, 0=Non): "); | ||
| int confirm; | ||
| scanf("%d", &confirm); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| #include <string.h> | ||
| #include <stdio.h> | ||
|
|
||
| #include "arbres_binaire.h" | ||
| #include "undo_stack.h" | ||
| /** | ||
| * @brief Crée et initialise une liste d'étudiants vide. | ||
|
|
@@ -90,6 +91,33 @@ void display_all_student(list_student *list_student) { | |
| } | ||
| } | ||
|
|
||
| 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; | ||
|
Comment on lines
+94
to
+99
|
||
| 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"); | ||
| } | ||
|
Comment on lines
+100
to
+118
|
||
| } | ||
|
|
||
| /** | ||
| * @brief Supprime un étudiant de la liste en utilisant son CNE. | ||
| * | ||
|
|
@@ -523,3 +551,35 @@ void sort_students_by_grade(list_student *list) { | |
|
|
||
| printf("Liste triee par moyenne avec succes.\n"); | ||
| } | ||
|
|
||
| static void display_student_arbre(student *student) { | ||
| printf("+--------------------------------------------+\n"); | ||
| printf("| INFORMATION ETUDIANT |\n"); // J'ai ajouté %-2d pour l'alignement | ||
| printf("+--------------------------------------------+\n"); | ||
| printf("| CNE : %-25.25s |\n", student->CNE); | ||
| printf("| Nom : %-25.25s |\n", student->nom); | ||
| printf("| Prenom : %-25.25s |\n", student->prenom); | ||
| printf("| Date Naissance : %02d/%02d/%-19d |\n", student->date_naissance.jour, | ||
| student->date_naissance.mois, student->date_naissance.annee); | ||
| printf("| Filiere : %-25.25s |\n", student->filiere); | ||
| printf("| Moyenne : %-25.2f |\n", student->moyenne); | ||
| printf("+--------------------------------------------+\n"); | ||
| } | ||
|
|
||
| static void sort_by_arbre_recursive(ab_node_student *node) { | ||
| if (node != NULL) { | ||
| sort_by_arbre_recursive(node->filsd); | ||
| display_student_arbre(node->std); | ||
| sort_by_arbre_recursive(node->filsg); | ||
| } | ||
| } | ||
|
|
||
| void afficher_arbres_trie(ab_racine_student *racine) { | ||
| if (racine == NULL || racine->racine == NULL) { | ||
| printf("L'arbre est vide.\n"); | ||
| return; | ||
| } | ||
| printf("\n=== Liste des etudiants triee par moyenne (Decroissant) ===\n"); | ||
| sort_by_arbre_recursive(racine->racine); | ||
| printf("===========================================================\n"); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.