Hey everyone I'm trying to write a program in C that will evaluate and display an expression tree correctly through the use of recursion. So an expression such as : 3 + 4 * 5 - 56/7 will evaluate correctly as (3 + (4 * 5)) - (56/7). I have been writing a header class that defines all needed type definitions and functions. Here's the code in progress so far :
Code:
#ifndef TREE_NODE
#define TREE_NODE
typedef struct Node_t {
int value;
struct Node_t *left;
struct Node_t *right;
} Node, *Node_p;
typedef char *String;
char* s = "string";
typedef enum {false, true} bool;
void setNode(Node_p np,
int value,
Node_p left,
Node_p right,
bool display);
value = (value, NULL, NULL);
String nodeToString(Node_p np);
int eval(Node_p np);
#endif
This code will be used by a test class that I will develop to test and see if everything is working properly. My problem is starting off the setNode function. I'm not exactly sure as to how I go about getting started on defining each type of Node. There should be a few "types":
-one that is just a numeric value, such as a 2. This won't have a pointer to either the left node or the right node.
-one for each arithmetical function multiply and times, and with pointers to either the left node or right node, or both.
This is actually my first program written in C, I haven't been studying it for more than a month, as I've mostly worked with Java to this point so it's a bit overwhelming to have a program working with recursion and a few other elements that I'm not used to using in C. Any sort of suggestions on how I should proceed or where there is error in my thinking will be greatly appreciated. I'm just not sure how setNode should proceed, am I supposed to create a Node for each case that I may run into, or am I looking at this from the wrong point of view?
Thanks again!