- /**
- * filename: ExpressionTree.java
- * package:
- * author: Nick Ma
- * email: nick.ma85@yahoo.com
- * date: Nov 1, 2008
- * description: this class implements an expression tree extended from binary tree.
- */
- import java.util.*;
- public class ExpressionTree extends BinaryTree {
- // Data Field
- /** the operators */
- private static final String OPERATORS = "+-*/" ;
- /** the original infix expression */
- private String originalExp;
- /**
- * description: the constructor with fields to build an expression tree from infix
- * @param originalExp - infix exp
- */
- public ExpressionTree(String originalExp) {
- this .originalExp = originalExp;
- InfixToPostfixConvertor pc = new InfixToPostfixConvertor();
- try {
- // convert the original infix exp to postfix
- StringTokenizer st = new StringTokenizer(pc.convert(originalExp));
- // stack to save temp tree nodes
- Stack<TreeNode> treeStack = new Stack<TreeNode>();
- while (st.hasMoreTokens()) {
- // iterator the tokens
- String nextToken = st.nextToken();
- char firstChar = nextToken.charAt( 0 );
- // determine if it is an operand
- if (Character.isJavaIdentifierStart(firstChar)
- || Character.isDigit(firstChar)){
- treeStack.push( new TreeNode(nextToken));
- } else if (isOperator(firstChar)) {
- // pop two operands
- TreeNode right = treeStack.pop();
- TreeNode left = treeStack.pop();
- //create a new tree to push
- treeStack.push( new TreeNode(nextToken, left, right));
- } else {
- throw new SyntaxErrorException( "syntax error!" );
- }
- }
- // determine if the tree stack just has one tree
- if (treeStack.size() != 1 ) {
- throw new SyntaxErrorException( "syntax error" );
- }
- // let the only tree node popped from the tree stack be the root
- this .root = treeStack.pop();
- // determine if the tree stack is empty
- if (!treeStack.isEmpty()) {
- throw new SyntaxErrorException( "syntax error" );
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * description: print prefix exp from this tree
- */
- public void prefix(TreeNode node) {
- if (node == null ) {
- return ;
- } else {
- System.out.print((String)node.getData());
- prefix(node.getLeft());
- prefix(node.getRight());
- }
- }
- /**
- * description: print infix exp from this tree
- */
- public void infix(TreeNode node) {
- if (node == null ) {
- return ;
- } else {
- if (!Character.isJavaIdentifierStart(((String)node.getData()).charAt( 0 ))) {
- System.out.print( "(" );
- }
- infix(node.getLeft());
- System.out.print((String)node.getData());
- infix(node.getRight());
- if (!Character.isJavaIdentifierStart(((String)node.getData()).charAt( 0 ))) {
- System.out.print( ")" );
- }
- }
- }
- /**
- * description: print postfix exp from this tree
- */
- public void postfix(TreeNode node) {
- if (node == null ) {
- return ;
- } else {
- postfix(node.getLeft());
- postfix(node.getRight());
- System.out.print((String)node.getData());
- }
- }
- /**
- * description: evaluate the expression tree
- * @return - the value of the expression
- */
- public int evaluate() {
- InfixToPostfixConvertor pc = new InfixToPostfixConvertor();
- StringBuilder sb = new StringBuilder();
- Random rd = new Random();
- String postfix = null ;
- try {
- // convert the original infix exp to postfix
- postfix = pc.convert( this .originalExp);
- // build a postfix expression string
- StringTokenizer st = new StringTokenizer(postfix);
- while (st.hasMoreTokens()) {
- String next = st.nextToken();
- if (Character.isJavaIdentifierStart(next.charAt( 0 ))) {
- // generate the integer random for the variables of this expression
- sb.append(rd.nextInt( 100 ));
- } else {
- sb.append(next);
- }
- sb.append( " " );
- }
- System.out.print(sb.toString() + " = " );
- // call postfix evaluator
- PostfixEvaluator pe = new PostfixEvaluator(sb.toString());
- return pe.eval();
- } catch (Exception e) {
- e.printStackTrace();
- return 0 ;
- }
- }
- /**
- * description: determine whether a char is an operator
- * @param op - the given char
- * @return - true if the char is an operator
- */
- private boolean isOperator( char op) {
- return OPERATORS.indexOf(op) != - 1 ;
- }
- /**
- * @return the root
- */
- public TreeNode getRoot() {
- return root;
- }
- /**
- * @param root the root to set
- */
- public void setRoot(TreeNode root) {
- this .root = root;
- }
- }
Expression Tree
最新推荐文章于 2024-02-28 19:17:26 发布