1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
   |  
typedef char data;
 
typedef struct noeud {
	struct noeud *fg, *fd, *f;
	data lettre;
}noeud, *Tree;
 
/* Alloue une feuille
 * */
Tree alloueNoeud(char lettre) {
	Tree noeud = malloc(sizeof(noeud));
	noeud->f=noeud->fd=noeud->fg=NULL;
	noeud->lettre=lettre;
	return noeud;
}
 
/* Fonction qui ajoute un mot dans un arbre
 * Parametres: l'arbre dans lequel on fait l'ajout, le mot a ajouter
 * Return: 1 si l'ajout c'est bien passé, 0 si le mot est deja dedans ou si erreur
 * */
int ajoute(Tree *T, char *word) {
	if(!(*T)) {
		while((*word)!='\0') {
			(*T)=alloueNoeud(*word);
			word++;
			printf("%c\n",(*T)->lettre);
			(*T)=(*T)->f;
		}
		return 1;
	}
	else {
		if(*word < (*T)->lettre)
	  	      ajoute(&((*T)->fg),word++);
	       if(*word > (*T)->lettre)
	  	  ajoute(&((*T)->fd),word++);
	        ajoute(&((*T)->f),word++);
  }
  return 1;
}
 
/* Fonction qui affiche l'arbre
 * Parametres: l'arbre qu'on doit afficher
 * Return: 
 * */
void printTree(Tree T) {
	if(!T) {
		printf("il est vide pr le print\n");
		return ;
	}
	printf("%c", T->lettre);
 
	printTree(T->fg);
	printTree(T->f);
	printTree(T->fd);
}
 
int main(int argc, char **argv) {
	Tree tree = malloc(sizeof(noeud));
	tree->f=tree->fd=tree->fg=NULL;
 
	ajoute(&tree,"coucou");
	//test(mot);
 
	printf("on affiche:\n");
	printTree(tree);
 
	return 0;
} | 
Partager