Dear all,
I have the following code. I constructed an expression tree and I am stuck parsing it to find the result
You will find the details within my code
public enum OpertaionType { add, sub, div, mul}
public class Node {
public Node(Node lhs, Node rhs, OpertaionType opType, int index) {
this.lhs = lhs;
this.rhs = rhs;
this.opType = opType;
this.index = index;
}
Node lhs;
Node rhs;
OpertaionType opType;
int index;
}
class Program
{
static void Main(string[] args)
{
// I don't want this to be part of the node data structure
// because in the actual implementation I will end up referencing an
// array of data
int[] value = new int[5];
Node[] nodes = new Node[value.Length];
for (int i = 0; i < value.Length; i++)
{
value[i] = i+1;
nodes[i] = new Node(null, null, 0, i);
}
// suppose I constructed the tree like that
// note that the end nodes are marked by non-negative index that indexes the
// values array
Node a = new Node(nodes[0], nodes[1], OpertaionType.add, -1);
Node b = new Node(nodes[2], a, OpertaionType.mul, -1);
Node c = new Node(b, nodes[3], OpertaionType.div, -1);
// How can I find the result of Node c programatically
// such that the result is (a[2]*(a[0]+a[1]))/a[3] = 9/4
}
}