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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
/******************/
/* Les structures */
/******************/
//structure noeud
typedef struct noeud{
char valeur[10];
struct noeud * droite;
int fg;
struct noeud * gauche;
int fd;
}Noeud ;
/*************************/
/* Fonctions secondaires */
/*************************/
//Determination de la distance minimum
void mini(float mat[][5],int *t){
//variables
float min=1000.00;
int i,j;
//recherche du minimum
for(i=0;i<5;i++){
for(j=0;j<i;j++){
if(min>mat[i][j] && mat[i][j]!=0){
min=mat[i][j];
t[0]=i;
t[1]=j;
}
}
}
//affichage test
printf("La distance min est => %2.2f\n",min);
}
//Permet d'afficher la matrice
void afficheMat(float mat[5][5]){
int i,j;
for (i=0;i<5;i++){
for (j=0;j<5;j++){
printf("%2.2f ",mat[i][j]);
}
printf("\n");
}
}
//Remplit la matrice avec les nouvelles valeurs de distance calculées
void rempMat(float mat[5][5],int *t){
int i,j;
int a=t[0];
int b=t[1];
//calcul des distances
for(i=0;i<5;i++){
for(j=0;j<5;j++){
if((i==a || i==b)&&(j!=a && j!=b)){
mat[i][j]=(mat[a][j]+mat[b][j])/2;
}
if((j==a || j==b)&&(i!=a && i!=b)){
mat[i][j]=(mat[i][a]+mat[i][b])/2;
}
}
}
for(i=0;i<5;i++){
for(j=0;j<5;j++){
if(i==a || j==a){
mat[i][j]=0;
}
}
}
}
//fabrique les noeud ou feuilles plutôt !
Noeud * newNoeud (char *nom){
Noeud * a = (Noeud *) malloc(sizeof(Noeud));
a->gauche = (Noeud *) NULL;
a->droite = (Noeud *) NULL;
strcpy(a->valeur,nom);
return a;
}
int min2(int a,int b){
if(a>b)
return b;
else
return a;
}
int max2(int a,int b){
if(a<b)
return b;
else
return a;
}
//crée la liste des noeuds
Noeud * listNoeud(Noeud * a, Noeud * b) {
Noeud * c = (Noeud *) malloc(sizeof(Noeud));
char name[50]="";
strcat(name,a->valeur);
strcat(name,b->valeur);
strcpy(c->valeur,name);
c->gauche = a;
c->droite = b;
return c;
}
//affiche l'arbre
void afficheArbre(Noeud * a,int niv){
if (a->gauche != NULL)
afficheArbre(a->gauche,niv++);
if (a->droite != NULL)
afficheArbre(a->droite,niv++);
if (a->droite == NULL && a->gauche == NULL){
int i;
for(i=0;i<niv;i++)
printf("---");
printf("a: %s\n", a->valeur);
}
printf("|\n");
}
/*********************/
/* Fonction primaire */
/*********************/
int main(){
//table des especes
char nom[][10]= {"f1", "f2", "f3", "f4", "f5"};
//matrice de distance
float matrice[5][5]={{0,5,3,1,12},
{5,0,7,5,5},
{3,7,0,5,8},
{1,5,5,0,12},
{12,5,8,12,0}};
//affiche la matrice
afficheMat(matrice);
int i,k;
Noeud * feuille[5];
for(i=0;i<5;i++){
//cree les feuilles
char name[5];
strcpy(name,nom[i]);
feuille[i] = newNoeud(name);
printf("test=%s\n",feuille[i]->valeur);
}
for(k=0;k<4;k++){
//repère le minimum
int t[2]={0,0};
mini(matrice,t);
int a,b;
a=min2(t[0],t[1]);
b=max2(t[0],t[1]);
printf("i=%d et j=%d\n",a,b);
//regroupe les noeuds
feuille[a]=listNoeud(feuille[a], feuille[b]);
for (a=b+1; a<k; a++) {
feuille[a-1] = feuille[a];
}
//remplace la matrice
rempMat(matrice,t);
}
printf("\n------------------------------\n\n");
//afficher_arbre(feuille[0],0,5);
afficheArbre(feuille[0],0);
return 0;
} |
Partager