Sie sind auf Seite 1von 4

//

// Arbres Part I.h


// TP Structure de données
// \[]
// Created by Adam Boukhssimi on 13/04/2017.
// Copyright © 2017 Adam Boukhssimi. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>

typedef struct arbre{


int n;
struct arbre *droite,*gauche;
}arbre;

/* J'ai marre de toujours mettre la condition d'allocation dans mes fonctions ...
alors je crée ma fonction qui alloue la
memoire et initialise les cases à 0 sinon arrête le programme :) */

void *alloueMemoire(const unsigned int taille, const unsigned int octets){


void *ptr;

ptr = calloc(taille, octets);

if (ptr == NULL){
puts("Erreur ! La mémoire vive (RAM) est insuffisante !\nLe programme va
quitter son execution ...");
exit(EXIT_FAILURE);
}

else return ptr;

arbre *creerElement(void){
arbre *sortie;

sortie = (arbre*)alloueMemoire(1,sizeof(arbre));

fprintf(stdout,"Veuillez entrer le nombre de l'element à creer :\t");


scanf("\n%d",&sortie->n);

putchar('\n');

return sortie;
}

//=================================================================================
=================================================

void insererElement(arbre **p , arbre * elem){


if(*p == NULL)*p = elem;

else {
if( (*p)->n >= elem->n ) insererElement( &(*p)->gauche, elem);
else insererElement( &(*p)->droite, elem);
}
}

//=================================================================================
=================================================

arbre *creerArbre(int cases){


arbre *premier=NULL;

while(cases > 0){


insererElement(&premier, creerElement());
cases --;
}

return premier;
}

//=================================================================================
=================================================

void afficherCroissant(const arbre *a){


if(a == NULL)return;
else{
afficherCroissant(a->gauche);
fprintf(stdout,"%d\t",a->n);
afficherCroissant(a->droite);
}
}

//=================================================================================
=================================================

void afficherDecroissant(const arbre *a){


if(a == NULL)return;
else{
afficherDecroissant(a->droite);
fprintf(stdout,"%d\t",a->n);
afficherDecroissant(a->gauche);
}
}

//=================================================================================
=================================================

unsigned int nbElements(const arbre *restrict a){


if( a == NULL) return 0;
else return 1 + nbElements(a->gauche) + nbElements(a->droite);
}

//=================================================================================
=================================================

unsigned int nbFeuilles(const arbre *restrict a){


if(a==NULL)return 0;
else if (a->droite == NULL && a->gauche == NULL)return 1;
else return nbFeuilles(a->gauche) + nbFeuilles(a->droite);
}
//=================================================================================
=================================================

void afficherFeuilles(const arbre *restrict a){


if( a== NULL)return;
else {

if (a->droite == NULL && a->gauche == NULL)fprintf(stdout,"%d\t",a->n);

else {
afficherFeuilles(a->gauche);
afficherFeuilles(a->droite);
}

}
}

//=================================================================================
=================================================

int max(const int a,const int b){


if (a<b)return b;
else return a;
}

int calculerProfondeur(arbre *restrict a){


if(a==NULL)return 0;
else return 1+max( calculerProfondeur(a->gauche) , calculerProfondeur(a-
>droite) );
}

//=================================================================================
=================================================

void supprimerArbre(arbre **a){

if (*a == NULL)return;

else if( (*a)->droite == NULL && (*a)->gauche == NULL){


free(*a);
*a = NULL;
}

else{
supprimerArbre(&(*a)->gauche);
supprimerArbre(&(*a)->droite);
}
}

//=================================================================================
=================================================

void menu(arbre **a){


int choix=12;

while (choix > 11){


puts("Pour creer un élément taper 1");
puts("Pour inserer un element taper 2");
puts("Pour creer un arbre taper 3");
puts("Pour afficher croissant taper 4");
puts("Pour afficher decroissant taper 5");
puts("Pour afficher le nombre d'elements taper 6");
puts("Pour afficher le nombre de feuilles taper 7");
puts("Pour afficher les feuilles taper 8");
puts("Pour calculer la profondeur taper 9");
puts("Pour quitter le programme taper 10");
fprintf(stdout,"Pour supprimer l'arbre taper 11 : \t");
scanf("%ud",&choix);
putchar('\n');
}

switch(choix){
case 1: *a=creerElement(); break;
case 2: insererElement(a, creerElement());break;
case 3: {
unsigned int i;
puts("Combien de cases reserver à l'arbre à creer ?");
scanf("\n%ud",&i);
supprimerArbre(a);
*a=creerArbre(i);
};break;
case 4: afficherCroissant(*a); putchar('\n');break;
case 5: afficherDecroissant(*a);putchar('\n');break;
case 6: fprintf(stdout,"Nombre d'éléments dans cet arbre :\t
%ud\n",nbElements(*a));break;
case 7: fprintf(stdout,"Nombre de feuilles dans cet arbre :\t
%ud\n",nbFeuilles(*a));break;
case 8: afficherFeuilles(*a);putchar('\n');break;
case 9: supprimerArbre(a);break;
case 10: exit(EXIT_SUCCESS);break;
case 11: supprimerArbre(a);break;
}
}

Das könnte Ihnen auch gefallen