codice:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
struct Infof
{
char nome[12];
char telefono[10];
};
struct albero
{
struct Infof info;
struct albero *left;
struct albero *right;
};
typedef struct albero bst;
bst *crea_nodo(char *name)
{
bst *nuovo = (bst *)malloc(sizeof(bst));
puts(name);
strcpy(nuovo->info.nome, name);
printf("Inserisci numero di telefono: ");
scanf("%s", nuovo->info.telefono);
nuovo->left = NULL;
nuovo->right = NULL;
return nuovo;
}
bst *insert(bst *padre, char *nom)
{
if(padre == NULL)
{
return crea_nodo(nom);
}
if(strcmp(padre->info.nome, nom) <= 0)
{
padre->left = insert(padre->left, nom);
return padre;
}
else //if (strcmp(padre->info.nome, nom) > 0)
{
padre->right = insert(padre->right, nom);
return padre;
}
}
void preorder(bst *nodo)
{
if(nodo != NULL)
{
printf("Nome: %s\n", nodo->info.nome);
printf("Numero di telefono: %s\n", nodo->info.telefono);
preorder(nodo->left);
preorder(nodo->right);
}
}
int main()
{
bst *head = NULL;
char nome[12];
printf("Inserisci nome: ");
gets(nome);
head = insert(head, nome);
short int grado;
printf("Inserisci il grado della radice: ");
scanf("%hi", &grado);
grado = pow(2,grado)-2;
short int i;
for(i = 0; i < grado; i++)
{
printf("Inserisci nome: ");
scanf("%s", nome);
insert(head, nome);
}
preorder(head);
return 0;
}