begin process at 2013 05 24 23:33:40
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Astuces

 > ARBRE BINAIRE ORDONÉE HORIZONTALEMENT

ARBRE BINAIRE ORDONÉE HORIZONTALEMENT


 Information sur la source

Note :
Aucune note
Catégorie :Astuces Classé sous :arbre, binaire, ordonée, horizontalement, dadamagouil Niveau :Expert Date de création :07/02/2007 Date de mise à jour :07/02/2007 21:26:48 Vu :7 252

Auteur : dadamagouil

Ecrire un message privé
Site perso
Ce membre participe au partage de revenus publicitaires
Commentaire sur cette source (3)
Ajouter un commentaire et/ou une note


 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

BUFFER CIRCULAIRE

 Sources de la même categorie

Source avec Zip CROSSREF MULTI FICHIERS par ccgousset
Source avec Zip Source avec une capture EVAL EXPRESSION COMPLEXE EN 15 LIGNES DE CODE par yann_lo_san
Source avec Zip SCHEDULER RR FIFO par yvesB87
Source avec Zip ALGORITHMES RÉCURSIFS VS ALGORITHMES ITÉRATIFS par yvesB87
Source avec Zip Source avec une capture C++ FORMAT D'IMAGE AVEC QT par pop70

 Sources en rapport avec celle ci

EVALUATION D'UNE CHAÎNE DE CARACTÈRES AVEC UN ARBRE BINAIRE par pabbati
Source avec Zip DICTIONNAIRE DANS UN ARBRE BINAIRE par pabbati
Source avec Zip PARCOURS D'UN ARBRE par krissssss
Source avec Zip Source avec une capture ARBRE BINAIRE DE RECHERCHE par laderivier
ARBRE BINAIRE (NON-EQUILIBRE) par JCDjcd

Commentaires et avis

Commentaire de capouto le 11/02/2008 16:42:25

Je viens de tester ton programme mais il ne marche pas. j'ai essayé avec Dev cpp et TC mais les pointeurs(->) ne sont pas reconnue. Donc il faut revoir.

Commentaire de dadamagouil le 11/03/2009 16:38:55

C'est du c...
Je l'aie développé avec Visual Studio de MS

Commentaire de TPB le 22/05/2011 02:28:26

Salut à tous !
@CAPOUTO : c'est un problème de portabilité.

 Ajouter un commentaire


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 &#224; savoir ce que repr&#233;sente exatement tout ce qui concerne les arbres binaire, par exemple la diff&#233;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&#233;cursivit&#233; ni les pointeurs pour les sous-arbres et je


Nos sponsors


Sondage...

CalendriCode

Mai 2013
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
2728293031  

Consulter la suite du CalendriCode

Photothèque

A découvrir



 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 2,449 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales