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
TECHDAYS PARIS 2012 : NOUVELLES TENDANCES DU POSTE DE TRAVAIL - BRING YOUR OWN PCTECHDAYS PARIS 2012 : NOUVELLES TENDANCES DU POSTE DE TRAVAIL - BRING YOUR OWN PC par ROMELARD Fabrice
Speakers: Thierry Rapatout, Antoine Petit et Xavier Trebbia Cette session entre dans le cadre des RDV Décideurs des TechDays 2012, elle est liée à la consumérisation de l'IT et la mise en place du "DeskTop as a Service" dans de plus en ...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : SYSTEM CENTER SERVICE MANAGER 2012 VUE D'ENSEMBLETECHDAYS PARIS 2012 : SYSTEM CENTER SERVICE MANAGER 2012 VUE D'ENSEMBLE par ROMELARD Fabrice
Speakers: Julien Marechal, Gautier Confiant, Sébastien MEYER La session débute par le positionnement de la solution System Center par rapport aux concepts d'organisation ITIL. Le portail du catalogue de se...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : PLEINIèRE SECOND JOURTECHDAYS PARIS 2012 : PLEINIèRE SECOND JOUR par ROMELARD Fabrice
Après une première journée dédiée aux développeurs, cette seconde journée est dédiée au monde des entreprises et de ses applications. Ainsi, cette pleinière est dédiée à faire un 360 de l'évolution des applications Business aux demandes ac...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : RETOUR D'EXPéRIENCE SUR LA MISE EN PLACE D'UN CLOUD PRIVéTECHDAYS PARIS 2012 : RETOUR D'EXPéRIENCE SUR LA MISE EN PLACE D'UN CLOUD PRIVé par ROMELARD Fabrice
Speaker : Guillaume Rochette Cette session est dédiée à fournir le retour sur la mise en place d'un cloud privé (IaaS) par Osiatis pour son compte ou celui de ses clients. Ce projet s'est déroulé sur 4 mois et a permis de faire évoluer...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : COMMENT SHAREPOINT A SAUVé MES TECHDAYSTECHDAYS PARIS 2012 : COMMENT SHAREPOINT A SAUVé MES TECHDAYS par ROMELARD Fabrice
Speakers : Lionel Limozin et Alain Marty La session commence par une découverte de SharePoint à travers la mise en place d'un environnement SharePoint pour la gestion des Sessions animées par BeWise. Le besoin est très ba...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|