- #include <stdio.h>
- #include <malloc.h>
- #include <conio.h>
- #include <string.h>
-
- #define OK 0
- #define ERREUR_MEMOIRE 1
- #define ERREUR_ELEMENT_EXISTE 2
-
- struct Arbre
- {
- int n ; // contient le nombre
- struct Arbre * PG; // Pointeur sur l'élément de droite ou NULL s'il n'existe pas
- struct Arbre * PD; // Pointeur sur l'élément de gauche ou NULL s'il n'existe pas
- };
-
- int Ajouter( int n, struct Arbre **PPSommet );
- int Cherche( int n, struct Arbre *PSommet );
- void Parcourir( struct Arbre *PSommet );
- void Parcourir2( struct Arbre *PSommet );
- void Parcourir3( struct Arbre *PSommet );
-
-
- void main (void)
-
- {
- struct Arbre *PSommet = NULL;
- int n;
- char choix; // Variable qui contiendra le choix de l'opération à effectuer
-
- do
- {
- printf("\t\t\t Arbre binaire de recherche\n\n\n");
- printf("\t 1. Ajouter 1 nombre\n");
- printf("\t 2. Parcourir l'arbre du plus petit au plus grand\n");
- printf("\t 3. Parcourir l'arbre du plus grand au plus petit\n");
- printf("\t 4. Parcourir l'arbre de gauche -> droite\n");
- printf("\t 5. Chercher un nombre\n");
- printf("\t 6. Quitter\n\n\n");
- printf(" Faire votre choix svpl ? ");
-
-
- fflush(stdin);
- scanf("%c",&choix);
- switch(choix)
- {
- case '1' :
- // Ajoute un élément dans l'arbre
- printf("Entrer un nombre : ");
- fflush(stdin);
- scanf("%d", &n);
- if( Ajouter( n , &PSommet) != OK)
- printf("Le nombre n'a pas pu etre ajouté!\n");
-
- break;
-
- case '2' :
- // Parcourir tout l'arbre du plus petit au plus grand
- Parcourir( PSommet );
- break;
-
- case '3' :
- // Parcourir tout l'arbre du plus grand au plus petit
- Parcourir2( PSommet );
- break;
-
- case '4' :
- // Parcourir tout l'arbre de gauche à droite
- Parcourir3( PSommet );
- break;
-
- case '5' :
- // Cherche un élément dans l'arbre
-
- printf("Entrer un nombre : ");
- fflush(stdin);
- scanf("%d", &n);
- if( Cherche( n, PSommet) == NULL)
- printf("\nLe nombre n'a pas été trouvé!\n");
- else
- printf("\nNombre trouvé!\n");
- break;
-
- case '6' : printf("Au revoir");break;
-
- default : printf("Vérifier votre choix svpl ! \n");
- }
- }
- while( choix != '6' ) ;
- }
- int Ajouter( int n, struct Arbre **PPSommet )
- {
- struct Arbre *P, *P2;
- int Retour = OK;
- P = ( struct Arbre *)malloc (sizeof(struct Arbre));
- if (P != NULL)
- {
- P->n = n;
- P->PD = NULL;
- P->PG = NULL;
- if(*PPSommet==NULL)
- {
- *PPSommet=P;
- }
- else
- {
- P2=*PPSommet;
- do
- {
- if(P2->n == P->n)
- {
- Retour = ERREUR_ELEMENT_EXISTE;
- P2=NULL;
- free(P);
- }
- else
- {
- if (P->n > P2->n)
- {
- if(P2->PD == NULL)
- {
- P2->PD = P;
- P2=NULL;
- }
- else
- {P2 = P2->PD;}
- }
- else
- {
- if(P2->PG==NULL)
- {
- P2->PG = P;
- P2=NULL;
- }
- else
- {P2 = P2->PG;}
- }
- }
- }
- while(P2!=NULL);
- }
- }
- else{Retour = ERREUR_MEMOIRE;}
- return (Retour);
- }
- int Cherche( int n, struct Arbre *PSommet )
- {
- struct Arbre *P;
- int Retour = NULL;
- P = PSommet;
- while (P != NULL)
- {
- if (P->n == n)
- {
- Retour =! NULL;
- P=NULL;
- }
- else
- {
- if (P->n > n){P=P->PG;}
- else {P=P->PD;}
- }
- }
- return (Retour);
- }
- void Parcourir( struct Arbre *PSommet )
- {
- if (PSommet!=NULL)
- {
- Parcourir(PSommet->PG);
- printf("%d\n", PSommet->n);
- Parcourir(PSommet->PD);
- }
- }
- void Parcourir2( struct Arbre *PSommet )
- {
- if (PSommet!=NULL)
- {
- Parcourir2(PSommet->PD);
- printf("%d\n", PSommet->n);
- Parcourir2(PSommet->PG);
- }
- }
- void Parcourir3( struct Arbre *PSommet )
- {
- if (PSommet!=NULL)
- {
- printf("%d\n", PSommet->n);
- Parcourir3(PSommet->PG);
- Parcourir3(PSommet->PD);
- }
- }
#include <stdio.h>
#include <malloc.h>
#include <conio.h>
#include <string.h>
#define OK 0
#define ERREUR_MEMOIRE 1
#define ERREUR_ELEMENT_EXISTE 2
struct Arbre
{
int n ; // contient le nombre
struct Arbre * PG; // Pointeur sur l'élément de droite ou NULL s'il n'existe pas
struct Arbre * PD; // Pointeur sur l'élément de gauche ou NULL s'il n'existe pas
};
int Ajouter( int n, struct Arbre **PPSommet );
int Cherche( int n, struct Arbre *PSommet );
void Parcourir( struct Arbre *PSommet );
void Parcourir2( struct Arbre *PSommet );
void Parcourir3( struct Arbre *PSommet );
void main (void)
{
struct Arbre *PSommet = NULL;
int n;
char choix; // Variable qui contiendra le choix de l'opération à effectuer
do
{
printf("\t\t\t Arbre binaire de recherche\n\n\n");
printf("\t 1. Ajouter 1 nombre\n");
printf("\t 2. Parcourir l'arbre du plus petit au plus grand\n");
printf("\t 3. Parcourir l'arbre du plus grand au plus petit\n");
printf("\t 4. Parcourir l'arbre de gauche -> droite\n");
printf("\t 5. Chercher un nombre\n");
printf("\t 6. Quitter\n\n\n");
printf(" Faire votre choix svpl ? ");
fflush(stdin);
scanf("%c",&choix);
switch(choix)
{
case '1' :
// Ajoute un élément dans l'arbre
printf("Entrer un nombre : ");
fflush(stdin);
scanf("%d", &n);
if( Ajouter( n , &PSommet) != OK)
printf("Le nombre n'a pas pu etre ajouté!\n");
break;
case '2' :
// Parcourir tout l'arbre du plus petit au plus grand
Parcourir( PSommet );
break;
case '3' :
// Parcourir tout l'arbre du plus grand au plus petit
Parcourir2( PSommet );
break;
case '4' :
// Parcourir tout l'arbre de gauche à droite
Parcourir3( PSommet );
break;
case '5' :
// Cherche un élément dans l'arbre
printf("Entrer un nombre : ");
fflush(stdin);
scanf("%d", &n);
if( Cherche( n, PSommet) == NULL)
printf("\nLe nombre n'a pas été trouvé!\n");
else
printf("\nNombre trouvé!\n");
break;
case '6' : printf("Au revoir");break;
default : printf("Vérifier votre choix svpl ! \n");
}
}
while( choix != '6' ) ;
}
int Ajouter( int n, struct Arbre **PPSommet )
{
struct Arbre *P, *P2;
int Retour = OK;
P = ( struct Arbre *)malloc (sizeof(struct Arbre));
if (P != NULL)
{
P->n = n;
P->PD = NULL;
P->PG = NULL;
if(*PPSommet==NULL)
{
*PPSommet=P;
}
else
{
P2=*PPSommet;
do
{
if(P2->n == P->n)
{
Retour = ERREUR_ELEMENT_EXISTE;
P2=NULL;
free(P);
}
else
{
if (P->n > P2->n)
{
if(P2->PD == NULL)
{
P2->PD = P;
P2=NULL;
}
else
{P2 = P2->PD;}
}
else
{
if(P2->PG==NULL)
{
P2->PG = P;
P2=NULL;
}
else
{P2 = P2->PG;}
}
}
}
while(P2!=NULL);
}
}
else{Retour = ERREUR_MEMOIRE;}
return (Retour);
}
int Cherche( int n, struct Arbre *PSommet )
{
struct Arbre *P;
int Retour = NULL;
P = PSommet;
while (P != NULL)
{
if (P->n == n)
{
Retour =! NULL;
P=NULL;
}
else
{
if (P->n > n){P=P->PG;}
else {P=P->PD;}
}
}
return (Retour);
}
void Parcourir( struct Arbre *PSommet )
{
if (PSommet!=NULL)
{
Parcourir(PSommet->PG);
printf("%d\n", PSommet->n);
Parcourir(PSommet->PD);
}
}
void Parcourir2( struct Arbre *PSommet )
{
if (PSommet!=NULL)
{
Parcourir2(PSommet->PD);
printf("%d\n", PSommet->n);
Parcourir2(PSommet->PG);
}
}
void Parcourir3( struct Arbre *PSommet )
{
if (PSommet!=NULL)
{
printf("%d\n", PSommet->n);
Parcourir3(PSommet->PG);
Parcourir3(PSommet->PD);
}
}