2020-11-21 15:26:27 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
struct tree {
|
2020-11-22 11:54:57 +00:00
|
|
|
char value[50];
|
2020-11-21 15:26:27 +00:00
|
|
|
struct tree* left;
|
|
|
|
struct tree* right;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct tree* read_tree(){
|
2020-11-22 11:54:57 +00:00
|
|
|
char buffer[50];
|
|
|
|
memset(buffer,0,50);
|
|
|
|
char* r = fgets(buffer,50,stdin);
|
2020-11-21 15:26:27 +00:00
|
|
|
int x = strlen(buffer);
|
|
|
|
buffer[x-1]='\0';
|
2020-11-21 17:34:29 +00:00
|
|
|
|
2020-11-21 15:26:27 +00:00
|
|
|
assert(r);
|
2020-11-22 12:28:03 +00:00
|
|
|
|
2020-11-22 12:13:44 +00:00
|
|
|
if(buffer[0] != '\0'){
|
|
|
|
struct tree* node = calloc(1,sizeof(struct tree));
|
|
|
|
memcpy(node->value, buffer,50);
|
|
|
|
if(buffer[0] != '*'){
|
|
|
|
if(node->left == NULL){
|
|
|
|
node->left = read_tree();
|
|
|
|
}
|
|
|
|
if(node->right == NULL){
|
|
|
|
node->right = read_tree();
|
|
|
|
}
|
2020-11-22 12:28:03 +00:00
|
|
|
}
|
2020-11-21 15:26:27 +00:00
|
|
|
return node;
|
2020-11-22 12:13:44 +00:00
|
|
|
}
|
2020-11-22 12:28:03 +00:00
|
|
|
|
2020-11-21 15:26:27 +00:00
|
|
|
}
|
2020-11-22 11:54:57 +00:00
|
|
|
struct tree* search(struct tree* this){
|
|
|
|
|
2020-11-22 12:55:30 +00:00
|
|
|
int c = getchar();
|
2020-11-22 13:59:10 +00:00
|
|
|
if(this == NULL || this->value[0] == '*'){
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-11-22 11:54:57 +00:00
|
|
|
if(c == 'a'){
|
2020-11-22 13:11:47 +00:00
|
|
|
printf("%s\n", this->left->value);
|
2020-11-22 13:59:10 +00:00
|
|
|
return search(this->left);
|
2020-11-22 11:54:57 +00:00
|
|
|
}
|
2020-11-22 13:11:47 +00:00
|
|
|
else if(c == 'n'){
|
|
|
|
printf("%s\n", this->right->value);
|
2020-11-22 13:59:10 +00:00
|
|
|
return search(this->right);
|
2020-11-22 11:54:57 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-22 13:59:10 +00:00
|
|
|
|
|
|
|
void print_tree(struct tree* tree,int offset){
|
|
|
|
int i;
|
|
|
|
for (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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-22 14:27:57 +00:00
|
|
|
struct tree* find_minimum(struct tree *root){
|
|
|
|
if(root == NULL){
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if(root->left != NULL){
|
|
|
|
return find_minimum(root->left);
|
|
|
|
}
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct tree* destroy_tree(struct tree* root){
|
2020-11-21 15:26:27 +00:00
|
|
|
if(root == NULL) return;
|
2020-11-22 14:27:57 +00:00
|
|
|
|
|
|
|
if(root->left == NULL && root->right == NULL){
|
|
|
|
free(root);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if(root->left == NULL || root->right == NULL){
|
|
|
|
struct tree* temp;
|
|
|
|
if(root->left == NULL){
|
|
|
|
temp = root->right;
|
|
|
|
}else{
|
|
|
|
temp = root->left;
|
|
|
|
}
|
|
|
|
free(root);
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
struct tree* this = find_minimum(root->left);
|
|
|
|
strcpy(root->value, this->value);
|
|
|
|
root->right = destroy_tree(root->right);
|
|
|
|
}
|
|
|
|
root->left = destroy_tree(root->left);
|
|
|
|
root->right = destroy_tree(root->right);
|
2020-11-21 15:26:27 +00:00
|
|
|
free(root);
|
|
|
|
root = NULL;
|
2020-11-21 17:34:29 +00:00
|
|
|
}
|
|
|
|
int count_leaves(struct tree* node){
|
|
|
|
int count = 0;
|
|
|
|
if(node->left == NULL && node->right == NULL){
|
|
|
|
count = 1;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
if(node->left != NULL){
|
|
|
|
count += count_leaves(node->left);
|
|
|
|
}
|
|
|
|
if(node->right != NULL){
|
|
|
|
count += count_leaves(node->right);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
|
|
|
|
}
|
|
|
|
int count_all(struct tree* node){
|
2020-11-21 15:26:27 +00:00
|
|
|
if(node == NULL){
|
|
|
|
return 0;
|
|
|
|
}
|
2020-11-21 17:34:29 +00:00
|
|
|
int count = 1;
|
|
|
|
if(node->left != NULL){
|
|
|
|
count += count_all(node->left);
|
2020-11-21 15:26:27 +00:00
|
|
|
}
|
2020-11-21 17:34:29 +00:00
|
|
|
if(node->right != NULL){
|
|
|
|
count += count_all(node->right);
|
2020-11-21 15:26:27 +00:00
|
|
|
}
|
2020-11-21 17:34:29 +00:00
|
|
|
return count;
|
2020-11-21 15:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(){
|
|
|
|
struct tree* tree = NULL;
|
|
|
|
tree = read_tree();
|
2020-11-21 17:34:29 +00:00
|
|
|
int count = count_leaves(tree);
|
2020-11-22 13:59:10 +00:00
|
|
|
//print_tree(tree, 3);
|
2020-11-22 11:56:13 +00:00
|
|
|
printf("Expert z bufetu to vie.\n");
|
|
|
|
printf("Pozna %d druhov ovocia a zeleniny.\n", count);
|
2020-11-21 15:32:04 +00:00
|
|
|
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
2020-11-22 14:00:35 +00:00
|
|
|
printf("%s\n", tree->value);
|
2020-11-22 14:27:57 +00:00
|
|
|
tree = search(tree);
|
2020-11-22 14:00:35 +00:00
|
|
|
destroy_tree(tree);
|
2020-11-22 12:41:02 +00:00
|
|
|
printf("Koniec\n");
|
2020-11-21 15:26:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|