/// <summary> /// Function to evaluate the value of the expression tree. /// </summary> /// <param name="root"> Expression tree root.</param> /// <returns> evaluated value of tree.</returns> private double Evaluate(ExpressionTreeNode root) { ConstantNode constantNode = root as ConstantNode; // will return null if node isn't constantNode.// if (constantNode != null) { return(constantNode.Value); } VariableNode variableNode = root as VariableNode; if (variableNode != null) { return(variableNode.Evaluate()); } OperatorNode operatorNode = root as OperatorNode; if (operatorNode != null) { return(operatorNode.Evaluate()); } throw new NotSupportedException(); }
/// <summary> /// Initializes a new instance of the <see cref="ExpressionTree"/> class. /// </summary> /// <param name="expression"> Denotes the expression to be converted.</param> public ExpressionTree(string expression) { this.root = this.Compile(expression); this.Expression = expression; }
/// <summary> /// Initializes a new instance of the <see cref="ExpressionTree"/> class. /// Default ExpretionTree. /// </summary> /// <param name="expression">the infixexpresstion to be evaluated.</param> public ExpressionTree(string expression) { this.root = this.BuildTree(expression); }