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
/
parte3.c
< prev
next >
Wrap
C/C++ Source or Header
|
1999-10-10
|
2KB
|
100 lines
/* Esame 20 settembre '99 - parte 3 */
#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 Union1(tree t1, tree *t2)
{
if (t1!=empty())
{
insert(t1->el,t2);
Union1(t1->left,t2);
Union1(t1->right,t2);
}
}
void Union(tree t1, tree t2, tree * t3)
{ Union1(t1,t3);
Union1(t2,t3);
}
int main(void)
{
char fname[MAX];
tree t1=empty(),t2=empty();
tree t3 = 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,&t1);
printf("Tree t1 from file %s: ",fname);
print(t1);
printf("\n");
while (fscanf(fd,"%d",&i)==1 && i !=-1)
insert(i,&t2);
printf("Tree t2 from file %s: ",fname);
print(t2);
printf("\n");
Union(t1,t2,&t3);
printf("Union: ");
print(t3);
printf("\n");
fclose(fd);
}
else
printf("Error opening file %s\n",fname);
}