Vous êtes sur la page 1sur 7

#include <stdio.

h>

#include <stdlib.h>

typedef

struct Noeud{

int valeur;

struct Noeud* gauche;

struct Noeud* droite;

}* Arbre;

void creation2(Arbre R)
{

int x;

printf("entre un nombre");

scanf("%d",&x);

if(x==-1)
{

R=NULL;

else{

Arbre R=malloc(sizeof(Arbre));

R->valeur=x;

creation2(R->gauche);

creation2(R->droite);

Arbre findadresse(Arbre R, int x)


{ if(R==NULL) return NULL;

else if(R->valeur==x)

return R;

else return findadresse(R->gauche,x);

return findadresse(R->droite,x);

void ajouter(Arbre pere, int valfis , int position)

{
Arbre nod=malloc(sizeof(Arbre));

nod->valeur=valfis;
nod->gauche=NULL;

nod->droite=NULL;

if(pere == NULL)

printf("ce neoud n'existe pas");

else if (position==0)

pere->gauche = nod;

else if (position==1)

pere->droite = nod;

else printf("error de la position");

int calculNbrVinf(Arbre R,int x)

{
if (R==NULL) return 0;

else if (R->valeur<x)

return 1+calculNbrVinf(R->gauche,x) + calculNbrVinf (R->droite , x);

else return calculNbrVinf(R->gauche,x) + calculNbrVinf (R->droite , x);

void calculNbrVinfABR(Arbre R,int x)


{ while (R!=NULL)

if(R->valeur<x )

{
printf("%d ..",R->valeur);

calculNbrVinfABR(R->gauche,x);

calculNbrVinfABR (R->droite , x);

int NbrOCCABR(Arbre R,int x)

{
if (R==NULL) return 0;

while(x<R->valeur)

{ if (R->valeur==x)

return 1+NbrOCCABR(R->gauche,x)+NbrOCCABR(R->droite,x);

else return NbrOCCABR(R->gauche,x)+NbrOCCABR(R->droite,x);

break;

if (R->valeur==x)

return 1+NbrOCCABR(R->droite,x)+NbrOCCABR(R->gauche,x);

else return NbrOCCABR(R->droite,x)+NbrOCCABR(R->gauche,x);

void affichage(Arbre R)
{
if(R!=NULL){

printf("%d - ",R->valeur);

affichage(R->gauche);

affichage(R->droite);
}

Arbre suppression(Arbre R, int x)

{
if(R==NULL) return NULL;

else if(R->valeur==x)

R=NULL;

else return suppression(R->gauche,x);

return suppression(R->droite,x);

void main()
{

int choix=1;

int x;

int X;

int V;

int nbr;

int valfis,position;

Arbre R=(Arbre)malloc(sizeof(Arbre));

R->valeur=4;

R->gauche=(Arbre)malloc(sizeof(Arbre));

R->gauche->valeur=2;

R->gauche->gauche=(Arbre)malloc(sizeof(Arbre));

R->gauche->gauche->valeur=1;

R->gauche->gauche->gauche=NULL;
R->gauche->gauche->droite=NULL;

R->gauche->droite=(Arbre)malloc(sizeof(Arbre));

R->gauche->droite->valeur=3;

R->gauche->droite->gauche=NULL;

R->gauche->droite->droite=NULL;

R->droite=(Arbre)malloc(sizeof(Arbre));

R->droite->valeur=5;

R->droite->droite=NULL;

R->droite->gauche=NULL;

printf(" DEVOIR N°2 Atil Nour El houda G2 \n 1 pour affichage de l'arbre \n 2 pour
EXO N 2-1 (find adresse) \n
3 pour EXO N 2-2 (ajoute noeud) \n 4 pour EXO N 7 (calculer nbr neuod < X) \n
5 pour EXO N 13 (imprime les valeur < X) \n 6 pour EXO N 14 (occurence d'un
ABR) \n
7 pour suppresion Heap Min \n ");

while(choix!=0)

{ printf(" \n");

scanf("%d",&choix);

if(choix<1 || choix>10)

{ printf("errooor");

}
else
{
switch(choix)

case 1:

affichage(R);

break;
case 2:

printf("donnez la valeur de neoud ");

scanf("%d",&x);

findadresse(R,x);

break;

case 3:

printf("donnez la valeur de fis ");

scanf("%d",&valfis);

printf("donner la position ");

scanf("%d",&position);

ajouter(R,valfis,position);

break;

case 4:

printf("donnez la valeur de x ");

scanf("%d",&x);

nbr=calculNbrVinf(R,x);

printf("la nombre des neoud inferieur de %d est : %d ",x,nbr);

break;
case 5:

printf("donnez la valeur de x ");

scanf("%d",&x);

printf("les valeur qui inferieur de %d sont : ",x);

calculNbrVinfABR(R,x);

break;

case 6:

nbr=NbrOCCABR(R,x);

printf("le nombre de occurence de %d dans la ABR est : %d ",x,nbr);

break;

case 7:

printf("donnez la valeur de neoud ");

scanf("%d",&x);

suppression(R,x);

break;

Vous aimerez peut-être aussi