Ciao!
Sto cercando di creare in C++ un albero binario ordinato, che si forma ricevendo in input un numero illimitato di interi.
L'albero viene diviso in due alberi diversi, uno che racchiude i numeri dispari, l'altro i pari.
Ho prima creato il codice per l'albero ordinato unico:
Codice PHP:
#include <cstdlib>
#include <iostream>
using namespace std;
class box
{
private:
int valore;
box *destro;
box *sinistro;
box *padre;
box* getdestro()
{return destro;}
box* getsinistro()
{return sinistro;}
box* getpadre()
{return padre;}
box()
{valore=0;
destro=NULL;
sinistro=NULL;
padre=NULL;}
void set(int newvalore)
{valore=newvalore;}
int getint()
{return valore;}
void stampa()
{cout<< valore;}
void pointatsx(box *wheretopoint)
{sinistro=wheretopoint;};
void pointatdx(box *wheretopoint)
{destro=wheretopoint;}
void pointatpadre(box *wheretopoint)
{padre=wheretopoint;}
public:
void visualizza(box *punt);
void inserisci(int newvalore);
};
box *start=NULL;
box *boxpointer=NULL;
void box::inserisci(int newvalore)
{
int flag=0;
boxpointer=new box;
boxpointer->set(newvalore);
if (start==NULL)
start=boxpointer;
else
{
flag=0;
box* startcopia=start;
while(flag==0)
{
if (newvalore<=startcopia->getint())
{
if (startcopia->getsinistro()==NULL)
{
startcopia->pointatsx(boxpointer);
boxpointer->pointatpadre(startcopia);
flag=1;
}
else
startcopia=startcopia->getsinistro();
}
else
{
if (startcopia->getdestro()==NULL)
{
startcopia->pointatdx(boxpointer);
boxpointer->pointatpadre(startcopia);
flag=1;
}
else
startcopia=startcopia->getdestro();
}
}
}
}
void box::visualizza(box *nodoattuale)
{
if (start==NULL)
cout<<"\n\nL'albero e' vuoto.\n\n";
else
{
if (nodoattuale->getsinistro()!=NULL)
visualizza(nodoattuale->getsinistro());
nodoattuale->stampa();
cout << " ";
if (nodoattuale->getdestro()!=NULL)
visualizza(nodoattuale->getdestro());
}
}
int main(int argc, char *argv[])
{
box *ogg;
int valore=0;
int scelta=0;
while (scelta!=9)
{
cout<<"Digita la tua scelta:\n\n1. Per inserire un elemento nell'albero\n2. Per visualizzare l'albero\n9. Per uscire dal programma\n\n\n";
cin>>scelta;
cout<<"\n\n";
switch(scelta)
{case 1:
cout<<"\n\nDigita l'elemento da inserire:\n\n";
cin>>valore;
cout<<"\n";
ogg->inserisci(valore);
break;
case 2:
ogg->visualizza(start);
cout<<"\n\n";
break;
case 9:
break;
system("pause");}
}
system("PAUSE");
return EXIT_SUCCESS;
}
E questo codice funziona perfettamente.
Poi ho fatto delle piccole modifiche per distinguere i due alberi, quello pari e dispari:
Codice PHP:
#include <cstdlib>
#include <iostream>
using namespace std;
class box
{
private:
int valore;
box *destro;
box *sinistro;
box *padre;
box* getdestro()
{return destro;}
box* getsinistro()
{return sinistro;}
box* getpadre()
{return padre;}
box()
{valore=0;
destro=NULL;
sinistro=NULL;
padre=NULL;}
void set(int newvalore)
{valore=newvalore;}
int getint()
{return valore;}
void stampa()
{cout<< valore;}
void pointatsx(box *wheretopoint)
{sinistro=wheretopoint;};
void pointatdx(box *wheretopoint)
{destro=wheretopoint;}
void pointatpadre(box *wheretopoint)
{padre=wheretopoint;}
public:
void visualizza(box *punt);
void inserisci(int newvalore);
};
box *startpari=NULL;
box *startdispari=NULL;
box *boxpointer=NULL;
void box::inserisci(int newvalore)
{
int flag=0;
boxpointer=new box;
boxpointer->set(newvalore);
int div;
div=(newvalore%2);
if (startpari==NULL&&div==0)
startpari=boxpointer;
if (startdispari==NULL&&div!=0)
startdispari=boxpointer;
if (startpari!=NULL&&startdispari!=NULL)
{
box* startcopia=NULL;
flag=0;
if (div==0)
box* startcopia=startpari;
else
box* startcopia=startdispari;
while(flag==0)
{
if (newvalore<=startcopia->getint())
{
if (startcopia->getsinistro()==NULL)
{
startcopia->pointatsx(boxpointer);
boxpointer->pointatpadre(startcopia);
flag=1;
}
else
startcopia=startcopia->getsinistro();
}
else
{
if (startcopia->getdestro()==NULL)
{
startcopia->pointatdx(boxpointer);
boxpointer->pointatpadre(startcopia);
flag=1;
}
else
startcopia=startcopia->getdestro();
}
}
}
}
void box::visualizza(box *nodoattuale)
{
if (nodoattuale==NULL)
cout<<"\n\nL'albero e' vuoto.\n\n";
else
{
if (nodoattuale->getsinistro()!=NULL)
visualizza(nodoattuale->getsinistro());
nodoattuale->stampa();
cout << " ";
if (nodoattuale->getdestro()!=NULL)
visualizza(nodoattuale->getdestro());
}
}
int main(int argc, char *argv[])
{
box *ogg;
int valore=0;
int scelta=0;
while (scelta!=9)
{
cout<<"Digita la tua scelta:\n\n1. Per inserire un elemento nell'albero\n2. Per visualizzare l'albero pari\n3. Per visualizzare l'albero dispari\n9. Per uscire dal programma\n\n\n";
cin>>scelta;
cout<<"\n\n";
switch(scelta)
{case 1:
cout<<"\n\nDigita l'elemento da inserire:\n\n";
cin>>valore;
cout<<"\n";
ogg->inserisci(valore);
break;
case 2:
ogg->visualizza(startpari);
cout<<"\n\n";
break;
case 3:
ogg->visualizza(startdispari);
cout<<"\n\n";
break;
case 9:
break;
system("pause");}
}
system("PAUSE");
return EXIT_SUCCESS;
}
Ma, mandando in esecuzione il programma così modificato, dà errore durante l'inserimento dei dati.
Come soluzione ho semplicemente impostato 2 radici diverse su cui operare, una per il primo numero pari inserito, e l'altra per il primo numero dispari inserito.
Ma nel mio codice cosa sbaglio?
Premetto che so che ci sono anche altri modi per creare 2 alberi a seconda della parità dei numeri inseriti, forse anche più "eleganti", io stesso sto iniziando a considerarli e magari scegliere di implementare un altro metodo per farlo, ma prima di passare a questo step e quindi cambiare del tutto il modo in cui ho concepito il codice, sarei lieto se qualcuno mi riuscisse a far capire cosa c'è che non va nel codice così come l'ho scritto, perchè per quanto continui a rivederlo, mi sembra privo di errori
Grazie