1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| #include <stdio.h>
#include <stdlib.h>
#define NEW(x) (x*)malloc(sizeof(x))
#define BOOLEEN int
#define VRAI 0
#define FAUX 1
#define pNoeud NOEUD *
typedef struct {
int numPersonne;
char *nom;
char *prenom;
int poids;
float note;
} PERSONNE;
// ==================================================================================================================================
typedef struct bidon {
void *OBJET;
struct bidon *FilsGauche;
struct bidon *FilsDroit;
} NOEUD;
typedef struct {
NOEUD *racine;
} ARBRE;
static void _ajoutFeuille (void *objet, pNoeud * n) {
*n=(pNoeud)malloc(sizeof(NOEUD));
(*n)->OBJET = objet;
(*n)->FilsGauche = NULL;
(*n)->FilsDroit = NULL;
}
NOEUD * filsGauche (NOEUD *n) {
return (n->FilsGauche);
}
NOEUD * filsDroit (NOEUD *n) {
return (n->FilsDroit);
}
void insertArbre(NOEUD *n, void *objet,int (*comparElem)(void *, void*)) {
if(n == NULL)
_ajoutFeuille (objet,&n);
else {
if (comparElem(objet,n->OBJET) == -1)
insertArbre(filsGauche(n),objet,comparElem);
else
insertArbre(filsDroit(n),objet,comparElem);
}
}
int comparPersNom (void *pers1, void *pers2) {
PERSONNE *p1 = (PERSONNE *)pers1;
PERSONNE *p2 = (PERSONNE *)pers2;
return strcmp(p1,p2);
}
// Interface
int main(int argc, char *argv[]) {
int i;
ARBRE *arbre = NEW(ARBRE);
arbre->racine = NULL;
PERSONNE _tb[] = {
{1973,"Seth","Dominique",78,7.5},
{1968,"Enoch","Abdelkader",14,8.25},
{1418,"Irad","Igor",71,12.5},
{1515,"Adam","Constantin",68,12.5},
{1111,"Henosh","Marcel",73,13.},
{6699,"Noe","Georges",27,8.5},
{1984,"Cain","Apollinaire",45,8.25},
{1789,"Abel","Aristide",18,11.75},
{9669,"Sem","Maurice",43,7.5},
{3945,"Cham","Leonardo",99,12.5},
{3141,"Japhet","Aziz",66,5.25}
};
for (i=0;i<2;i++) {
//verif = ajoutElem (arbre, &(_tb[i]), compareNom);
//verif = ajouterFeuille (&(_tb[i]), &(arbre->racine), compareNom);
insertArbre(arbre->racine,&(_tb[i]),comparPersNom);
}
if (arbre->racine) {
PERSONNE *p = arbre->racine->OBJET;
printf("Nom : %s\n",p->nom);
}
else
printf("Pb d'allocation\n");
//affichArbre(arbre,affichPersonne);
} |
Partager