- /*************************************Arbre Binaire**********************************************/
-
- #include<stdio.h>
- #include<conio.h>
- #include<malloc.h>
-
- struct noeud
- { int val ;
- noeud *fg ;
- noeud *fd ;
- } ;
- typedef noeud* arbre ;
- //***********************************************************
- arbre inserer ( arbre T , int e )
- { noeud *P ;
- if ( T == NULL )
- { P = (noeud*) malloc (sizeof(noeud)) ;
- P->val = e ;
- P->fg = NULL ;
- P->fd = NULL ;
- T = P ;
- }
- else
- { if (T->val < e)
- T->fd = inserer (T->fd , e) ;
- else
- T->fg = inserer (T->fg , e) ;
- }
- return(T) ;
- }
- //************************************************************
- void affiche ( arbre T )
- { if (T!= NULL)
- { affiche(T->fg) ;
- printf(" %d",T->val) ;
- affiche(T->fd) ;
- }
- }
- //*************************************************************
- arbre creation()
- {
- arbre A=NULL;int Num;
- printf(" Donner les numeros <Taper 0 pour terminer> : \n" ) ;
- printf(" * Le 1ere numero: " ) ;
- scanf("%d",&Num) ;
- while ( Num != 0 )
- { A = inserer(A,Num) ;
- printf(" * element suivant : " ) ;
- scanf("%d",&Num) ;
-
- }
- return A;
- }
- //*************************************************************
- int somme(arbre A)
- {int s=0;
- if(A!=0)
- {
- s=A->val+somme(A->fd)+somme(A->fg);//appel recursive
- }
- return s;
- }
-
-
- void liberer(arbre T)
- { noeud *G , *D ;
- if (T!=NULL)
- { G = T->fg ;
- D = T->fd ;
- free (T) ;
- liberer(G) ;
- liberer(D) ;
- }
- }
-
- //*****************************************************************
- void main()
- { arbre A=NULL ;
- int Num , s,choix ;
- A=creation();
- do
- { printf("\n\n ****************************************************************** " ) ;
- printf("\n 0 = Quitter " ) ;
- printf("\n 1 = Ajouter " ) ;
- printf("\n 2 = Afficher " ) ;
- printf("\n 3 = Somme des Noeuds ");
-
- printf("\n Donner votre choix : " ) ;
- scanf("%d",&choix) ;
- switch (choix)
- {
- case 0 :
- printf("\n \t***...END...*** " ) ;
- break ;
-
- case 1 :
- printf("\n Donner l'entier à ajouter : " ) ;
- scanf("%d",&Num) ;
-
- A = inserer(A,Num) ;
- printf(" L'entier a été ajouter. " ) ;
-
- break ;
-
- case 2 :
- printf("\n L'arbre est : " ) ;
- affiche(A) ;
- break ;
- case 3:
- {s=somme(A);printf("somme =%d",s);
- break;
-
- }
-
- default :
- printf("\n Le choix est incorrecte. " ) ;
- break ;
- }
- }
- while ( choix != 0 ) ;
-
- liberer(A) ;
- getch();
- }
/*************************************Arbre Binaire**********************************************/
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct noeud
{ int val ;
noeud *fg ;
noeud *fd ;
} ;
typedef noeud* arbre ;
//***********************************************************
arbre inserer ( arbre T , int e )
{ noeud *P ;
if ( T == NULL )
{ P = (noeud*) malloc (sizeof(noeud)) ;
P->val = e ;
P->fg = NULL ;
P->fd = NULL ;
T = P ;
}
else
{ if (T->val < e)
T->fd = inserer (T->fd , e) ;
else
T->fg = inserer (T->fg , e) ;
}
return(T) ;
}
//************************************************************
void affiche ( arbre T )
{ if (T!= NULL)
{ affiche(T->fg) ;
printf(" %d",T->val) ;
affiche(T->fd) ;
}
}
//*************************************************************
arbre creation()
{
arbre A=NULL;int Num;
printf(" Donner les numeros <Taper 0 pour terminer> : \n" ) ;
printf(" * Le 1ere numero: " ) ;
scanf("%d",&Num) ;
while ( Num != 0 )
{ A = inserer(A,Num) ;
printf(" * element suivant : " ) ;
scanf("%d",&Num) ;
}
return A;
}
//*************************************************************
int somme(arbre A)
{int s=0;
if(A!=0)
{
s=A->val+somme(A->fd)+somme(A->fg);//appel recursive
}
return s;
}
void liberer(arbre T)
{ noeud *G , *D ;
if (T!=NULL)
{ G = T->fg ;
D = T->fd ;
free (T) ;
liberer(G) ;
liberer(D) ;
}
}
//*****************************************************************
void main()
{ arbre A=NULL ;
int Num , s,choix ;
A=creation();
do
{ printf("\n\n ****************************************************************** " ) ;
printf("\n 0 = Quitter " ) ;
printf("\n 1 = Ajouter " ) ;
printf("\n 2 = Afficher " ) ;
printf("\n 3 = Somme des Noeuds ");
printf("\n Donner votre choix : " ) ;
scanf("%d",&choix) ;
switch (choix)
{
case 0 :
printf("\n \t***...END...*** " ) ;
break ;
case 1 :
printf("\n Donner l'entier à ajouter : " ) ;
scanf("%d",&Num) ;
A = inserer(A,Num) ;
printf(" L'entier a été ajouter. " ) ;
break ;
case 2 :
printf("\n L'arbre est : " ) ;
affiche(A) ;
break ;
case 3:
{s=somme(A);printf("somme =%d",s);
break;
}
default :
printf("\n Le choix est incorrecte. " ) ;
break ;
}
}
while ( choix != 0 ) ;
liberer(A) ;
getch();
}