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.98
/
parte1.c
next >
Wrap
C/C++ Source or Header
|
1999-03-11
|
1KB
|
67 lines
/* Esame 14 settembre '98 - parte 1 */
#include <stdio.h>
#include <stdlib.h>
#define MAX 30
typedef struct node *tree;
struct node
{
int el;
tree left,right;
};
void insert(int,tree*);
void print(tree);
void insert(int i, tree *t)
{
if (*t==NULL) /* caso base: albero vuoto */
{
*t=(tree) malloc(sizeof(struct node));
(*t)->el=i;
(*t)->left=NULL;
(*t)->right=NULL;
}
else if (i<=(*t)->el) /* passo induttivo, sottoalbero sinistro */
insert(i,&((*t)->left));
else /* passo induttivo, sottoalbero destro (i>(*t)->el) */
insert(i,&((*t)->right));
}
void print(tree t)
{
if (t!=NULL) /* se l'albero e` vuoto non stampa nulla */
{
print(t->left);
printf(" %d",t->el);
print(t->right);
}
}
int main(void)
{
char fname[MAX];
tree t=NULL;
FILE *fd=NULL;
int i;
printf("Enter file name: ");
scanf("%s",fname);
if (fd=fopen(fname,"r"))
{
while (fscanf(fd,"%d",&i)==1 && i!=-1)
insert(i,&t);
printf("Tree from file %s: ",fname);
print(t);
printf("\n");
fclose(fd);
}
else
printf("Error opening file %s\n",fname);
}