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.99
/
parte2.c
< prev
next >
Wrap
C/C++ Source or Header
|
1999-10-10
|
1KB
|
87 lines
/* Esame 20 settembre '99 - parte 2 */
#include <stdio.h>
#include <stdlib.h>
#define MAX 30
typedef struct node *tree;
struct node
{
int el;
tree left,right;
};
tree empty()
{
return NULL;
}
void insert(int i, tree *t)
{
if (*t==empty()) /* caso base: albero vuoto */
{
*t=(tree) malloc(sizeof(struct node));
(*t)->el=i;
(*t)->left=empty();
(*t)->right=empty();
}
else if (i<(*t)->el) /* passo induttivo, sottoalbero sinistro */
insert(i,&((*t)->left));
else if (i>(*t)->el) /* passo induttivo, sottoalbero destro */
insert(i,&((*t)->right));
/* nota bene: nel caso i==(*t)->el non fa nulla! */
}
void print(tree t)
{
if (t!=empty()) /* se l'albero e` vuoto non stampo nulla */
{
print(t->left);
printf(" %d",t->el);
print(t->right);
}
}
void dispose(tree *t)
{
if (*t!=empty())
{
dispose(&((*t)->left));
dispose(&((*t)->right));
free(*t);
*t=empty();
}
}
int main(void)
{
char fname[MAX];
tree t=empty();
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");
dispose(&t);
printf("Now is deleted: ");
print(t);
printf("\n");
}
else
printf("Error opening file %s\n",fname);
fclose(fd);
}