83 lines
1.7 KiB
C
83 lines
1.7 KiB
C
#include<stdio.h>
|
|
#include<string.h>
|
|
#include<assert.h>
|
|
#include<stdlib.h>
|
|
|
|
#define SIZE 100
|
|
|
|
struct tree{
|
|
char value[SIZE];
|
|
struct tree* left;
|
|
struct tree* right;
|
|
};
|
|
|
|
struct tree* read_tree(){
|
|
char buffer[SIZE];
|
|
memset(buffer,0,SIZE);
|
|
char* r = fgets(buffer,SIZE,stdin);
|
|
assert(r);
|
|
struct tree* node = calloc(1,sizeof(struct tree));
|
|
memcpy(node->value,buffer,SIZE);
|
|
return node;
|
|
}
|
|
|
|
struct tree* load_tree() {
|
|
struct tree* node = read_tree();
|
|
if (!node) {
|
|
return NULL;
|
|
}
|
|
if (node->value[0] != '*') {
|
|
node->left = load_tree();
|
|
node->right = load_tree();
|
|
}
|
|
return node;
|
|
}
|
|
|
|
void print_tree(struct tree* tree, int offset){
|
|
for (int i = 0; i < offset; i++){
|
|
printf(" ");
|
|
}
|
|
printf("%s", tree->value);
|
|
if (tree->left){
|
|
print_tree(tree->left, offset + 3);
|
|
print_tree(tree->right, offset + 3);
|
|
}
|
|
}
|
|
|
|
void destroy_tree(struct tree* tree){
|
|
if(tree->left != NULL){
|
|
destroy_tree(tree->left);
|
|
}
|
|
if(tree->right != NULL){
|
|
destroy_tree(tree->right);
|
|
}
|
|
free(tree);
|
|
}
|
|
|
|
void count_items(struct tree* tree, int* count){
|
|
if(tree->left == NULL && tree->right == NULL){
|
|
(*count)++;
|
|
}else{
|
|
count_items(tree->left, count);
|
|
count_items(tree->right, count);
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
struct tree* root = load_tree();
|
|
if (!root) {
|
|
printf("Chyba: Nepodarilo sa nacitat bazu pravidiel.\n");
|
|
return 1;
|
|
}
|
|
|
|
int count = 0;
|
|
count_items(root, &count);
|
|
printf("Pozna %d druhov ovocia a zeleniny.\n", count);
|
|
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
|
|
|
run_tree(root);
|
|
|
|
destroy_tree(root);
|
|
return 0;
|
|
}
|