#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct info {
int day, month, year;
};
struct info info_set(int day, int month, int year){
struct info inf;
inf.day = day;
inf.month = month;
inf.year = year;
return inf;
}
struct der {
struct info inf;
int n;
struct der* l;
struct der* r;
};
int der_add(struct der** tr, struct info inf, int n);
void der_print(FILE* hout, struct der* tr);
void der_clear(struct der* tr);
int main(void){
int i;
struct der* t = NULL;
for(i = 0; i < 20; ++i)
der_add(&t, info_set(rand()%29, rand()%12, 2012+(rand()%3)), rand()%50);
der_print(stdout, t);
der_clear(t);
return 0;
}
//добавление
int der_add(struct der** tr, struct info inf, int n){
struct der* p = *tr;
while(p != NULL){
if(n < p->n) {
tr = &p->l;
p = p->l;
} else if(n > p->n){
tr = &p->r;
p = p->r;
} else
return 0;
}
p = (struct der*)malloc(sizeof(struct der));
if(p != NULL){
p->inf = inf;
p->n = n;
p->l = p->r = NULL;
*tr = p;
}
return (p != NULL);
}
//печать
void der_print(FILE* hout, struct der* tr){
if(tr != NULL){
if(tr->l != NULL)
der_print(hout, tr->l);
fprintf(hout, "n: %d\tdate: %02d.%02d.%d\n",
tr->n, tr->inf.day, tr->inf.month, tr->inf.year);
if(tr->r != NULL)
der_print(hout, tr->r);
}
}
//чистка
void der_clear(struct der* tr){
if(tr != NULL){
if(tr->l != NULL)
der_clear(tr->l);
if(tr->r != NULL)
der_clear(tr->r);
free(tr);
}
}
Проверка кода:
http://ideone.com/VGufA1