Update cv7/program.c

This commit is contained in:
Yurii Yakovenko 2024-11-11 22:13:27 +00:00
parent cba7e62977
commit bf7202663e

View File

@ -8,7 +8,7 @@
struct tree struct tree
{ {
char value[SIZE]; char value[SIZE];
struct tree *left, *right; struct tree *left, *right;
@ -18,20 +18,24 @@ struct tree* read_tree()
{ {
char buffer[SIZE]; char buffer[SIZE];
memset(buffer,0,SIZE); memset(buffer,0,SIZE);
char* r = fgets(buffer,SIZE,stdin); char* r = fgets(buffer,SIZE,stdin);
assert(r); assert(r);
if(buffer[0]=='\n')
return NULL;
struct tree* node = (struct tree*)calloc(1,sizeof(struct tree)); struct tree* node = (struct tree*)calloc(1,sizeof(struct tree));
memcpy(node->value,buffer,SIZE); memcpy(node->value,buffer,SIZE);
node->left=NULL; node->left=NULL;
node->right=NULL; node->right=NULL;
if(node->value[0]!='*') if(node->value[0]!='*')
{ {
node->left=read_tree(node->left); node->left=read_tree(node->left);
node->right=read_tree(node->right); node->right=read_tree(node->right);
} }
return node; return node;
@ -42,7 +46,7 @@ void print_tree(struct tree* tree,int offset){
for (int i = 0; i < offset; i++){ for (int i = 0; i < offset; i++){
printf("."); printf(".");
} }
if(tree) if(tree)
printf("%s",tree->value); printf("%s",tree->value);
if (tree->left){ 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) int main(void)
{ {
@ -82,11 +93,10 @@ int main(void)
}while(1); }while(1);
}while(1); }while(1);
printf("Koniec\n"); printf("Koniec\n");
destroy_tree(tr);
tr=NULL;
return 0; return 0;
} }