usaa21/cv8/program.c

111 lines
2.3 KiB
C
Raw Normal View History

2021-11-25 01:36:11 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define SIZE 100
2021-11-26 01:37:17 +00:00
2021-11-25 21:58:01 +00:00
struct node {
2021-11-25 01:36:11 +00:00
// Otázka aleo odpoveď
2021-11-25 21:58:01 +00:00
char data[SIZE];
2021-11-25 01:36:11 +00:00
// Odpoveď áno
2021-11-25 21:58:01 +00:00
struct node* left;
2021-11-25 01:36:11 +00:00
// Odpoveď nie
2021-11-25 21:58:01 +00:00
struct node* right;
};
2021-11-25 01:36:11 +00:00
2021-11-25 21:58:01 +00:00
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;
2021-11-25 01:36:11 +00:00
}
2021-11-25 21:58:01 +00:00
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;
2021-11-25 01:36:11 +00:00
}
2021-11-25 21:58:01 +00:00
void remove_tree(struct node* node){
2021-11-25 01:36:11 +00:00
if ( node != NULL ){
2021-11-25 21:58:01 +00:00
remove_tree(node->left);
remove_tree(node->right);
free(node);
2021-11-25 01:36:11 +00:00
}
2021-11-25 21:58:01 +00:00
}
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);
}
}
2021-11-26 01:37:17 +00:00
void printTabs(int numtabs){
for(int i=0; i<numtabs; i++){
printf("\t");
}
}
void printTree(struct node* root, int level){
if(root == NULL) {
printTabs(level);
printf("empty\n");
return;
}
printTabs(level);
printf("hodnota %s\n", root->data);
printTabs(level);
printf("vlavo \n");
printTree(root->left, level+1);
printTabs(level);
printf("vpravo \n");
printTree(root->right, level+1);
printTabs(level);
printf("hotovo \n");
}
2021-11-25 01:36:11 +00:00
2021-11-26 01:37:17 +00:00
void printtree(struct node* root){
printTree(root, 0);
}
2021-11-25 01:36:11 +00:00
int main(){
2021-11-25 21:58:01 +00:00
struct node* tree = NULL;
2021-11-26 01:37:17 +00:00
2021-11-25 21:58:01 +00:00
char buff[SIZE];
char* r;
int counter=0;
int lists=0;
while(1){
r = fgets(buff, SIZE, stdin);
2021-11-26 01:37:17 +00:00
if(buff[0] == '\n' || r == NULL){
2021-11-25 21:58:01 +00:00
break;
}
2021-11-26 01:37:17 +00:00
2021-11-25 21:58:01 +00:00
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.");
2021-11-26 01:37:17 +00:00
printf("counter %d, lists %d\n", counter, lists);
2021-11-25 21:58:01 +00:00
//printt_tree(tree, 1);
2021-11-26 01:37:17 +00:00
printtree(tree);
2021-11-25 21:58:01 +00:00
remove_tree(tree);
2021-11-25 01:36:11 +00:00
return 0;
}