home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.disi.unige.it
/
2015-02-11.ftp.disi.unige.it.tar
/
ftp.disi.unige.it
/
pub
/
.person
/
CataniaB
/
teach-act
/
testi-esami
/
labo-9.00
/
parte3.c
< prev
next >
Wrap
C/C++ Source or Header
|
2000-09-20
|
1KB
|
98 lines
#include <stdio.h>
#include <stdlib.h>
#define MAX 30
/* TIPI */
typedef struct nodo *tree;
struct nodo
{
tree dx;
tree sx;
int info;
};
/* PROTOTIPI */
tree empty(void);
void insert( int, tree *);
void print(tree);
int leaves_number(tree);
/* FUNZIONI */
tree empty()
{
return NULL;
}
void insert(int i,tree *t)
{
if(i>=0)
{
if((*t)==empty())
{
(*t)=(tree)malloc(sizeof(struct nodo));
(*t)->info=i;
(*t)->sx=empty();
(*t)->dx=empty();
}
else
if(i<(*t)->info)
insert(i,&((*t)->sx));
else if(i>(*t)->info)
insert(i,&((*t)->dx));
}
}
int leaves_number(tree t)
{
if(t==empty())
return 0;
else if(t->sx==empty() && t->dx==empty())
return 1;
else
return(leaves_number(t->sx)+leaves_number(t->dx));
}
void print(tree t)
{
if(t!=empty())
{
print(t->sx);
printf("%d ",t->info);
print(t->dx);
}
}
/* MAIN */
void main()
{
char nome[MAX];
tree t;
int i,n;
FILE *fd=NULL;
t=empty();
printf("Dammi il nome del file:\n");
scanf("%s",nome);
if(fd=fopen(nome,"r"))
{
while(fscanf(fd,"%d",&i) && i!=-1)
insert(i,&t);
printf("%d\n",leaves_number(t));
fclose(fd);
}
else
printf("Errore di apertura del file");
}