This commit is contained in:
Deinerovych 2024-11-08 14:33:17 +01:00
parent 79d2450671
commit 8d2235827e

View File

@ -41,11 +41,24 @@ Node* parse_knowledge_base(char lines[][MAX_LINE_LENGTH], int *index, int line_c
return node; return node;
} }
int validate_tree(Node* node) { int validate_tree_structure(char lines[][MAX_LINE_LENGTH], int line_count) {
if (!node) return 0; int leaf_count = 0;
if ((!node->yes && node->no) || (node->yes && !node->no)) return 0; int question_count = 0;
if (!node->yes && !node->no) return 1;
return validate_tree(node->yes) && validate_tree(node->no); 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) { int count_products(Node *node) {
@ -108,7 +121,7 @@ int main() {
line_count++; line_count++;
} }
if (line_count == 0) { if (line_count == 0 || !validate_tree_structure(lines, line_count)) {
printf("Expert z bufetu to vie.\n"); printf("Expert z bufetu to vie.\n");
printf("Chybna databaza\n"); printf("Chybna databaza\n");
return 0; return 0;
@ -117,10 +130,9 @@ int main() {
int index = 0; int index = 0;
Node *root = parse_knowledge_base(lines, &index, line_count); Node *root = parse_knowledge_base(lines, &index, line_count);
if (!root || !validate_tree(root)) { if (!root) {
printf("Expert z bufetu to vie.\n"); printf("Expert z bufetu to vie.\n");
printf("Chybna databaza\n"); printf("Chybna databaza\n");
free_tree(root);
return 0; return 0;
} }