|
#include <stdio.h>
#include <stdlib.h>
#define N 10
typedef struct Tree {
int data;
struct Tree *right;
struct Tree *left;
} Tree;
void print(Tree *oya);
Tree *dainyu(Tree *oya, int i, int a[]);
Tree *alloc(int data, Tree *left, Tree *right);
int main(void)
{
static int a[N] = {1,2,3,4,5,6,7,8,9,10};
int i;
Tree *oya = alloc(0, NULL, NULL);
for (i = 0; i < N - 1; i++)
oya = dainyu(oya, i, a);
print(oya);
return 0;
}
Tree *alloc(int data, Tree *left, Tree *right)
{
Tree *oya = malloc(sizeof(Tree));
if (oya == NULL) puts("out of memory"), exit(1);
oya->data = data;
oya->right = right;
oya->left = left;
return oya;
}
Tree *dainyu(Tree *oya, int i, int a[])
{
oya->data = a[i];
return alloc(0, oya, alloc(a[i+1], NULL, NULL));
}
void print(Tree *oya)
{
if (oya->left != NULL) print(oya->left);
if (oya->right != NULL) print(oya->right);
printf(" %d", oya->data);
}
|