From 3043acee18dc02e9267bf74a662c58b3e4281f9b Mon Sep 17 00:00:00 2001 From: Yurii Yakovenko Date: Mon, 11 Nov 2024 21:30:26 +0000 Subject: [PATCH] Update cv7/program.c --- cv7/program.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index a684cc0..40dcf06 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -9,31 +9,35 @@ struct tree { - + char value[SIZE]; struct tree *left, *right; }; -struct tree* read_tree(struct tree* p) +struct tree* read_tree() { char buffer[SIZE]; memset(buffer,0,SIZE); char* r = fgets(buffer,SIZE,stdin); + assert(r); struct tree* node = (struct tree*)calloc(1,sizeof(struct tree)); memcpy(node->value,buffer,SIZE); - //printf("\n%s", node->value); + + + + node->left=NULL; node->right=NULL; if(node->value[0]!='*') { - read_tree(node->left); - - read_tree(node->right); + node->left=read_tree(node->left); + + node->right=read_tree(node->right); } return node; } @@ -43,8 +47,9 @@ void print_tree(struct tree* tree,int offset){ for (int i = 0; i < offset; i++){ printf("."); } - - printf("(%s)",tree->value); + + if(tree) + printf("%s",tree->value); if (tree->left){ print_tree(tree->left,offset +3); } @@ -59,8 +64,9 @@ int main(void) struct tree *tr; - read_tree(tr); - printf("-------"); + tr=read_tree(); + print_tree(tr,3); + return 0; } \ No newline at end of file