Bonjour j'essaye d'ecrire un programme utilisant un arbre binaire!
Le probleme c'est que pour remplir mon arbre j'utilise une fonction recursive mais a chaque fois l'adresse de mon pointer passe à 0!
Et donc mon arbre est vide à la fin :(
----------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
struct arbre
{
/* Valeur de l'élément */
char valeur;
/* Pointeur sur l'élément suivant */
struct arbre * fils;
struct arbre * frere;
};
typedef struct arbre arbre;
void ajoutmot(char * ,arbre *,int , int * );
void retirer1lettre(char *);
void parcours_arbre(arbre *);
void ajoutmot(char * mot,arbre * root,int depart,int * l)
{
arbre * temparbre;
printf("%x\n",root);
//while we are not at the end of the world
if( * l>depart)
{
//if we are a the root of the tree
if(root == NULL)
{
//root is NULL so we can addthe letter here
//first we allow memory for the node
temparbre = (arbre *) malloc(sizeof(struct arbre));
//add information
(* temparbre).valeur = mot[depart];
(* temparbre).frere = NULL;
(* temparbre).fils = NULL;
//next letter for the next round
depart++;
//root because the arbre that we have jsut created by putting the pointer of temparbre in root
root = temparbre;
ajoutmot(mot,(* root).fils,depart,l);
}
else
if((* root).valeur == mot[depart])
{
depart++;
ajoutmot(mot,(* root).fils,depart,l);
}
else if((* root).valeur != mot[depart])
{
ajoutmot(mot,(* root).frere,depart,l);
}
}
}
void parcours_arbre(arbre *root)
{
if(root != NULL)
{
printf("%c",(*root).valeur);
parcours_arbre((*root).fils);
parcours_arbre((*root).frere);
}
}
main()
{
arbre * root;
char * mot;
int length,depart;
root = (arbre *) malloc(sizeof(struct arbre));
mot = "1,2,3";
length = strlen(mot);
depart = 0;
ajoutmot(mot,root,depart,&length);
parcours_arbre(root);
}
-----------------------------------------------
Si quelqu'un voit où est mon erreur!
Merci,
Laurent