From 79d2450671f1904306c9278c360fb2f1cbdef3e3 Mon Sep 17 00:00:00 2001 From: Deinerovych Date: Fri, 8 Nov 2024 14:24:26 +0100 Subject: [PATCH] hz8 --- cv7/program.c | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index dad6d19..bbd6083 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -41,24 +41,11 @@ Node* parse_knowledge_base(char lines[][MAX_LINE_LENGTH], int *index, int line_c return node; } -int validate_tree_structure(char lines[][MAX_LINE_LENGTH], int line_count) { - int leaf_count = 0; - int question_count = 0; - - for (int i = 0; i < line_count; i++) { - if (lines[i][0] == '*') { - leaf_count++; - } else { - question_count++; - if (i + 2 >= line_count || lines[i + 1][0] == '*' && lines[i + 2][0] == '*') { - continue; - } else if (i + 2 < line_count && lines[i + 2][0] != '*') { - return 0; // Обнаружена некорректная структура (более двух ответов) - } - } - } - - return question_count >= leaf_count; +int validate_tree(Node* node) { + if (!node) return 0; + if ((!node->yes && node->no) || (node->yes && !node->no)) return 0; + if (!node->yes && !node->no) return 1; + return validate_tree(node->yes) && validate_tree(node->no); } int count_products(Node *node) { @@ -121,7 +108,7 @@ int main() { line_count++; } - if (line_count == 0 || !validate_tree_structure(lines, line_count)) { + if (line_count == 0) { printf("Expert z bufetu to vie.\n"); printf("Chybna databaza\n"); return 0; @@ -130,9 +117,10 @@ int main() { int index = 0; Node *root = parse_knowledge_base(lines, &index, line_count); - if (!root) { + if (!root || !validate_tree(root)) { printf("Expert z bufetu to vie.\n"); printf("Chybna databaza\n"); + free_tree(root); return 0; }