diff --git a/cv7/program.c b/cv7/program.c index bbd6083..959c129 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -41,11 +41,24 @@ Node* parse_knowledge_base(char lines[][MAX_LINE_LENGTH], int *index, int line_c return node; } -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 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] == '*') { + i += 2; // Skip the two leaves as expected + } else if (i + 1 < line_count && lines[i + 1][0] != '*') { + return 0; // Обнаружена некорректная структура (более двух дочерних узлов у вопроса) + } + } + } + + return question_count >= leaf_count; } int count_products(Node *node) { @@ -108,7 +121,7 @@ int main() { line_count++; } - if (line_count == 0) { + if (line_count == 0 || !validate_tree_structure(lines, line_count)) { printf("Expert z bufetu to vie.\n"); printf("Chybna databaza\n"); return 0; @@ -117,10 +130,9 @@ int main() { int index = 0; Node *root = parse_knowledge_base(lines, &index, line_count); - if (!root || !validate_tree(root)) { + if (!root) { printf("Expert z bufetu to vie.\n"); printf("Chybna databaza\n"); - free_tree(root); return 0; }