|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Apr 2007
Messaggi: 381
|
[C] Alberi Binari Di Ricerca - Inserimento Di Un Elemento
Dov'è l'errore? Non capisco bene cosa inserire nella malloc..
Codice HTML:
struct nodo
{
int DATO;
struct nodo *DX; //PUNTATORE AL SOTTOALBERO DESTRO
struct nodo *SX; //PUNTATORE AL SOTTOALBERO SINISTRO
}NODO;
typedef struct nodo* tree;
Codice HTML:
tree InserimentoDiUnElemento(tree T, int e)
{
if(T==NULL)
{
tree new=NULL;
new=(nodo)malloc(sizeof(*tree));
new->DATO=e;
new->SX=NULL;
new->DX=NULL;
}
if(T->DATO==e)
{
printf(" ERRORE \n");
return NULL;
}
if(T->DATO>e)
{
InserimentoDiUnElemento(T->SX,e);
}else
{
InserimentoDiUnElemento(T->DX,e);
}
}
|
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Aug 2005
Messaggi: 2755
|
Perchè allochi un *tree e non un tree?
Lo spazio che devi allocare è per un nodo, non per un puntatore ad un nodo.
__________________
|
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Apr 2007
Messaggi: 381
|
mi da ancora questi errori
error: expected identifier before '=' token error: no matching function for call to `nodo::nodo(void*)' |
|
|
|
|
|
#4 |
|
Member
Iscritto dal: Dec 2007
Messaggi: 190
|
malloc restituisce un puntatore *void
Codice:
#include <stdio.h>
main(){
struct nodo
{
int DATO;
struct nodo *DX; //PUNTATORE AL SOTTOALBERO DESTRO
struct nodo *SX; //PUNTATORE AL SOTTOALBERO SINISTRO
}NODO;
typedef struct nodo* tree;
tree InserimentoDiUnElemento(tree T, int e)
{
if(T==NULL)
{
tree new=NULL;
new=(void *)malloc(sizeof(tree));
new->DATO=e;
new->SX=NULL;
new->DX=NULL;
}
if(T->DATO==e)
{
printf(" ERRORE \n");
return NULL;
}
if(T->DATO>e)
{
InserimentoDiUnElemento(T->SX,e);
}else
{
InserimentoDiUnElemento(T->DX,e);
}
}
}
Ultima modifica di xsatellitex : 02-05-2008 alle 10:40. |
|
|
|
|
|
#5 |
|
Senior Member
Iscritto dal: Apr 2007
Messaggi: 381
|
come a char????
|
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Apr 2007
Messaggi: 381
|
comq sbagliavo ad utilizzare la parola new che nel c non si può usare, però se metto char nella malloc da errore lo stesso
|
|
|
|
|
|
#7 |
|
Member
Iscritto dal: Dec 2007
Messaggi: 190
|
scusami volevo dire void ... ho corretto sopra
|
|
|
|
|
|
#8 |
|
Member
Iscritto dal: Dec 2007
Messaggi: 190
|
in C la keyword new non esiste... è come una qualsiasi altra parola...
tu la stai utilizzando come puntatore quindi va bene |
|
|
|
|
|
#9 |
|
Senior Member
Iscritto dal: Apr 2007
Messaggi: 381
|
mi da quest'altro errore ora
error: void value not ignored as it ought to be Codice HTML:
tree InserimentoDiUnElemento(tree T, int e)
{
if(T==NULL)
{
tree nuo=NULL;
nuo=(void)malloc(sizeof(tree));
nuo->DATO=e;
nuo->SX=NULL;
nuo->DX=NULL;
}
if(T->DATO==e)
{
printf(" ERRORE \n");
return NULL;
}
if(T->DATO>e)
{
InserimentoDiUnElemento(T->SX,e);
}else
{
InserimentoDiUnElemento(T->DX,e);
}
}
|
|
|
|
|
|
#10 |
|
Member
Iscritto dal: Dec 2007
Messaggi: 190
|
devi mettere l'asterisco dopo void come ho scritto sopra...
malloc ritorna un puntatore all'area di memoria allocata... il puntatore restituito è di tipo void.. |
|
|
|
|
|
#11 |
|
Senior Member
Iscritto dal: Apr 2007
Messaggi: 381
|
niente, non va..puoi trovarmi tu l'errore?
Codice HTML:
#include <iostream> #include <stdlib.h> using namespace std; struct nodo { int DATO; struct nodo *DX; //PUNTATORE AL SOTTOALBERO DESTRO struct nodo *SX; //PUNTATORE AL SOTTOALBERO SINISTRO }NODO; typedef struct nodo* tree; bool Is_Empty(tree RADICE) //RESTITUISCE TRUE SE L'ALBERO E' VUOTO, ALTRIMENTI RESTITUISCE FALSE { return(RADICE==NULL); } tree Albero_Vuoto(void) //COSTRUISCE UN ALBERO VUOTO { return NULL; } int Valore_Etichetta(tree RADICE) //RESTITUISCE IL VALORE (O ETICHETTA) DEL NODO { if (Is_Empty(RADICE)) abort(); else return(RADICE->DATO); } tree Sinistro(tree RADICE) //RESTITUISCE IL PUNTATORE AL SOTTOALBERO SINISTRO { if (Is_Empty(RADICE)) return NULL; else return(RADICE->SX); } tree Destro(tree RADICE) //RESTITUISCE IL PUNTATORE AL SOTTOALBERO DESTRO { if (Is_Empty(RADICE)) return(NULL); else return(RADICE->DX); } tree Costruisci_Albero(int ETICHETTA,tree S,tree D) //COSTRUISCE UN ALBERO BINARIO NON ORDINATO { tree RADICE; RADICE = (nodo *) malloc(sizeof(NODO)); RADICE->DATO = ETICHETTA; RADICE->SX = S; RADICE->DX = D; return (RADICE); } void Inorder(tree RADICE) //STAMPA INORDER DELL'ALBERO { if (!(Is_Empty(RADICE))) { Inorder(Sinistro(RADICE)); cout<<Valore_Etichetta(RADICE)<<" "; Inorder(Destro(RADICE)); } } void Preorder(tree RADICE) //STAMPA PREORDER DELL'ALBERO { if (!(Is_Empty(RADICE))) { cout<<Valore_Etichetta(RADICE)<<" "; Preorder(Sinistro(RADICE)); Preorder(Destro(RADICE)); } } void Postorder(tree RADICE) //STAMPA POSTORDER DELL'ALBERO { if (!(Is_Empty(RADICE))) { Postorder(Sinistro(RADICE)); Postorder(Destro(RADICE)); cout<<Valore_Etichetta(RADICE)<<" "; } } int ContaNodi(tree RADICE) //RESTITUISCE IL NUMERO DEI NODI DELL'ALBERO { if(Is_Empty(RADICE)) return 0; else return(1 + ContaNodi(Sinistro(RADICE)) + ContaNodi(Destro(RADICE))); } int ContaFoglie(tree RADICE) //RESTITUISCE IL NUMERO DELLE FOGLIE DELL'ALBERO { if(Is_Empty(RADICE)) return(0); else { if ((Sinistro(RADICE)==NULL) && (Destro(RADICE)==NULL)) return(1); else return( ContaFoglie(Sinistro(RADICE)) + ContaFoglie(Destro(RADICE)) ); } } bool Perf_Bil(tree RADICE) //Restituisce TRUE se l'albero è perfettamente bilanciato { if (Is_Empty(RADICE)) return(true); //Oppure ERROR, secondo la definizione else { if ((Sinistro(RADICE)==NULL) && (Destro(RADICE)==NULL)) //Praticamente: se il nodo è una foglia.. return(true); else { if ((Sinistro(RADICE)!=NULL) && (Destro(RADICE)!=NULL)) //Se il nodo ha tutti e due i figli.. return( Perf_Bil(Sinistro(RADICE)) && Perf_Bil(Destro(RADICE)) ); else return(false); } } } bool Ricerca(tree RADICE,int X) //RICERCA UN VALORE X IN UN ALBERO PUNTATO DA RADICE { if (Is_Empty(RADICE)) return(false); else { if (X==Valore_Etichetta(RADICE)) return(true); else return(Ricerca(Sinistro(RADICE),X) || Ricerca(Destro(RADICE),X)); } } int Altezza_Nodo(tree N) //Restituisce l'altezza di un nodo (vedere definizione di altezza) { int ALTD=0,ALTS=0; if (Is_Empty(N)) return(-1); else { ALTS=Altezza_Nodo(Sinistro(N)); ALTD=Altezza_Nodo(Destro(N)); if (ALTS>ALTD) return(ALTS+1); else return(ALTD+1); } } tree Ins_Ord(int E,tree RADICE) //Costruisce un albero binario di ricerca (un albero "ordinato") { if (Is_Empty(RADICE)) { RADICE=(tree)malloc(sizeof(NODO)); //chiede l'indirizzo di una cella di memoria libera RADICE->DATO=E; RADICE->SX=NULL; RADICE->DX=NULL; return RADICE; } else { if(E<Valore_Etichetta(RADICE)) { RADICE->SX=Ins_Ord(E,Sinistro(RADICE)); return RADICE; } else { RADICE->DX=Ins_Ord(E,Destro(RADICE)); return RADICE; } } } bool RicercaBinaria(int X,tree RADICE) //Ricerca dicotomica (o binaria) per alberi binari di ricerca (ordinati) { if (Is_Empty(RADICE)) return(false); else { if (X==Valore_Etichetta(RADICE)) return(true); else { if (X < Valore_Etichetta(RADICE)) return( RicercaBinaria(X,Sinistro(RADICE)) ); else return( RicercaBinaria(X,Destro(RADICE)) ); } } } ///////////////////////////////////////////////////////////////////////////////////////////////////////// tree InserimentoDiUnElemento(tree T, int e) { if(T==NULL) { tree nuo=NULL; nuo=(void*)malloc(sizeof(tree)); nuo->DATO=e; nuo->SX=NULL; nuo->DX=NULL; } if(T->DATO==e) { printf(" ERRORE \n"); return NULL; } if(T->DATO>e) { InserimentoDiUnElemento(T->SX,e); }else { InserimentoDiUnElemento(T->DX,e); } } int main(void) { tree t1,t2,t3,t4,t5,t6,t7; t4=Costruisci_Albero(3,Albero_Vuoto(),Albero_Vuoto()); t5=Costruisci_Albero(6,Albero_Vuoto(),Albero_Vuoto()); t6=Costruisci_Albero(22,Albero_Vuoto(),Albero_Vuoto()); t7=Costruisci_Albero(26,Albero_Vuoto(),Albero_Vuoto()); t2=Costruisci_Albero(5,t4,t5); t3=Costruisci_Albero(24,t6,t7); t1=Costruisci_Albero(10,t2,t3); t1=InserimentoDiUnElemento(t1,7); Inorder(t1); } |
|
|
|
|
|
#12 |
|
Member
Iscritto dal: Dec 2007
Messaggi: 190
|
hai dimenticato di nuovo il void in costruisci albero...
che errore ti da? |
|
|
|
|
|
#13 |
|
Senior Member
Iscritto dal: Apr 2007
Messaggi: 381
|
error: invalid conversion from `void*' to `nodo*'
comq penso che l'errore sia solo nella funzione InserimentoDiUnElemento, perchè tutto il resto funziona, gia ho provato... |
|
|
|
|
|
#14 |
|
Member
Iscritto dal: Dec 2007
Messaggi: 190
|
main()
tree Albero_Vuoto() al posto di int main(void) tree Albero_Vuoto(void) fammi sapere p.s. inserimentodiunelemento io ho compilato quello che ti ho scritto prima e va! Ultima modifica di xsatellitex : 02-05-2008 alle 11:51. |
|
|
|
|
|
#15 |
|
Senior Member
Iscritto dal: Apr 2007
Messaggi: 381
|
non va, ma com'è possibile...
mi mandi quello che a te compila? |
|
|
|
|
|
#16 |
|
Member
Iscritto dal: Dec 2007
Messaggi: 190
|
quello che a me compila l ho scritto sopra.
ti da ancora errore col void? ti dice anche a che riga ? |
|
|
|
|
|
#17 |
|
Senior Member
Iscritto dal: Apr 2007
Messaggi: 381
|
si errore con void, riga 198, io devo scappare a pranzo torno dopo, se scopri qualcosa lascia un messaggio comq, grazie
Codice HTML:
#include <iostream> #include <stdlib.h> using namespace std; struct nodo { int DATO; struct nodo *DX; //PUNTATORE AL SOTTOALBERO DESTRO struct nodo *SX; //PUNTATORE AL SOTTOALBERO SINISTRO }NODO; typedef struct nodo* tree; bool Is_Empty(tree RADICE) //RESTITUISCE TRUE SE L'ALBERO E' VUOTO, ALTRIMENTI RESTITUISCE FALSE { return(RADICE==NULL); } tree Albero_Vuoto(void) //COSTRUISCE UN ALBERO VUOTO { return NULL; } int Valore_Etichetta(tree RADICE) //RESTITUISCE IL VALORE (O ETICHETTA) DEL NODO { if (Is_Empty(RADICE)) abort(); else return(RADICE->DATO); } tree Sinistro(tree RADICE) //RESTITUISCE IL PUNTATORE AL SOTTOALBERO SINISTRO { if (Is_Empty(RADICE)) return NULL; else return(RADICE->SX); } tree Destro(tree RADICE) //RESTITUISCE IL PUNTATORE AL SOTTOALBERO DESTRO { if (Is_Empty(RADICE)) return(NULL); else return(RADICE->DX); } tree Costruisci_Albero(int ETICHETTA,tree S,tree D) //COSTRUISCE UN ALBERO BINARIO NON ORDINATO { tree RADICE; RADICE = (nodo *) malloc(sizeof(NODO)); RADICE->DATO = ETICHETTA; RADICE->SX = S; RADICE->DX = D; return (RADICE); } void Inorder(tree RADICE) //STAMPA INORDER DELL'ALBERO { if (!(Is_Empty(RADICE))) { Inorder(Sinistro(RADICE)); cout<<Valore_Etichetta(RADICE)<<" "; Inorder(Destro(RADICE)); } } void Preorder(tree RADICE) //STAMPA PREORDER DELL'ALBERO { if (!(Is_Empty(RADICE))) { cout<<Valore_Etichetta(RADICE)<<" "; Preorder(Sinistro(RADICE)); Preorder(Destro(RADICE)); } } void Postorder(tree RADICE) //STAMPA POSTORDER DELL'ALBERO { if (!(Is_Empty(RADICE))) { Postorder(Sinistro(RADICE)); Postorder(Destro(RADICE)); cout<<Valore_Etichetta(RADICE)<<" "; } } int ContaNodi(tree RADICE) //RESTITUISCE IL NUMERO DEI NODI DELL'ALBERO { if(Is_Empty(RADICE)) return 0; else return(1 + ContaNodi(Sinistro(RADICE)) + ContaNodi(Destro(RADICE))); } int ContaFoglie(tree RADICE) //RESTITUISCE IL NUMERO DELLE FOGLIE DELL'ALBERO { if(Is_Empty(RADICE)) return(0); else { if ((Sinistro(RADICE)==NULL) && (Destro(RADICE)==NULL)) return(1); else return( ContaFoglie(Sinistro(RADICE)) + ContaFoglie(Destro(RADICE)) ); } } bool Perf_Bil(tree RADICE) //Restituisce TRUE se l'albero è perfettamente bilanciato { if (Is_Empty(RADICE)) return(true); //Oppure ERROR, secondo la definizione else { if ((Sinistro(RADICE)==NULL) && (Destro(RADICE)==NULL)) //Praticamente: se il nodo è una foglia.. return(true); else { if ((Sinistro(RADICE)!=NULL) && (Destro(RADICE)!=NULL)) //Se il nodo ha tutti e due i figli.. return( Perf_Bil(Sinistro(RADICE)) && Perf_Bil(Destro(RADICE)) ); else return(false); } } } bool Ricerca(tree RADICE,int X) //RICERCA UN VALORE X IN UN ALBERO PUNTATO DA RADICE { if (Is_Empty(RADICE)) return(false); else { if (X==Valore_Etichetta(RADICE)) return(true); else return(Ricerca(Sinistro(RADICE),X) || Ricerca(Destro(RADICE),X)); } } int Altezza_Nodo(tree N) //Restituisce l'altezza di un nodo (vedere definizione di altezza) { int ALTD=0,ALTS=0; if (Is_Empty(N)) return(-1); else { ALTS=Altezza_Nodo(Sinistro(N)); ALTD=Altezza_Nodo(Destro(N)); if (ALTS>ALTD) return(ALTS+1); else return(ALTD+1); } } tree Ins_Ord(int E,tree RADICE) //Costruisce un albero binario di ricerca (un albero "ordinato") { if (Is_Empty(RADICE)) { RADICE=(tree)malloc(sizeof(NODO)); //chiede l'indirizzo di una cella di memoria libera RADICE->DATO=E; RADICE->SX=NULL; RADICE->DX=NULL; return RADICE; } else { if(E<Valore_Etichetta(RADICE)) { RADICE->SX=Ins_Ord(E,Sinistro(RADICE)); return RADICE; } else { RADICE->DX=Ins_Ord(E,Destro(RADICE)); return RADICE; } } } bool RicercaBinaria(int X,tree RADICE) //Ricerca dicotomica (o binaria) per alberi binari di ricerca (ordinati) { if (Is_Empty(RADICE)) return(false); else { if (X==Valore_Etichetta(RADICE)) return(true); else { if (X < Valore_Etichetta(RADICE)) return( RicercaBinaria(X,Sinistro(RADICE)) ); else return( RicercaBinaria(X,Destro(RADICE)) ); } } } ///////////////////////////////////////////////////////////////////////////////////////////////////////// tree InserimentoDiUnElemento(tree T, int e) { if(T==NULL) { tree nuo=NULL; nuo=(void*)malloc(sizeof(tree)); nuo->DATO=e; nuo->SX=NULL; nuo->DX=NULL; } if(T->DATO==e) { printf(" ERRORE \n"); return NULL; } if(T->DATO>e) { InserimentoDiUnElemento(T->SX,e); }else { InserimentoDiUnElemento(T->DX,e); } } int main() { tree t1,t2,t3,t4,t5,t6,t7; t4=Costruisci_Albero(3,Albero_Vuoto(),Albero_Vuoto()); t5=Costruisci_Albero(6,Albero_Vuoto(),Albero_Vuoto()); t6=Costruisci_Albero(22,Albero_Vuoto(),Albero_Vuoto()); t7=Costruisci_Albero(26,Albero_Vuoto(),Albero_Vuoto()); t2=Costruisci_Albero(5,t4,t5); t3=Costruisci_Albero(24,t6,t7); t1=Costruisci_Albero(10,t2,t3); t1=InserimentoDiUnElemento(t1,7); Inorder(t1); } |
|
|
|
|
|
#18 |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 2788
|
Provo a dare la soluzione
Ogni volta che fai la malloc quello che allochi è un nodo, giusto? Quindi alla sizeof dovresti passargli NODO. Con tree non dovrebbe funzionare (o meglio non dovrebbe fare quello che vuoi tu) perché tree non è di tipo NODO ma di tipo puntatore a NODO. Inoltre la malloc ti restituisce un puntatore a void, quindi devi fare un casting al tipo desiderato, in questo caso un puntatore a nodo, quindi tree, o se preferisci *NODO. |
|
|
|
|
|
#19 |
|
Senior Member
Iscritto dal: Apr 2007
Messaggi: 381
|
ho corretto come dici tu però quando compilo mi da il classico errore "non inviare..." puoi provarlo un attimo tu?
Codice HTML:
#include <iostream> #include <stdlib.h> using namespace std; struct nodo { int DATO; struct nodo *DX; //PUNTATORE AL SOTTOALBERO DESTRO struct nodo *SX; //PUNTATORE AL SOTTOALBERO SINISTRO }NODO; typedef struct nodo* tree; bool Is_Empty(tree RADICE) //RESTITUISCE TRUE SE L'ALBERO E' VUOTO, ALTRIMENTI RESTITUISCE FALSE { return(RADICE==NULL); } tree Albero_Vuoto(void) //COSTRUISCE UN ALBERO VUOTO { return NULL; } int Valore_Etichetta(tree RADICE) //RESTITUISCE IL VALORE (O ETICHETTA) DEL NODO { if (Is_Empty(RADICE)) abort(); else return(RADICE->DATO); } tree Sinistro(tree RADICE) //RESTITUISCE IL PUNTATORE AL SOTTOALBERO SINISTRO { if (Is_Empty(RADICE)) return NULL; else return(RADICE->SX); } tree Destro(tree RADICE) //RESTITUISCE IL PUNTATORE AL SOTTOALBERO DESTRO { if (Is_Empty(RADICE)) return(NULL); else return(RADICE->DX); } tree Costruisci_Albero(int ETICHETTA,tree S,tree D) //COSTRUISCE UN ALBERO BINARIO NON ORDINATO { tree RADICE; RADICE = (nodo *) malloc(sizeof(NODO)); RADICE->DATO = ETICHETTA; RADICE->SX = S; RADICE->DX = D; return (RADICE); } void Inorder(tree RADICE) //STAMPA INORDER DELL'ALBERO { if (!(Is_Empty(RADICE))) { Inorder(Sinistro(RADICE)); cout<<Valore_Etichetta(RADICE)<<" "; Inorder(Destro(RADICE)); } } void Preorder(tree RADICE) //STAMPA PREORDER DELL'ALBERO { if (!(Is_Empty(RADICE))) { cout<<Valore_Etichetta(RADICE)<<" "; Preorder(Sinistro(RADICE)); Preorder(Destro(RADICE)); } } void Postorder(tree RADICE) //STAMPA POSTORDER DELL'ALBERO { if (!(Is_Empty(RADICE))) { Postorder(Sinistro(RADICE)); Postorder(Destro(RADICE)); cout<<Valore_Etichetta(RADICE)<<" "; } } int ContaNodi(tree RADICE) //RESTITUISCE IL NUMERO DEI NODI DELL'ALBERO { if(Is_Empty(RADICE)) return 0; else return(1 + ContaNodi(Sinistro(RADICE)) + ContaNodi(Destro(RADICE))); } int ContaFoglie(tree RADICE) //RESTITUISCE IL NUMERO DELLE FOGLIE DELL'ALBERO { if(Is_Empty(RADICE)) return(0); else { if ((Sinistro(RADICE)==NULL) && (Destro(RADICE)==NULL)) return(1); else return( ContaFoglie(Sinistro(RADICE)) + ContaFoglie(Destro(RADICE)) ); } } bool Perf_Bil(tree RADICE) //Restituisce TRUE se l'albero è perfettamente bilanciato { if (Is_Empty(RADICE)) return(true); //Oppure ERROR, secondo la definizione else { if ((Sinistro(RADICE)==NULL) && (Destro(RADICE)==NULL)) //Praticamente: se il nodo è una foglia.. return(true); else { if ((Sinistro(RADICE)!=NULL) && (Destro(RADICE)!=NULL)) //Se il nodo ha tutti e due i figli.. return( Perf_Bil(Sinistro(RADICE)) && Perf_Bil(Destro(RADICE)) ); else return(false); } } } bool Ricerca(tree RADICE,int X) //RICERCA UN VALORE X IN UN ALBERO PUNTATO DA RADICE { if (Is_Empty(RADICE)) return(false); else { if (X==Valore_Etichetta(RADICE)) return(true); else return(Ricerca(Sinistro(RADICE),X) || Ricerca(Destro(RADICE),X)); } } int Altezza_Nodo(tree N) //Restituisce l'altezza di un nodo (vedere definizione di altezza) { int ALTD=0,ALTS=0; if (Is_Empty(N)) return(-1); else { ALTS=Altezza_Nodo(Sinistro(N)); ALTD=Altezza_Nodo(Destro(N)); if (ALTS>ALTD) return(ALTS+1); else return(ALTD+1); } } tree Ins_Ord(int E,tree RADICE) //Costruisce un albero binario di ricerca (un albero "ordinato") { if (Is_Empty(RADICE)) { RADICE=(tree)malloc(sizeof(NODO)); //chiede l'indirizzo di una cella di memoria libera RADICE->DATO=E; RADICE->SX=NULL; RADICE->DX=NULL; return RADICE; } else { if(E<Valore_Etichetta(RADICE)) { RADICE->SX=Ins_Ord(E,Sinistro(RADICE)); return RADICE; } else { RADICE->DX=Ins_Ord(E,Destro(RADICE)); return RADICE; } } } bool RicercaBinaria(int X,tree RADICE) //Ricerca dicotomica (o binaria) per alberi binari di ricerca (ordinati) { if (Is_Empty(RADICE)) return(false); else { if (X==Valore_Etichetta(RADICE)) return(true); else { if (X < Valore_Etichetta(RADICE)) return( RicercaBinaria(X,Sinistro(RADICE)) ); else return( RicercaBinaria(X,Destro(RADICE)) ); } } } ///////////////////////////////////////////////////////////////////////////////////////////////////////// tree InserimentoDiUnElemento(tree T, int e) { if(T==NULL) { tree nuovo; nuovo=(tree)malloc(sizeof(NODO)); nuovo->DATO=e; nuovo->SX=NULL; nuovo->DX=NULL; } if(T->DATO==e) { printf(" ERRORE \n"); return NULL; } if(T->DATO>e) { InserimentoDiUnElemento(T->SX,e); }else { InserimentoDiUnElemento(T->DX,e); } } int main() { tree t1,t2,t3,t4,t5,t6,t7; t4=Costruisci_Albero(3,Albero_Vuoto(),Albero_Vuoto()); t5=Costruisci_Albero(6,Albero_Vuoto(),Albero_Vuoto()); t6=Costruisci_Albero(22,Albero_Vuoto(),Albero_Vuoto()); t7=Costruisci_Albero(26,Albero_Vuoto(),Albero_Vuoto()); t2=Costruisci_Albero(5,t4,t5); t3=Costruisci_Albero(24,t6,t7); t1=Costruisci_Albero(10,t2,t3); t1=InserimentoDiUnElemento(t1,7); Inorder(t1); } |
|
|
|
|
|
#20 |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 2788
|
Codice:
void InserimentoDiUnElemento(tree T, int e)
{
if(T==NULL)
{
tree nuovo;
nuovo=(tree)malloc(sizeof(NODO));
nuovo->DATO=e;
nuovo->SX=NULL;
nuovo->DX=NULL;
}
//Ho creato una catena di else if
else if(T->DATO==e)
{
printf(" ERRORE \n");
//Ho tolto il return
}
else if(T->DATO>e)
{
InserimentoDiUnElemento(T->SX,e);
}else
{
InserimentoDiUnElemento(T->DX,e);
}
}
int main()
{
tree t1,t2,t3,t4,t5,t6,t7;
t4=Costruisci_Albero(3,Albero_Vuoto(),Albero_Vuoto());
t5=Costruisci_Albero(6,Albero_Vuoto(),Albero_Vuoto());
t6=Costruisci_Albero(22,Albero_Vuoto(),Albero_Vuoto());
t7=Costruisci_Albero(26,Albero_Vuoto(),Albero_Vuoto());
t2=Costruisci_Albero(5,t4,t5);
t3=Costruisci_Albero(24,t6,t7);
t1=Costruisci_Albero(10,t2,t3);
/*Ho tolto l'assegnamento*/InserimentoDiUnElemento(t1,7);
Inorder(t1);
system("pause");
}
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 06:08.



















