Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct Info{
- int num;
- };
- typedef struct Info TInfo;
- struct Nodo{
- TInfo info;
- struct Nodo *destro;
- struct Nodo *sinistro;
- };
- typedef struct Nodo TNodo;
- typedef TNodo *Albero;
- Albero Crea_Albero();
- void Distruggi_Nodo(TNodo *nodo);
- Albero Inserisci_Elemento(Albero bt, TInfo info);
- TInfo Leggi_Info();
- void Stampa_Info(TInfo info);
- void Stampa_Albero(Albero bt);
- Albero Distruggi_Albero(Albero bt);
- TNodo *Ricerca(Albero bt,TInfo info);
- TNodo *Cerca_Minimo(Albero bt);
- Albero Cancella_Elemento(Albero bt,TInfo info);
- int main()
- {
- Albero bt;
- TInfo info;
- int i,dim;
- TNodo *min,*search;
- bt=Crea_Albero();
- printf("Quanti elementi vuoi inserire?: ");scanf("%d",&dim);
- for (i=0;i<dim;i++)
- {
- info=Leggi_Info();
- bt=Inserisci_Elemento(bt,info);
- }
- ///stampa albero
- printf("\nElementi dell'albero\n");
- Stampa_Albero(bt);
- ///RICERCA ELEMENTO
- printf("\n\nRicerca elemento\n");
- info=Leggi_Info();
- search=Ricerca(bt,info);
- if(search==NULL)
- printf("Elemento non presente!\n");
- else
- printf("Elemento presente!\n");
- ///INSERISCI UN NUOVO ELEM e ristampa per vedere se
- ///se è ancora in ordine l'albero binario
- printf("\nInserisci un nuovo elemento\n");
- info=Leggi_Info();
- bt=Inserisci_Elemento(bt,info);
- Stampa_Albero(bt);
- ///CANCELLA ELEMENTO e ristampa
- printf("\n\nCancella elemento\n");
- info=Leggi_Info();
- bt=Cancella_Elemento(bt,info);
- Stampa_Albero(bt);
- ///STAMPA MINIMO
- min=Cerca_Minimo(bt);
- printf("\n\nMinimo: ");
- Stampa_Info(min->info);
- printf("\n");
- return 0;
- }
- Albero Crea_Albero()
- {
- return NULL;
- }
- void Distruggi_Nodo(TNodo *nodo)
- {
- free(nodo);
- }
- Albero Inserisci_Elemento(Albero bt, TInfo info)
- {
- if(bt==NULL)
- {
- TNodo *nuovo_nodo;
- nuovo_nodo=(TNodo*)malloc(sizeof(TNodo));
- nuovo_nodo->info=info;
- nuovo_nodo->destro=NULL;
- nuovo_nodo->sinistro=NULL;
- if(nuovo_nodo==NULL)
- {
- printf("\nErrore di allocazione");
- exit(1);
- }
- return nuovo_nodo;
- }
- else if(info.num < bt->info.num)
- {
- bt->sinistro=Inserisci_Elemento(bt->sinistro,info);
- return bt;
- }
- else
- {
- bt->destro=Inserisci_Elemento(bt->destro,info);
- return bt;
- }
- }
- TInfo Leggi_Info()
- {
- TInfo info;
- printf("\nInserisci numero: ");
- scanf("%d",&(info.num));
- return info;
- }
- void Stampa_Info(TInfo info)
- {
- printf("%d ",info.num);
- }
- void Stampa_Albero(Albero bt)
- {
- if(bt!=NULL)
- {
- Stampa_Albero(bt->sinistro);
- Stampa_Info(bt->info);
- Stampa_Albero(bt->destro);
- }
- }
- Albero Distruggi_Albero(Albero bt)
- {
- if(bt==NULL)
- return NULL;
- if(bt->destro==NULL && bt->sinistro==NULL)
- {
- free(bt);
- return NULL;
- }
- else
- {
- bt->sinistro=Distruggi_Albero(bt->sinistro);
- bt->destro=Distruggi_Albero(bt->destro);
- Distruggi_Nodo(bt);
- return NULL;
- }
- }
- TNodo *Ricerca(Albero bt,TInfo info)
- {
- if(bt==NULL || bt->info.num == info.num)
- return bt;
- else
- {
- if(info.num < bt->info.num)
- return Ricerca(bt->sinistro,info);
- else
- return Ricerca(bt->destro,info);
- }
- }
- TNodo *Cerca_Minimo(Albero bt)
- {
- if(bt==NULL)
- return NULL;
- else if(bt->sinistro==NULL)
- return bt;
- else
- {
- Albero min=Cerca_Minimo(bt->sinistro);
- return min;
- }
- }
- Albero Cancella_Elemento(Albero bt,TInfo info)
- {
- if(bt==NULL)
- return NULL;
- else if(info.num < bt->info.num)
- {
- bt->sinistro=Cancella_Elemento(bt->sinistro,info);
- return bt;
- }
- else if(info.num > bt->info.num)
- {
- bt->destro=Cancella_Elemento(bt->destro,info);
- return bt;
- }
- else ///info.num == bt->info.num . E' STATO TROVATO IL NODO DA ELIMINARE
- { /// VARI CASI
- if(bt->destro==NULL && bt->sinistro==NULL)/// NODO FOGLIA
- {
- Distruggi_Nodo(bt);
- return NULL;
- }
- else if(bt->destro == NULL)/// NODO CON SOLO IL FIGLIO DX
- {
- Albero p; ///albero usato per salvare ciò che andrebbe perso dopo la cancellazione del nodo
- p=bt->sinistro;
- Distruggi_Nodo(bt);
- return p;
- }
- else if(bt->sinistro == NULL)///NODO CON SOLO IL FIGLIO SX
- {
- Albero p; ///albero usato per salvare ciò che andrebbe perso dopo la cancellazione del nodo
- p=bt->destro;
- Distruggi_Nodo(bt);
- return p;
- }
- ///NODO CON ENTRAMBI I FIGLI - DOBBIAMO PRESERVARE L'ORDINE DEI COLLEGAMENTI
- ///MIN DX mi serve trovare il minimo del sottoalbero dx per sostituirlo al nodo da eliminare
- Albero min_destro;
- min_destro=Cerca_Minimo(bt->destro);
- bt->info=min_destro->info; ///sostituisci l'elem da cancellare con il min_dx
- bt->destro=Cancella_Elemento(bt->destro,min_destro->info); ///ora dobbiamo cancellare min_dx perché
- ///ne abbiamo due a causa della
- ///sostituzione dell'elem precedente
- return bt;
- }
- }
Add Comment
Please, Sign In to add comment