Torna indietro   Hardware Upgrade Forum > Software > Programmazione

Recensione REDMI Note 17 Pro: il midrange con batteria da 8.340 mAh e ricarica veloce
Recensione REDMI Note 17 Pro: il midrange con batteria da 8.340 mAh e ricarica veloce
REDMI Note 17 Pro porta in fascia media una batteria da 8.340 mAh con ricarica HyperCharge a 67W, un display AMOLED da 6,83 pollici capace di picchi di luminosità molto elevati e una struttura certificata TÜV SÜD contro cadute e infiltrazioni d'acqua, il tutto racchiuso in una scocca da 223 grammi. Lo abbiamo provato per diversi giorni tra fotocamera, prestazioni, autonomia e prezzo sul mercato italiano
Insta360 Luna Ultra: la potenza del sensore da 1 pollice incontra la portabilità estrema
Insta360 Luna Ultra: la potenza del sensore da 1 pollice incontra la portabilità estrema
Insta360 Luna Ultra integra un sensore da 1 pollice 8K, ottiche Leica e triplo chip IA. Tra schermo OLED rimovibile, workflow I-Log a 10 bit e stabilizzazione a tre assi, analizziamo le doti tecniche di una gimbal camera pensata per i professionisti
Marvel's Wolverine, la recensione: Logan torna protagonista in un'avventura brutale e intensa
Marvel's Wolverine, la recensione: Logan torna protagonista in un'avventura brutale e intensa
Marvel's Wolverine porta Logan in un'avventura inedita, violenta e fortemente narrativa, costruita attorno alla sua natura di combattente e al difficile rapporto con il proprio passato. Insomniac Games punta su combattimenti spettacolari, progressione e personalizzazione, inserendo l'azione in un mondo segnato dalla persecuzione dei mutanti. Un viaggio intenso, che alterna mattanza, esplorazione e momenti sorprendentemente emotivi.
Tutti gli articoli Tutte le news

Vai al Forum
Rispondi
 
Strumenti
Old 01-05-2008, 21:19   #1
xbubbax
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);
    }
}
xbubbax è offline   Rispondi citando il messaggio o parte di esso
Old 01-05-2008, 23:25   #2
wisher
Senior Member
 
L'Avatar di wisher
 
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.
__________________
wisher è offline   Rispondi citando il messaggio o parte di esso
Old 02-05-2008, 08:31   #3
xbubbax
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*)'
xbubbax è offline   Rispondi citando il messaggio o parte di esso
Old 02-05-2008, 09:04   #4
xsatellitex
Member
 
L'Avatar di xsatellitex
 
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 09:40.
xsatellitex è offline   Rispondi citando il messaggio o parte di esso
Old 02-05-2008, 09:33   #5
xbubbax
Senior Member
 
Iscritto dal: Apr 2007
Messaggi: 381
come a char????
xbubbax è offline   Rispondi citando il messaggio o parte di esso
Old 02-05-2008, 09:36   #6
xbubbax
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
xbubbax è offline   Rispondi citando il messaggio o parte di esso
Old 02-05-2008, 09:41   #7
xsatellitex
Member
 
L'Avatar di xsatellitex
 
Iscritto dal: Dec 2007
Messaggi: 190
scusami volevo dire void ... ho corretto sopra
xsatellitex è offline   Rispondi citando il messaggio o parte di esso
Old 02-05-2008, 09:44   #8
xsatellitex
Member
 
L'Avatar di xsatellitex
 
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
xsatellitex è offline   Rispondi citando il messaggio o parte di esso
Old 02-05-2008, 09:49   #9
xbubbax
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);
    }
}
xbubbax è offline   Rispondi citando il messaggio o parte di esso
Old 02-05-2008, 09:52   #10
xsatellitex
Member
 
L'Avatar di xsatellitex
 
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..

xsatellitex è offline   Rispondi citando il messaggio o parte di esso
Old 02-05-2008, 10:08   #11
xbubbax
Senior Member
 
Iscritto dal: Apr 2007
Messaggi: 381
niente, non va..puoi trovarmi tu l'errore? ti incollo tutto il file magari sbaglio in qualche altro punto..la zona che mi interessa sta in basso, senza che leggi tutte le funzioni

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);




}
xbubbax è offline   Rispondi citando il messaggio o parte di esso
Old 02-05-2008, 10:32   #12
xsatellitex
Member
 
L'Avatar di xsatellitex
 
Iscritto dal: Dec 2007
Messaggi: 190
hai dimenticato di nuovo il void in costruisci albero...
che errore ti da?
xsatellitex è offline   Rispondi citando il messaggio o parte di esso
Old 02-05-2008, 10:39   #13
xbubbax
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...
xbubbax è offline   Rispondi citando il messaggio o parte di esso
Old 02-05-2008, 10:46   #14
xsatellitex
Member
 
L'Avatar di xsatellitex
 
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 10:51.
xsatellitex è offline   Rispondi citando il messaggio o parte di esso
Old 02-05-2008, 11:07   #15
xbubbax
Senior Member
 
Iscritto dal: Apr 2007
Messaggi: 381
non va, ma com'è possibile...
mi mandi quello che a te compila?
xbubbax è offline   Rispondi citando il messaggio o parte di esso
Old 02-05-2008, 11:12   #16
xsatellitex
Member
 
L'Avatar di xsatellitex
 
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 ?
xsatellitex è offline   Rispondi citando il messaggio o parte di esso
Old 02-05-2008, 11:14   #17
xbubbax
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);




}
xbubbax è offline   Rispondi citando il messaggio o parte di esso
Old 02-05-2008, 12:08   #18
wingman87
Senior Member
 
Iscritto dal: Nov 2005
Messaggi: 2794
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.
wingman87 è offline   Rispondi citando il messaggio o parte di esso
Old 02-05-2008, 12:12   #19
xbubbax
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);

}
xbubbax è offline   Rispondi citando il messaggio o parte di esso
Old 02-05-2008, 13:52   #20
wingman87
Senior Member
 
Iscritto dal: Nov 2005
Messaggi: 2794
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");
}
Comunque stai mischiando C con C++, iostream è una libreria C++
wingman87 è offline   Rispondi citando il messaggio o parte di esso
 Rispondi


Recensione REDMI Note 17 Pro: il midrange con batteria da 8.340 mAh e ricarica veloce Recensione REDMI Note 17 Pro: il midrange con ba...
Insta360 Luna Ultra: la potenza del sensore da 1 pollice incontra la portabilità estrema Insta360 Luna Ultra: la potenza del sensore da 1...
Marvel's Wolverine, la recensione: Logan torna protagonista in un'avventura brutale e intensa Marvel's Wolverine, la recensione: Logan torna p...
DJI Romo 2: tante novità lo rendono un robot completo DJI Romo 2: tante novità lo rendono un ro...
Sony Bravia 9 II: il True RGB alla prova, dove l'LCD sfida l'OLED Sony Bravia 9 II: il True RGB alla prova, dove l...
Space Pioneer starebbe realizzando un pr...
SpaceX ritirerà ''a breve'' il Fa...
La Luna ha un nuovo cratere di grandi di...
ROG Cronox: ASUS alza l'asticella dei ca...
Lenovo annuncia nuovi sistemi per la vir...
M6 e M5 Ultra, i primi benchmark conferm...
La nuova falla della cybersecurity &egra...
The Blood of Dawnwalker, il sequel potre...
Colpiti i data center Amazon: irrecupera...
Google Wallet, nuova interfaccia in arri...
iPhone Duo, problemi di produzione per i...
Firefox diventa più veloce con PDF e JPE...
WhatsApp, arriva su iOS la nuova scorcia...
Snap presenta Specs: realtà aumentata, c...
SteamOS verso un cambiamento epocale: ar...
Chromium
GPU-Z
OCCT
LibreOffice Portable
Opera One Portable
Opera One 106
CCleaner Portable
CCleaner Standard
Cpu-Z
Driver NVIDIA GeForce 546.65 WHQL
SmartFTP
Trillian
Google Chrome Portable
Google Chrome 120
VirtualBox
Tutti gli articoli Tutte le news Tutti i download

Strumenti

Regole
Non Puoi aprire nuove discussioni
Non Puoi rispondere ai messaggi
Non Puoi allegare file
Non Puoi modificare i tuoi messaggi

Il codice vB è On
Le Faccine sono On
Il codice [IMG] è On
Il codice HTML è Off
Vai al Forum


Tutti gli orari sono GMT +1. Ora sono le: 23:45.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Served by www3v