diff --git a/cv7/program.c b/cv7/program.c index 3c7eabe..657f883 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -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;