From 1fc66d2e645079087e2b810e45146c87577c60af Mon Sep 17 00:00:00 2001 From: Maryna Kravtsova Date: Sun, 22 Nov 2020 14:59:10 +0100 Subject: [PATCH] buffet --- cv8/program.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/cv8/program.c b/cv8/program.c index 21a57e2..4a62ade 100644 --- a/cv8/program.c +++ b/cv8/program.c @@ -37,21 +37,32 @@ struct tree* read_tree(){ struct tree* search(struct tree* this){ int c = getchar(); - - if(this->left != NULL && this->right != NULL){ + if(this == NULL || this->value[0] == '*'){ + return this; + } + if(c == 'a'){ - //this->left = search(this->left); printf("%s\n", this->left->value); - this->left = search(this->left); + return search(this->left); } else if(c == 'n'){ - //this->right = search(this->right); printf("%s\n", this->right->value); - this->right = search(this->right); + return search(this->right); } - } } - + +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); + } +} + void destroy_tree(struct tree* root){ if(root == NULL) return; @@ -95,11 +106,11 @@ int main(){ struct tree* tree = NULL; tree = read_tree(); int count = count_leaves(tree); - + //print_tree(tree, 3); printf("Expert z bufetu to vie.\n"); printf("Pozna %d druhov ovocia a zeleniny.\n", count); printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); - printf("%s\n", tree->value); + //printf("%s\n", tree->value); tree = search(tree); //destroy_tree(tree); printf("Koniec\n");