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
| #include<stdio.h>
#include <stdlib.h>
#include <conio.h>
/*type liste*/
typedef struct noeud * ab;
struct noeud
{
int val;
ab g;
ab d;
};
/*creation dun neoud*/
ab creat(int val,ab ls, ab rs)
{
ab e = new(noeud);
e->val = val;
e->g = ls;
e->d = rs;
return e;
}
/*ajout dun element dans un ab*/
void insert(int val,ab r)
{
if( r == NULL )
{
r=creat(val,NULL,NULL);
}
else if(r->g==NULL)
{
insert(val,r->g);
}
else if(r->d==NULL)
{
insert(val,r->d);
}
}
/*affichage dun ab*/
void affichage(ab r)
{if (r!=NULL){
printf("%d",r->val);
affichage(r->g);
affichage(r->d);}
}
/*programme principal*/
main ()
{int choix,cho,x;
noeud* a=NULL;
do {
/*faire le choix*/
printf("\n");
printf("1)- creation d'un arbre binaire.");
printf("\n");
printf("2)- affichage d'un arbre binaire.");
printf("\n");
printf("\n");
printf("ENTRER VOTRE CHOIX !!: ");
scanf("%d",&choix);
printf("\n");
switch(choix)
{
case 1: {
cho=1;
while(cho!=0){
printf("Entrer la valeur pour l'inserer dans l'arbre:\n");
scanf("%d",&x);
insert(x,a);
printf("Voulez vous ajoutez un element? (1 / 0)\n");
scanf("%d",&cho);
}
printf("\n");
break;
}
case 2: {
printf("**AFFICHAGE DE LA LISTE**\n");
affichage(a);
break;
}
}
}
while (choix!=0);
getch();
} |
Partager