begin process at 2008 07 05 21:24:48
1 205 358 membres
326 nouveaux aujourd'hui
14 119 membres club

Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

ARBRE BINAIRE ORDONÉE HORIZONTALEMENT


Information sur la source

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 : 3 724

Note :
Aucune note

Commentaire sur cette source (1)
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
07 février 2007 21:26:48 :
Faute de frappe dans le titre j'ai écrit BONAIRE ou lieu de BINAIRE
  • signaler à un administrateur
    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.

Ajouter un commentaire

Discussions en rapport avec ce code source

Pub



Appels d'offres

Plugin Dialer outlook
Budget : 2 000€
Travail graphique- ill...
Budget : 1 000€
creation de marque et ...
Budget : 1 000€

CalendriCode

Juillet 2008
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Téléchargements

Logiciels à télécharger sur le même thème :

Boutique

Boutique de goodies CodeS-SourceS