Accueil > > > ARBRE BINAIRE ORDONÉE HORIZONTALEMENT
ARBRE BINAIRE ORDONÉE HORIZONTALEMENT
Information sur la source
Description
Voici une source en C qui vous permet de : Ajouter 1 nombre Parcourir l'arbre du plus petit au plus grand Parcourir l'arbre du plus grand au plus petit Parcourir l'arbre de gauche -> droite Chercher un nombre
Source
- #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);
}
}
Conclusion
Si il y a des questions ou des bugs, il ne faut pas hésiter à me demander
Historique
- 07 février 2007 21:26:48 :
- Faute de frappe dans le titre j'ai écrit BONAIRE ou lieu de BINAIRE
Sources du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
Arbre Binaire [ par Fury_Vash ]
je souhaite savoir si il y a pas de code pour chercher le niveau d'un element dans un arbre j'ai tout essayer mais je ne suis pas parvenu a trouver
Arbre Binaire Equilibré [ par messier79 ]
BonjourJe voudrais savoir comment implémenter un arbre Binaire de Recherche (ou un Arbre Equilibré) en utilisant la STL.Si possible avec un exemple...
expression mathematique sous forme d'arbre binaire [ par Milhouse57 ]
Je recherche un code qui transformerait une expression mathematique (donnée par l'utilisateur sous forme de chaine de charactere) en un arbre binaire
arbre binaire [ par moltese ]
Salut, je cherche à savoir si il est possible de créer un arbre binaire par itération? Et si oui est-il possible d'en avoir le code? Merci
arbre binaire de recherche equilibrée [ par mrihab ]
salut je suis une etudiante en informatique je veux savoir comment realiser une interface graphique representant cette arbre binaire equilibrée
arbre binaire [ par ghounaya ]
je cherche une simulation graphique des arbres binaires :recherche,ajout et suppression d'un élément.
arbre binaire [ par stephanelin ]
Bonsoir,comment créer un tableau qui effectue un tri décroissant (d'entiers), en utilisant la notion d'arbre binaire ?MerciStéphane
arbre binaire [ par pfmk ]
je voudrai enregistrer un arbre binaire dans un fichier texte ou binaire? j'arrive pas à trouver comment je vais organiser mon fichier pour pouvoir r
Arbre binaire profondeur hauteur [ par ecolopolo ]
BonjourJe chercher à savoir ce que représente exatement tout ce qui concerne les arbres binaire, par exemple la différence entre la hau
arbre binaire itératif [ par fred100582 ]
Salut, je travaille en ce moment sur un arbre binaire mais je ne dois utiliser ni la récursivité ni les pointeurs pour les sous-arbres et je
|
Derniers Blogs
ETENDRE LE TEAM WEB ACCESS DE TFS 2012 - STEP 0ETENDRE LE TEAM WEB ACCESS DE TFS 2012 - STEP 0 par Philess
L'extensibilité du Team Web Access
Le Web Access (site d'équipe) de Team Foundation Server a été complètement réécrit dans la version 2012 avec pas moins de 400.000 lignes de JavaScript. Ce nouveau modèle a été pensé pour offrir de grandes...
Cliquez pour lire la suite de l'article par Philess SIMULER FACILEMENT L'ENVOI DE MAILSIMULER FACILEMENT L'ENVOI DE MAIL par JeremyJeanson
il m'a été demandé, à plusieurs reprises, comment je faisais pour simuler l'envoi de mail lors de mes démos de Workflow Foundation. Ma solution est plutôt simple : j'utilise la configuration par défaut du SmtpClient et j'oriente les mails vers un dossier ...
Cliquez pour lire la suite de l'article par JeremyJeanson VOTEZ POUR LE TOP 10 DES INFLUENCEURS SHAREPOINT FRANCOPHONES !VOTEZ POUR LE TOP 10 DES INFLUENCEURS SHAREPOINT FRANCOPHONES ! par Patrick Guimonet
Si ce n'est déjà fait (comme plus de 600 personnes déjà), il est encore temps de voter pour le concours TOP 10 des influenceurs SharePoint francophones ! Il est organisé par harmon.ie et accessible ici : http://harmon.ie/top-...
Cliquez pour lire la suite de l'article par Patrick Guimonet [CONF'SHAREPOINT] DERNIER RAPPEL ! :-)[CONF'SHAREPOINT] DERNIER RAPPEL ! :-) par Patrick Guimonet
La Conf'SharePoint en chiffres c'est : 3 jours de SharePoint ! 4 parcours et 60 sessions 17 partenaires représentant toutes les fac...
Cliquez pour lire la suite de l'article par Patrick Guimonet
Forum
PB PACMAN C++PB PACMAN C++ par garfield95
Cliquez pour lire la suite par garfield95
Logiciels
Easy-Planning (4.5.0.11)EASY-PLANNING (4.5.0.11)Easy-Planning permet de créer des plannings sous la représentation de diagrammes et est adapté a... Cliquez pour télécharger Easy-Planning CVEasy (3.1.0.51)CVEASY (3.1.0.51)PHMSD-CVEasy est un logiciel d'aide à la rédaction de CV d'une simplicité déconcertante.
PHMSD-C... Cliquez pour télécharger CVEasy LettresFaciles 2011 (8.6.0.31)LETTRESFACILES 2011 (8.6.0.31)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011 sDEVIS-FACTURES vlPRO (8.4.2.62)SDEVIS-FACTURES VLPRO (8.4.2.62)sDEVIS-FACTURES vlPRO a été mis au point pour les particuliers, créateurs, entrepreneurs, artisa... Cliquez pour télécharger sDEVIS-FACTURES vlPRO Devis-Factures PHMSD (2.1.0.11)DEVIS-FACTURES PHMSD (2.1.0.11)Configuration minimale
Nécessite Windows™ 2000, XP, Windows 7, 8, Vista (Service Pack à... Cliquez pour télécharger Devis-Factures PHMSD
|