I'm looking at a book called 'Data Structure (Cheonin Gukje)', which is easy to learn with C.

In the tree chapter, there is an example of a mathematical tree calculation program.

It's as follows

```
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int data;
struct TreeNode *left, *right;
}TreeNode;
TreeNode n1 = { 1,NULL,NULL };
TreeNode n2 = { 4,NULL,NULL };
TreeNode n3 = { '*',&n1,&n2 };
TreeNode n4 = { 16,NULL,NULL };
TreeNode n5 = { 25,NULL,NULL };
TreeNode n6 = { '+',&n4,&n5 };
TreeNode n7 = { '+',&n3,&n6 };
TreeNode *exp = &n7;
// // Expression Evaluation Function
int evaluate(TreeNode *root)
{
if (root == NULL)
return 0;
if (root->left == NULL && root->right == NULL)
return root->data;
else {
int op1 = evaluate(root->left);
int op2 = evaluate(root->right);
printf("calculate %d %c %d.\n", op1, root->data, op2);
switch (root->data) {
case '+' :
return op1 + op2;
case '-':
return op1 - op2;
case '*':
return op1 * op2;
case '/':
return op1 / op2;
}
}
return 0;
}
int main(void)
{
printf("Evaluation value is %d.\n", evaluate(exp));
return 0;
}
```

I don't understand the *return 0;* processing indicated above.

If it was to create an exception, I probably made an exception handling block separately, but why did I write return 0; at the end... I have a question.

Does anyone have any idea what he meant?

Please give us your opinion!

:)

data-structure c

2022-09-20 16:22

The question says return 0 with the asterisk, but I can't see the asterisk in the question. Do you mean return 0 in the last line of the evaluate function?

The return 0 at the end of the evaluate function means to return 0 when all of the above conditions are not established (i.e., incorrect input).

For example, if root->data is not one of +-*/.

2022-09-20 16:22

Popular Tags

Popular Questions

© 2022 pinfo. All rights reserved.