C# assistance with parsing my own expression tree

c# expression-trees parsing


I have the code shown below. I created an expression tree, but I'm having trouble parsing it to get the outcome.

The specifics are included in 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

8/8/2009 5:21:28 PM

Accepted Answer

I'm sorry, but I'm not aware of a truly thorough and in-depth work on the topic (maybe a book..?). For a basic introduction to C# 3.0's own expression trees, see, for example, here.

You may most easily assess your own hand-rolled format via recursion; in pseudocode:

def valof(node):
  if node.index >= 0: 
    return whateverarray[node.index]
  L = valof(node.lhs)
  R = valof(node.rhs)
  if node.opType == add:
    return L + R
  if node.opType == mul:
    return L * R
  # etc etc

As an additional twist, keep in mind to use a fraction or rational number type for your calculations as you seem to desire fraction outcomes but the input values are integers. I'm not sure whether C# comes with one, but in the worst case scenario, you can find many on the internet;-).

8/8/2009 5:12:59 PM

Popular Answer

A recursive algorithm is required, passing the values array (untested code):

class Node{
  double compute(int[] values){
    if(index >= 0)
      return values[index]; // leaf node; value stored in array
      case add: return lhs.compute(values)+rhs.compute(values);
      case sub: return lhs.compute(values)-rhs.compute(values);
      case div: return lhs.compute(values)*rhs.compute(values);
      case mul: return lhs.compute(values)/rhs.compute(values);
    throw new Exception("unsupported operation type");

Keep in mind that all calculations are done in double here; a rational type would have to be used if you actually wanted 9/4.

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow