Initializacia
This commit is contained in:
parent
b7e46be38d
commit
27c63a26ef
@ -10,39 +10,35 @@ struct tree {
|
|||||||
struct tree* right;
|
struct tree* right;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Function to read a single node from input
|
|
||||||
struct tree* read_tree() {
|
struct tree* read_tree() {
|
||||||
char buffer[SIZE];
|
char buffer[SIZE];
|
||||||
if (fgets(buffer, SIZE, stdin) == NULL || buffer[0] == '\n') {
|
if (fgets(buffer, SIZE, stdin) == NULL || buffer[0] == '\n') {
|
||||||
return NULL; // Return NULL for empty input
|
return NULL;
|
||||||
}
|
}
|
||||||
buffer[strcspn(buffer, "\n")] = 0; // Remove newline character
|
buffer[strcspn(buffer, "\n")] = 0;
|
||||||
struct tree* node = calloc(1, sizeof(struct tree));
|
struct tree* node = calloc(1, sizeof(struct tree));
|
||||||
strncpy(node->value, buffer, SIZE - 1);
|
strncpy(node->value, buffer, SIZE - 1);
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to load the tree structure from input
|
|
||||||
struct tree* load_tree() {
|
struct tree* load_tree() {
|
||||||
struct tree* node = read_tree();
|
struct tree* node = read_tree();
|
||||||
if (!node) {
|
if (!node) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (node->value[0] != '*') { // Not an answer, so load children
|
if (node->value[0] != '*') {
|
||||||
node->left = load_tree();
|
node->left = load_tree();
|
||||||
node->right = load_tree();
|
node->right = load_tree();
|
||||||
}
|
}
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to run the tree and interact with the user
|
|
||||||
void run_tree(struct tree* tree) {
|
void run_tree(struct tree* tree) {
|
||||||
if (!tree) {
|
if (!tree) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tree->value[0] == '*') {
|
if (tree->value[0] == '*') {
|
||||||
// If it's an answer
|
|
||||||
printf("*%s\nKoniec\n", tree->value + 1);
|
printf("*%s\nKoniec\n", tree->value + 1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -52,13 +48,11 @@ void run_tree(struct tree* tree) {
|
|||||||
|
|
||||||
char response;
|
char response;
|
||||||
while (1) {
|
while (1) {
|
||||||
// Wait for user input
|
|
||||||
if (scanf(" %c", &response) != 1 || (response != 'a' && response != 'n')) {
|
if (scanf(" %c", &response) != 1 || (response != 'a' && response != 'n')) {
|
||||||
printf("Nerozumiem\n");
|
printf("Nerozumiem\n");
|
||||||
// Re-prompt for input
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
break; // Valid input received
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response == 'a') {
|
if (response == 'a') {
|
||||||
@ -68,7 +62,6 @@ void run_tree(struct tree* tree) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to free the memory allocated for the tree
|
|
||||||
void destroy_tree(struct tree* tree) {
|
void destroy_tree(struct tree* tree) {
|
||||||
if (tree) {
|
if (tree) {
|
||||||
destroy_tree(tree->left);
|
destroy_tree(tree->left);
|
||||||
@ -77,11 +70,10 @@ void destroy_tree(struct tree* tree) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to count the number of unique items (answers) in the tree
|
|
||||||
void count_items(struct tree* tree, int* count) {
|
void count_items(struct tree* tree, int* count) {
|
||||||
if (tree) {
|
if (tree) {
|
||||||
if (tree->left == NULL && tree->right == NULL) {
|
if (tree->left == NULL && tree->right == NULL) {
|
||||||
(*count)++; // It's a leaf node (an answer)
|
(*count)++;
|
||||||
} else {
|
} else {
|
||||||
count_items(tree->left, count);
|
count_items(tree->left, count);
|
||||||
count_items(tree->right, count);
|
count_items(tree->right, count);
|
||||||
|
Loading…
Reference in New Issue
Block a user