Accueil > > > ARBRE BINAIRE
ARBRE BINAIRE
Information sur la source
Description
c'est un programme qui montre les différentes fonctions de manipulation d'arbres binaires .
parcours,ajout...
Source
- #include "stdio.h"
- #include "conio.h"
- #include "stdlib.h"
- struct cellule
- {
- int val ;
- struct cellule *gch ;
- struct cellule *drt;
- };
-
- typedef struct cellule *arbre ; //arbre defenie comme type pointeur de cellule
- void prefixer(arbre);
- arbre creefeuille(int);
- void infixer(arbre);
- void postfixer(arbre);
- int hauteur (arbre);
- int taille(arbre);
- void recherche(int ,arbre );
- arbre ajout(int ,arbre);
- void attacher_new(arbre ,arbre );
- arbre ajout_new(int ,arbre );
- arbre cons(int x, arbre G, arbre D )
- {
- arbre A = ( arbre ) malloc( sizeof( cellule )) ;
- A -> val = x ;
- A -> gch = G ;
- A -> drt = D ;
- return A ;
- }
-
- arbre cree_racine(int x)
- {
-
- arbre A = ( arbre ) malloc( sizeof( cellule )) ;
- A->val=x;
- A->gch=NULL;
- A->drt=NULL;
- return A;
- }
-
-
- void afficher_arbre (arbre A)
- {
- if(A!=NULL)
- {
- afficher_arbre(A->drt);
- printf("%d\t",A->val);
- afficher_arbre(A->gch);
-
- }
- }
-
-
- void main()
- {
- do
- {
- int x,choix,s,s1,el;
- arbre fd1;
- arbre r;
- arbre fg1,fg2,r1,r2,fd2;
- printf("si vous voulez :");
- printf("\n\n\ncreer un arbre 1.\n\nafficher l'arbre 2.\n\nexploration prefixer 3.\n\nexploration infixer 4.");
- printf("\n\nexploration postfixer 5.\n\ndeterminer la hauteur de l'arbre 6.\n\ndeterminer la taille de l'arbre 7.\n\nrechercher un element dans l'arbre 8.\n\najouter un element à l'arbre 9.\n\nnb d'occurence d'un element donnée 10.\n\n");
- gotoxy(29,25); printf("tapper votre choix ici :");scanf("%d",&choix);
- clrscr();
-
- switch (choix)
- {
- case 1:
- // printf("entrer un elt ");
- //scanf("%d",&x);
-
- fd1=creefeuille( 4);
- fg1=creefeuille(1) ;
- r1=cons(3,fg1,fd1);
- fd2=creefeuille( 8);
- //fg2=creefeuille(6) ;
- r2=cons(7,NULL,fd2);
- r=cons(5,r1,r2);
- printf("l'arbre est construit avec succé ");break ;
- case 2: afficher_arbre ( r);break;
- case 3:prefixer(r);printf("\nc'est le parcour prefixer");break;
- case 4:infixer(r);printf("\nc'est le parcour infixer");break;
- case 5:postfixer(r);printf("\nc'est le parcour postfixer");break;
- case 6:s=hauteur(r);
- printf("\nla hauteur de l'arbre est :%d ",s); break;
- case 7:s1=taille(r);
- printf("la taille de l'arbre est :%d",s1); break;
- case 8:printf("entrer l'elt rechercher: ");
- scanf("%d",&el);
- recherche(el,r);break;
- case 9:printf("entrer l'elt à ajouter: ");
- scanf("%d",&el);
- r= ajout_new(el,r);
- printf("\nc'est bien l'arbre se construit dynamiquement");break;//aj=ajout(el,r);break;
- case 10: printf("la racine :");
- scanf("%d",&x);
- r=cree_racine( x);break;
- default:printf("taper un nbr entre en 1 et 10");break;
-
- }
- getch();
- clrscr();
- }
-
- while(1);
- }
- arbre creefeuille(int x)
- {
- arbre f;
- f=(arbre)malloc(sizeof(cellule));
- f->val=x;
- f->drt=NULL;
- f->gch=NULL;
- return f;
- }
- //les trois parcours :prefixer , infixer , postfixer
- void prefixer(arbre a)
- {
- if(a!=NULL)
- {
- printf("%d\t",a->val);
- prefixer(a->gch);
- prefixer(a->drt);
- }
- }
- void infixer(arbre a)
- {
- if(a!=NULL)
- {
- infixer(a->gch);
- printf("%d\t",a->val);
- infixer(a->drt);
- }
- }
- void postfixer(arbre a)
- {
- if(a!=NULL)
- {
- postfixer(a->gch);
- if(a->drt==NULL)
- printf("%d\t",a->val);
- else{
- postfixer(a->drt);
- printf("%d\t",a->val);}
- }
- }
- int hauteur (arbre ar)
- {
- int h1,h2;
- if(ar==NULL)
- return(-1);
- else
- h1=hauteur(ar->drt);
- h2=hauteur(ar->gch);
- if(h1>h2)
- return h1+1;
- else
- return h2+1;
- }
- int taille(arbre ar)
- {
- int t1,t2;
- if(ar==NULL)
- return 0;
- t1=taille(ar->gch);
- t2=taille(ar->drt);
- return(t1+t2+1);
- }
- //la recherche d'un elt ds l'arbre
- void recherche(int x,arbre ar)
- {
- if(ar==NULL)
- printf("l'elt rechercher n'existe pas dans l'arbre ");
- else
- if(ar->val==x)
- printf("l'elt existe dans l'arbre");
- else
- if(ar->val>x)
- {
- recherche(x,ar->gch);
- if(ar->val==x)
- printf("l'elt existe dans l'arbre");}
- else
- {
- recherche(x,ar->drt);
- if(ar->val==x)
- printf("l'elt existe dans l'arbre");}
- }
-
- arbre ajout_new(int el,arbre s_pre)
- {
- arbre s;
- s=(arbre)malloc(sizeof(cellule));
- s->val=el;
- s->gch=NULL;
- s->drt=NULL;
- if(s_pre==NULL)
- {
- s_pre=s; }
- else
- attacher_new( s_pre, s);
- return s_pre;
- }
- //definition de la fonction qui attache le nouveau arbre(construit dans la fct ajout_new
- void attacher_new(arbre s_pre,arbre s)
- {
- int a=0;
- while(a==0)
- {
- if(s_pre->val<s->val)
- {
- if(s_pre->drt!=NULL)
- s_pre=s_pre->drt;
- else
- {
- s_pre->drt=s;
- a=1;}
- }
- else
- {
- if(s_pre->gch!=NULL)
- s_pre=s_pre->gch;
- else
- {
- s_pre->gch=s;
- a=1;
- }
- }
- }
-
- }
-
-
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
struct cellule
{
int val ;
struct cellule *gch ;
struct cellule *drt;
};
typedef struct cellule *arbre ; //arbre defenie comme type pointeur de cellule
void prefixer(arbre);
arbre creefeuille(int);
void infixer(arbre);
void postfixer(arbre);
int hauteur (arbre);
int taille(arbre);
void recherche(int ,arbre );
arbre ajout(int ,arbre);
void attacher_new(arbre ,arbre );
arbre ajout_new(int ,arbre );
arbre cons(int x, arbre G, arbre D )
{
arbre A = ( arbre ) malloc( sizeof( cellule )) ;
A -> val = x ;
A -> gch = G ;
A -> drt = D ;
return A ;
}
arbre cree_racine(int x)
{
arbre A = ( arbre ) malloc( sizeof( cellule )) ;
A->val=x;
A->gch=NULL;
A->drt=NULL;
return A;
}
void afficher_arbre (arbre A)
{
if(A!=NULL)
{
afficher_arbre(A->drt);
printf("%d\t",A->val);
afficher_arbre(A->gch);
}
}
void main()
{
do
{
int x,choix,s,s1,el;
arbre fd1;
arbre r;
arbre fg1,fg2,r1,r2,fd2;
printf("si vous voulez :");
printf("\n\n\ncreer un arbre 1.\n\nafficher l'arbre 2.\n\nexploration prefixer 3.\n\nexploration infixer 4.");
printf("\n\nexploration postfixer 5.\n\ndeterminer la hauteur de l'arbre 6.\n\ndeterminer la taille de l'arbre 7.\n\nrechercher un element dans l'arbre 8.\n\najouter un element à l'arbre 9.\n\nnb d'occurence d'un element donnée 10.\n\n");
gotoxy(29,25); printf("tapper votre choix ici :");scanf("%d",&choix);
clrscr();
switch (choix)
{
case 1:
// printf("entrer un elt ");
//scanf("%d",&x);
fd1=creefeuille( 4);
fg1=creefeuille(1) ;
r1=cons(3,fg1,fd1);
fd2=creefeuille( 8);
//fg2=creefeuille(6) ;
r2=cons(7,NULL,fd2);
r=cons(5,r1,r2);
printf("l'arbre est construit avec succé ");break ;
case 2: afficher_arbre ( r);break;
case 3:prefixer(r);printf("\nc'est le parcour prefixer");break;
case 4:infixer(r);printf("\nc'est le parcour infixer");break;
case 5:postfixer(r);printf("\nc'est le parcour postfixer");break;
case 6:s=hauteur(r);
printf("\nla hauteur de l'arbre est :%d ",s); break;
case 7:s1=taille(r);
printf("la taille de l'arbre est :%d",s1); break;
case 8:printf("entrer l'elt rechercher: ");
scanf("%d",&el);
recherche(el,r);break;
case 9:printf("entrer l'elt à ajouter: ");
scanf("%d",&el);
r= ajout_new(el,r);
printf("\nc'est bien l'arbre se construit dynamiquement");break;//aj=ajout(el,r);break;
case 10: printf("la racine :");
scanf("%d",&x);
r=cree_racine( x);break;
default:printf("taper un nbr entre en 1 et 10");break;
}
getch();
clrscr();
}
while(1);
}
arbre creefeuille(int x)
{
arbre f;
f=(arbre)malloc(sizeof(cellule));
f->val=x;
f->drt=NULL;
f->gch=NULL;
return f;
}
//les trois parcours :prefixer , infixer , postfixer
void prefixer(arbre a)
{
if(a!=NULL)
{
printf("%d\t",a->val);
prefixer(a->gch);
prefixer(a->drt);
}
}
void infixer(arbre a)
{
if(a!=NULL)
{
infixer(a->gch);
printf("%d\t",a->val);
infixer(a->drt);
}
}
void postfixer(arbre a)
{
if(a!=NULL)
{
postfixer(a->gch);
if(a->drt==NULL)
printf("%d\t",a->val);
else{
postfixer(a->drt);
printf("%d\t",a->val);}
}
}
int hauteur (arbre ar)
{
int h1,h2;
if(ar==NULL)
return(-1);
else
h1=hauteur(ar->drt);
h2=hauteur(ar->gch);
if(h1>h2)
return h1+1;
else
return h2+1;
}
int taille(arbre ar)
{
int t1,t2;
if(ar==NULL)
return 0;
t1=taille(ar->gch);
t2=taille(ar->drt);
return(t1+t2+1);
}
//la recherche d'un elt ds l'arbre
void recherche(int x,arbre ar)
{
if(ar==NULL)
printf("l'elt rechercher n'existe pas dans l'arbre ");
else
if(ar->val==x)
printf("l'elt existe dans l'arbre");
else
if(ar->val>x)
{
recherche(x,ar->gch);
if(ar->val==x)
printf("l'elt existe dans l'arbre");}
else
{
recherche(x,ar->drt);
if(ar->val==x)
printf("l'elt existe dans l'arbre");}
}
arbre ajout_new(int el,arbre s_pre)
{
arbre s;
s=(arbre)malloc(sizeof(cellule));
s->val=el;
s->gch=NULL;
s->drt=NULL;
if(s_pre==NULL)
{
s_pre=s; }
else
attacher_new( s_pre, s);
return s_pre;
}
//definition de la fonction qui attache le nouveau arbre(construit dans la fct ajout_new
void attacher_new(arbre s_pre,arbre s)
{
int a=0;
while(a==0)
{
if(s_pre->val<s->val)
{
if(s_pre->drt!=NULL)
s_pre=s_pre->drt;
else
{
s_pre->drt=s;
a=1;}
}
else
{
if(s_pre->gch!=NULL)
s_pre=s_pre->gch;
else
{
s_pre->gch=s;
a=1;
}
}
}
}
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
IMAGINE CUP 2012, MAKE A SIGN EN FINALEIMAGINE CUP 2012, MAKE A SIGN EN FINALE par junarnoalg
Voilà qui est fait, la nouvelle est officielle ! L'équipe belge "Make a Sign" va au pays des kangourous défendre son projet dans la catégorie Software Design. http://www.imaginecup.com/CompetitionsContent/Competition/WorldwideFinalists.aspx V...
Cliquez pour lire la suite de l'article par junarnoalg KINECT 1.5 IS OUT !KINECT 1.5 IS OUT ! par Vko
La version 1.5 du Kinect For Microsoft vient tout juste de sortir ! Plein de nouveautés: Tracking de squelette en Near Mode Détection en position assise Détection faciale avec un SDK dédié Documentation et des guideline (enfin) Un out...
Cliquez pour lire la suite de l'article par Vko LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) par richardc
Mise à jour des Web API du 14 Mai
Réservez dès maintenant votre journée du 20 juin pour le Windows Azure Dev Camp 2012 à Paris
Mise à jour de Team Foundation Service
MechCommander 2 sur Windows 8
Entity Framework 5 Release Candidate e...
Cliquez pour lire la suite de l'article par richardc REACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITERREACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITER par Groc
Une mauvaise utilisation de rx lors de l'écriture d'une couche d'accès à des services peut conduire à des cas embarassants avec des erreurs mal gérées, des appels qui ne partent lorsqu'ils le devraient, et même des résultats incorrects . le tout nuis...
Cliquez pour lire la suite de l'article par Groc SHAREPOINT BLOG SITE, PROBLèME D'ARCHIVESSHAREPOINT BLOG SITE, PROBLèME D'ARCHIVES par junarnoalg
Dernièrement, nous avons migré le site
myTIC
vers un nouveau serveur SharePoint 2010. Dans les contenus que nous vouloins récupérer, nous avions un certain nombre de blogs.
Nous avons utilisé les commandes Power...
Cliquez pour lire la suite de l'article par junarnoalg
Forum
MATRICE TEMPLATEMATRICE TEMPLATE par hjr2610
Cliquez pour lire la suite par hjr2610 RE : SAC A DOS RE : SAC A DOS par hadjkaddour
Cliquez pour lire la suite par hadjkaddour
Logiciels
sDEVIS-FACTURES vlPRO (8.1.0.3)SDEVIS-FACTURES VLPRO (8.1.0.3)sDEVIS-FACTURES vlPRO a été mis au point pour les particuliers, créateurs, entrepreneurs, artisa... Cliquez pour télécharger sDEVIS-FACTURES vlPRO 974 Application Server (12.2.4.6)974 APPLICATION SERVER (12.2.4.6)Développez de puissantes applications dans un environnement de 'cloud computing', clusterisé, séc... Cliquez pour télécharger 974 Application Server vPicture (1.4.2.1)VPICTURE (1.4.2.1)Avec vPicture, hébergez vos images facilement et rapidement.
vPicture est un utilitaire simple, ... Cliquez pour télécharger vPicture Easy-Planning (2.2.1.6)EASY-PLANNING (2.2.1.6)Easy-Planning permet de créer des plannings sous la représentation de diagrammes et est adapté au... Cliquez pour télécharger Easy-Planning COM-BACKUP (2.0)COM-BACKUP (2.0)
COM-BACKUP est un logiciel de sauvegarde qui permet de planifier les sauvegardes de vos dossiers ...
Cliquez pour télécharger COM-BACKUP
|