internal double rEval(Node node)
 {
     if ((node.GetType().Equals(typeof(VariableNode))))
     {
         return (node as VariableNode).Value;
     }
     else if ((node.GetType().Equals(typeof(NumNode))))
     {
         return (node as NumNode).Value;
     }
     else if ((node.GetType().Equals(typeof(OperatorNode))))
     {
         if ((node as OperatorNode).value == "+")
             return rEval((node as OperatorNode).left) + rEval((node as OperatorNode).right);
         else if ((node as OperatorNode).value == "-")
             return rEval((node as OperatorNode).left) - rEval((node as OperatorNode).right);
         else if ((node as OperatorNode).value == "/")
             return rEval((node as OperatorNode).left) / rEval((node as OperatorNode).right);
         else if ((node as OperatorNode).value == "*")
             return rEval((node as OperatorNode).left) * rEval((node as OperatorNode).right);
         else
             return 0;
     }
     else
         return 0;
 }
Beispiel #2
0
        //travels through a node and evaluates the tree
        //NOTE:
        //this version CAN take multiple expressions,
        //But does not take into account precedence of operators.
        double evaluate(Node root)
        {
            double evaluation = 0;

            if(root is opNode)
            {
                opNode myOp = (opNode)root;

                double lv = evaluate(myOp.Left);

                double rv = evaluate(myOp.Right);

                if(myOp.Data == "+")
                {
                    evaluation = lv + rv;
                }
                else if (myOp.Data == "-")
                {
                    evaluation = lv - rv;
                }
                else if (myOp.Data == "/")
                {
                    evaluation = lv / rv;
                }
                else if (myOp.Data == "*")
                {
                    evaluation = lv * rv;
                }
            }

            else if(root is constNode)
            {
                constNode myConst = (constNode)root;
                evaluation = myConst.Value;
            }

            else if(root is varNode)
            {
                 varNode myVar = (varNode)root;
                 evaluation = myVar.Value;
            }

            return evaluation;
        }
Beispiel #3
0
        void printTree(Node root)
        {
            if(root is opNode)
            {
                opNode myRoot = (opNode)root;

                if (myRoot.Left != null)
                {
                    Console.Write("(");
                    printTree(myRoot.Left);
                }

                Console.WriteLine(myRoot.Data);

                if(myRoot.Right != null)
                {
                    printTree(myRoot.Right);
                    Console.Write(")");
                }
            }

            else
            {
                Console.Write(" " + root.Data + " " );
            }
        }
        private void BuildTree()
        {
            string[] infix = Regex.Split(expression, @"([a-zA-Z][a-zA-Z0-9_]*)|([0-9]+)|([+-/*])|[^\s*]$"); // split expression into parts
            List<string> postfix = postfixer(infix);
            Stack<Node> nodeStack = new Stack<Node>();

            for (int i = 0; i < postfix.Count(); i++)
            {
                double value;
                if (Double.TryParse(postfix[i], out value)) // if is double
                {
                    NumNode newNode = new NumNode(postfix[i]);
                    nodeStack.Push(newNode);
                }
                else if (is_operator(postfix[i])) // Operator
                {
                    OperatorNode newNode = new OperatorNode(postfix[i]);
                    newNode.right = nodeStack.Pop();
                    newNode.left = nodeStack.Pop();
                    nodeStack.Push(newNode);
                }
                else // Variable
                {
                    VariableNode newNode = new VariableNode(postfix[i]);
                    nodeStack.Push(newNode);
                }
            }
            root = nodeStack.Pop();
        }
 // Implement this constructor to construct the tree from the specific expression
 public ExpTree(string exp)
 {
     root = null;
     expression = exp;
     BuildTree();
 }
 // Implement this constructor to construct the tree from the default expression
 public ExpTree()
 {
     root = null;
     expression = "A1-12-C1";
     BuildTree();
 }
 internal OperatorNode(string data)
     : base(data)
 {
     left = null;
     right = null;
 }
 private bool Search(Node next, string varName, double varValue)
 {
     if ((next.GetType().Equals(typeof(VariableNode))) && (next as VariableNode).value == varName)
     {
         (next as VariableNode).Value = varValue;
         return true;
     }
     else if ((next.GetType().Equals(typeof(OperatorNode))))
     {
         Search((next as OperatorNode).left, varName, varValue);
         Search((next as OperatorNode).right, varName, varValue);
     }
     else
     {
         return false;
     }
     return false;
 }