41 lines
849 B
C
41 lines
849 B
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
#define SIZE 100
|
|
|
|
struct tree {
|
|
char* value;
|
|
struct tree* left;
|
|
struct tree* right;
|
|
};
|
|
|
|
struct tree* read_tree(){
|
|
char buffer[SIZE];
|
|
memset(buffer, 0, SIZE);
|
|
char* r = fgets(buffer, SIZE, stdin);
|
|
if(buffer[0] == '\n'){
|
|
return NULL;
|
|
}
|
|
assert(r);
|
|
struct tree* node = calloc(1, sizeof(struct tree));
|
|
node->value = malloc(strlen(buffer) + 1);
|
|
assert(node->value);
|
|
node->left = read_tree();
|
|
node->right = read_tree();
|
|
return node;
|
|
}
|
|
|
|
void destroy_tree(struct tree* node) {
|
|
if (node == NULL) return;
|
|
destroy_tree(node->left);
|
|
destroy_tree(node->right);
|
|
free(node);
|
|
}
|
|
|
|
int main(){
|
|
struct tree* root = read_tree();
|
|
read_tree();
|
|
destroy_tree(root);
|
|
return 0;
|
|
} |