Initializacia

This commit is contained in:
Kozar 2024-11-12 12:56:52 +00:00
parent ee9e324e9f
commit cf685571c5

View File

@ -13,21 +13,24 @@ struct tree {
struct tree* read_tree() {
char buffer[SIZE];
if (!fgets(buffer, SIZE, stdin)) {
return NULL;
while (fgets(buffer, SIZE, stdin)) {
buffer[strcspn(buffer, "\n")] = 0;
if (buffer[0] == '\0') {
continue;
}
struct tree* node = calloc(1, sizeof(struct tree));
strncpy(node->value, buffer, SIZE - 1);
return node;
}
buffer[strcspn(buffer, "\n")] = 0;
if (buffer[0] == '\0') {
return NULL;
}
struct tree* node = calloc(1, sizeof(struct tree));
strncpy(node->value, buffer, SIZE - 1);
return node;
return NULL;
}
struct tree* load_tree() {
struct tree* node = read_tree();
if (!node) {
@ -38,14 +41,7 @@ struct tree* load_tree() {
node->left = load_tree();
node->right = load_tree();
if (!node->left || !node->right) {
destroy_tree(node);
return NULL;
}
} else {
struct tree* extra = read_tree();
if (extra) {
free(extra);
if (!node->left || !node->right) {
destroy_tree(node);
return NULL;
}
@ -57,6 +53,7 @@ struct tree* load_tree() {
void run_tree(struct tree* tree) {
if (!tree) {
return;