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
UNE JOLIE-HORLOGE ET PAS QU'UN PEU !UNE JOLIE-HORLOGE ET PAS QU'UN PEU ! par neodante
Pour les possesseurs d'iPhone, ça y est Bijin Tokei - qui se traduit littéralement en Français par " Jolie Horloge " - est arrivé et GRATUITEMENT s'il vous plaît ! Après la version Tokyo, Hokkaido, night club, racing, Gal, "pour les mademoiselles'", . voi...
Cliquez pour lire la suite de l'article par neodante TECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICESTECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICES par ROMELARD Fabrice
Animé par: Gaetan Bouveret et Julien Chomarat Business Connectivity Services (BCS) est dans SharePoint 2010 la version 2 de Business Data Catalog (BDC dans SharePoint 2007). Il s'agit de la solution permettant de visualiser des données provenan...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice [DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE[DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE par orion
Comme de nombreux geek, je suis un grand amateur de série TV et je rate régulièrement des épisodes de mes séries préférés. Une solution s'offre à vous avec ce merveilleux site : Tv Gorge - www.tvgorge.com Moteur de recherche à l'appui, vous pouvez ...
Cliquez pour lire la suite de l'article par orion TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Vincent Bellet et Baptiste Giraudier La BI dans SharePoint 2010, Les nouveaux services d'application dans SP2010 et SQL Server Reporting services 2008 R2. La BI dans SharePoint est généralisée pour tous afin de permettre à tous les coll...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Forum
RE : WIN APIRE : WIN API par racpp
Cliquez pour lire la suite par racpp WIN APIWIN API par omarino_007
Cliquez pour lire la suite par omarino_007
Logiciels
DB-MAIN (9.1.0)DB-MAIN (9.1.0)DB-MAIN is a data-modeling and data-architecture tool. It is designed to help developers and anal... Cliquez pour télécharger DB-MAIN Xilisoft DPG Convertisseur (5.1.37.0120)XILISOFT DPG CONVERTISSEUR (5.1.37.0120)Xilisoft DPG Convertisseur offre aux fans de Nintendo DS une bonne solution leur permettant de dé... Cliquez pour télécharger Xilisoft DPG Convertisseur GraphicsGale (2.01.01)GRAPHICSGALE (2.01.01)GraphicsGale est un logiciel de PixelArt avec de nombreuse fonctionnalités permettant de réalisé ... Cliquez pour télécharger GraphicsGale Architecte 3D (Platinum 2010)ARCHITECTE 3D (PLATINUM 2010)Architecte 3D Platinium vous permet de concevoir facilement les plans votre future maison, de l'é... Cliquez pour télécharger Architecte 3D TeamViewer 5 (TeamViewer 5)TEAMVIEWER 5 (TEAMVIEWER 5)Dépanner un ami,expliquer une manipulation devient un jeu d'enfant.
Prise en main d'un autre ord... Cliquez pour télécharger TeamViewer 5
Comparez les prix

HTC Hero
Entre 550€ et 550€
|