From bf7202663e1fbcd3545e8c38a24e792b4cb86e41 Mon Sep 17 00:00:00 2001 From: Yurii Yakovenko Date: Mon, 11 Nov 2024 22:13:27 +0000 Subject: [PATCH] Update cv7/program.c --- cv7/program.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index 50a6fff..842608c 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -8,7 +8,7 @@ struct tree { - + char value[SIZE]; struct tree *left, *right; @@ -18,20 +18,24 @@ struct tree* read_tree() { char buffer[SIZE]; memset(buffer,0,SIZE); - char* r = fgets(buffer,SIZE,stdin); - + char* r = fgets(buffer,SIZE,stdin); + + assert(r); + + if(buffer[0]=='\n') + return NULL; struct tree* node = (struct tree*)calloc(1,sizeof(struct tree)); memcpy(node->value,buffer,SIZE); - + node->left=NULL; node->right=NULL; if(node->value[0]!='*') { - + node->left=read_tree(node->left); - + node->right=read_tree(node->right); } return node; @@ -42,7 +46,7 @@ void print_tree(struct tree* tree,int offset){ for (int i = 0; i < offset; i++){ printf("."); } - + if(tree) printf("%s",tree->value); if (tree->left){ @@ -53,6 +57,13 @@ void print_tree(struct tree* tree,int offset){ } } +void destroy_tree(struct tree* tree){ + if(tree==NULL) return; + + destroy_tree(tree->left); + destroy_tree(tree->right); + free(tree); +} int main(void) { @@ -82,11 +93,10 @@ int main(void) }while(1); }while(1); - - + printf("Koniec\n"); + destroy_tree(tr); + tr=NULL; return 0; } - -