} // End function /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Function: recursEval * * Input: ExpNode * * Output: double * * Description: A function that recursively traverse the expresssion* * tree making the appropriate operations, and returns * * the result. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ public double recursEval(ExpNode currNode) { if (currNode == null) { return(0); } else if (variables.ContainsKey(currNode.getValue())) { return(variables[currNode.getValue()]); } else { switch (currNode.getValue()) { case "+": { return(recursEval(currNode.getLeftChild()) + recursEval(currNode.getRightChild())); } break; case "-": { return(recursEval(currNode.getLeftChild()) - recursEval(currNode.getRightChild())); } break; case "*": { return(recursEval(currNode.getLeftChild()) * recursEval(currNode.getRightChild())); } break; case "/": { return(recursEval(currNode.getLeftChild()) / recursEval(currNode.getRightChild())); } break; } return(Double.Parse(currNode.getValue())); } } // End function
} // End function /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Function: buildExpTree * * Input: string[] * * Output: void * * Description: A function makes the necessary connections between * * the nodes, and sets the root of the tree. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ public void buildExpTree(string[] postFix) { for (int i = 0; i < postFix.Length; i++) { if (postFix[i] == "+" || postFix[i] == "-" || postFix[i] == "*" || postFix[i] == "/") { ExpNode temp1 = ExpTreeStack.Pop(); ExpNode temp2 = ExpTreeStack.Pop(); ExpNode newOperatorNode = new ExpNode(postFix[i]); newOperatorNode.setLeftChild(temp2); newOperatorNode.setRightChild(temp1); ExpTreeStack.Push(newOperatorNode); } else { ExpTreeStack.Push(new ExpNode(postFix[i])); } } root = ExpTreeStack.Pop(); } // End function
} // End constructor public ExpTree() { root = null; } // End constructor
new Dictionary <string, double>(); // Dictionary for the variables // Constructor public ExpTree(string expression) { root = null; constructTree(expression); } // End constructor
} // End function /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Function: setLeftChild * * Input: ExpNode * * Output: void * * Description: A function that sets the value of the left child * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ public void setLeftChild(ExpNode left) { leftChild = left; // Sets the left child of the current node } // End Function
public ExpNode rightChild; // Right child of the node // Constructor public ExpNode(string str) { value = str; leftChild = null; rightChild = null; } // End constructor
} // End Function /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Function: setRightChild * * Input: ExpNode * * Output: void * * Description: A function that sets the value of the right child * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ public void setRightChild(ExpNode right) { rightChild = right; // Sets the left child of the current node } // End Function