second commit

This commit is contained in:
Valér Jakubčo 2021-11-25 22:58:01 +01:00
parent b8fa664a33
commit 07e5f2eb4a

View File

@ -4,78 +4,91 @@
#include <assert.h> #include <assert.h>
#define SIZE 100 #define SIZE 100
typedef struct tree { struct node {
// Otázka aleo odpoveď // Otázka aleo odpoveď
char value[SIZE]; char data[SIZE];
// Odpoveď áno // Odpoveď áno
struct tree* left; struct node* left;
// Odpoveď nie // Odpoveď nie
struct tree* right; struct node* right;
} tree; };
void print_tree(struct tree* node){
if( node == NULL){
printf("empty");
return;
}
printf("hodnota: %s\n",node->value);
printf("left ");
printf("%s\n",node->left->value);
printf("right ");
printf("%s\n",node->right->value);
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 tree* read_tree(tree* node){
char buffer[SIZE]; struct node* insert(struct node* root, char data[SIZE]){
memset(buffer,0, SIZE); if(root == NULL){
char* r = fgets(buffer, SIZE, stdin); root = getNewNode(data);
if( r == NULL ){ return root;
return node;
}
memcpy(node->value, buffer, SIZE);
r = fgets(buffer, SIZE, stdin);
if( r == NULL ){
node->left = NULL;
return node;
}
node->left = calloc(1, sizeof(struct tree));
memcpy(node->left->value, buffer, SIZE);
r = fgets(buffer, SIZE, stdin);
if( r == NULL ){
node->right = NULL;
return node;
}
node->right = calloc(1, sizeof(struct tree));
memcpy(node->right->value, buffer, SIZE);
read_tree(node);
print_tree(node);
free(node);
return node;
} }
void guess(tree* node){ if(root->left == NULL){
root->left = insert(root->left,data);
}else{
root->right = insert(root->right,data);
} }
/*void remove_tree(struct tree* node){ return root;
}
void remove_tree(struct node* node){
if ( node != NULL ){ if ( node != NULL ){
if( node->left != NULL){
remove_tree(node->left); remove_tree(node->left);
free(node);
}
if( node->right != NULL){
remove_tree(node->right); remove_tree(node->right);
free(node); 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(){ int main(){
tree* node = calloc(1, sizeof(struct tree)); struct node* tree = NULL;
read_tree(node); char buff[SIZE];
//remove_tree(node); 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; return 0;
} }