From 64638d2367e894769f84436a22580fb93ff86d3f Mon Sep 17 00:00:00 2001 From: Maryna Kravtsova Date: Sun, 22 Nov 2020 15:27:57 +0100 Subject: [PATCH] buffet --- cv8/program.c | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/cv8/program.c b/cv8/program.c index bbc153a..02a5b6d 100644 --- a/cv8/program.c +++ b/cv8/program.c @@ -63,11 +63,41 @@ void print_tree(struct tree* tree,int offset){ } } -void destroy_tree(struct tree* root){ +struct tree* find_minimum(struct tree *root){ + if(root == NULL){ + return NULL; + } + else if(root->left != NULL){ + return find_minimum(root->left); + } + return root; +} + + +struct tree* destroy_tree(struct tree* root){ if(root == NULL) return; - - destroy_tree(root->left); - destroy_tree(root->right); + + if(root->left == NULL && root->right == NULL){ + free(root); + return NULL; + } + else if(root->left == NULL || root->right == NULL){ + struct tree* temp; + if(root->left == NULL){ + temp = root->right; + }else{ + temp = root->left; + } + free(root); + return temp; + } + else { + struct tree* this = find_minimum(root->left); + strcpy(root->value, this->value); + root->right = destroy_tree(root->right); + } + root->left = destroy_tree(root->left); + root->right = destroy_tree(root->right); free(root); root = NULL; } @@ -111,8 +141,7 @@ int main(){ 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); - //tree = search(tree); - + tree = search(tree); destroy_tree(tree); printf("Koniec\n"); return 0;