Arbre binaire de recherche - insertion - segmentation fault
Bonjour,
Je travaille actuellement sur les arbres binaires de recherche mais malheureusement après compilation on m'affiche segmentation fault . Je ne connais pas l'origine de l'erreur mais c'est probablement du à ma fonction insertion qui n'insère aucune valeur:
Code:
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
| void abr::inserer(int y)
{
if(racine==NULL)
{
racine->cle=y;
}
else
{
inserer(y,racine);
}
}
void abr::inserer(int y, noeud * p)
{
if(p!=NULL)
{
if(y < p->cle && p->fg!=NULL)
inserer(y,p->fg);
if(y > p->cle && p->fd!=NULL)
inserer(y,p->fd);
}
if(y > p->cle)
{
if(p->fg!=NULL)
{
noeud * tmp = new noeud;
tmp->fg=p->fg;
tmp->cle=y;
tmp->fd=NULL;
}
else
{
p->fg->cle=y;
}
}
if(y<p->cle)
{
if(p->fd!=NULL)
{
noeud * tmp = new noeud;
tmp->fd=p->fd;
tmp->cle=y;
tmp->fg=NULL;
}
else
{
p->fd->cle=y;
}
}
} |
voici mon constructeur et mon destructeur de mon arbre binaire de recherche:
Code:
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
|
//constructeur
abr::abr()
{
racine=NULL;
}
//destructeur utilisant la fonction récursive suppression
abr::~abr()
{
if(racine!=NULL)
suppression(racine);
}
//fonction récursive suppression
void abr::suppression(noeud *p)
{
if(p==NULL) return;
if(p->fg!=NULL)
suppression(p->fg);
p->fg=NULL;
if(p->fd!=NULL)
suppression(p->fd);
p->fd=NULL;
delete p;
} |
Je vous remercie pour toute éventuelle aide.