third commit

This commit is contained in:
Valér Jakubčo 2021-11-26 02:37:17 +01:00
parent 07e5f2eb4a
commit 4e584924fc

View File

@ -4,6 +4,7 @@
#include <assert.h> #include <assert.h>
#define SIZE 100 #define SIZE 100
struct node { struct node {
// Otázka aleo odpoveď // Otázka aleo odpoveď
char data[SIZE]; char data[SIZE];
@ -27,7 +28,9 @@ struct node* insert(struct node* root, char data[SIZE]){
root = getNewNode(data); root = getNewNode(data);
return root; return root;
} }
if(strncmp(data, "*", 1)){
return root;
}
if(root->left == NULL){ if(root->left == NULL){
root->left = insert(root->left,data); root->left = insert(root->left,data);
}else{ }else{
@ -41,7 +44,6 @@ void remove_tree(struct node* node){
remove_tree(node->left); remove_tree(node->left);
remove_tree(node->right); remove_tree(node->right);
free(node); free(node);
} }
} }
@ -55,40 +57,57 @@ void printt_tree(struct node* tree,int offset){
printt_tree(tree->right,offset+3); printt_tree(tree->right,offset+3);
} }
} }
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");
}
void printtree(struct node* root){
printTree(root, 0);
}
int main(){ int main(){
struct node* tree = NULL; struct node* tree = NULL;
char buff[SIZE]; char buff[SIZE];
char* r; char* r;
int counter=0; int counter=0;
int lists=0; int lists=0;
while(1){ while(1){
r = fgets(buff, SIZE, stdin); r = fgets(buff, SIZE, stdin);
if(r == NULL){ if(buff[0] == '\n' || r == NULL){
break; break;
} }
if(strncmp(buff, "*", 1)) lists++;
tree = insert(tree, buff); tree = insert(tree, buff);
counter++; counter++;
} }
puts("Expert z bufetu to vie."); puts("Expert z bufetu to vie.");
printf("Pozna %d druhov ovocia a zeleniny.\n", lists); printf("Pozna %d druhov ovocia a zeleniny.\n", lists);
puts("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost."); puts("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.");
for(int i=0; i< counter; i++){
printf("%s", tree->data); printf("counter %d, lists %d\n", counter, lists);
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); //printt_tree(tree, 1);
printtree(tree);
remove_tree(tree); remove_tree(tree);
return 0; return 0;
} }