usaa21/cv8/program.c
2021-11-25 22:58:01 +01:00

95 lines
2.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define SIZE 100
struct node {
// Otázka aleo odpoveď
char data[SIZE];
// Odpoveď áno
struct node* left;
// Odpoveď nie
struct node* right;
};
struct node* getNewNode(char data[SIZE]){
struct node* newNode = (struct node*)calloc(1, sizeof(struct node));
memcpy(newNode->data, data, SIZE);
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct node* insert(struct node* root, char data[SIZE]){
if(root == NULL){
root = getNewNode(data);
return root;
}
if(root->left == NULL){
root->left = insert(root->left,data);
}else{
root->right = insert(root->right,data);
}
return root;
}
void remove_tree(struct node* node){
if ( node != NULL ){
remove_tree(node->left);
remove_tree(node->right);
free(node);
}
}
void printt_tree(struct node* tree,int offset){
for (int i = 0; i < offset; i++){
printf(" ");
}
printf("%s",tree->data);
if (tree->left){
printt_tree(tree->left,offset+3);
printt_tree(tree->right,offset+3);
}
}
int main(){
struct node* tree = NULL;
char buff[SIZE];
char* r;
int counter=0;
int lists=0;
while(1){
r = fgets(buff, SIZE, stdin);
if(r == NULL){
break;
}
if(strncmp(buff, "*", 1)) lists++;
tree = insert(tree, buff);
counter++;
}
puts("Expert z bufetu to vie.");
printf("Pozna %d druhov ovocia a zeleniny.\n", lists);
puts("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.");
for(int i=0; i< counter; i++){
printf("%s", tree->data);
r = fgets(buff, SIZE, stdin);
if(strncmp(buff, "a", 1) && tree->right != NULL){
tree = tree->right;
continue;
}else if(strncmp(buff, "n", 1) && tree->left != NULL){
tree = tree->left;
continue;
}else {
puts("Koniec");
break;
}
}
//printt_tree(tree, 1);
remove_tree(tree);
return 0;
}