Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Memorizzare i dati immessi in un albero binario in modo che l etichetta di un
- qualsiasi nodo sia maggiore di tutti i valorei del suo sottoalbero sinistro, e
- minore di tutti quelli del suo sottoalbero destro*/
- #include <stdio.h>
- #include <malloc.h>
- struct nodo{
- int inf;
- struct nodo *left;
- struct nodo *right;
- };
- struct nodo *creaAlb(void);
- struct nodo *creaNodo(struct nodo *, int);
- void anticipato(struct nodo *);
- main()
- {
- struct nodo *radice;
- radice=creaAlb();
- printf("\nVISITA IN ORDINE ANTICIPATO\n\n");
- anticipato(radice);
- printf("\n\nPremere un tasto per uscire..");
- getchar(); getchar(); return 0;
- }
- struct nodo *creaAlb(void)
- {
- struct nodo *p=NULL;
- int x=-1;
- while(x!=0){
- printf("\nInserire elemento, 0 per uscire\t");
- scanf("%d", &x);
- if(x!=0) p=creaNodo(p, x);}
- return p;
- }
- struct nodo *creaNodo(struct nodo *p, int val)
- {
- if(p==NULL){
- p=(struct nodo*)malloc(sizeof(struct nodo));
- p->inf=val;
- p->left=NULL;
- p->right=NULL;
- }
- else{
- if(val>p->inf) p->right=creaNodo(p->right, val);
- else if(val<p->inf) p->left=creaNodo(p->left, val);
- else{
- printf("\nElemento gia' presente!\t");
- getchar();}
- }
- return p;
- }
- void anticipato(struct nodo *p)
- {
- if(p!=NULL){
- printf("%d ", p->inf);
- if(p->left!=NULL){
- printf("\n<- ");
- anticipato(p->left);}
- if(p->right!=NULL){
- printf("\n\t-> ");
- anticipato(p->right);}}
- else printf("\nAlbero vuoto!!");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement