二叉树 计算后缀表达式

表达式树:

1.继承自LinkedBinaryTree

2.因为在类的声明时没有用泛型,所以在继承时给出具象ExpressionTreeObj

3.有参构造函数递归的计算count数

4.evaluateNode方法,递归的计算。

 

关键点

 

if (temp.isOperator()) { operand1 = evaluateNode(root.left); operand2 = evaluateNode(root.right); result = computeTerm(temp.getOperator(), operand1, operand2); } else result = temp.getValue();  

 

 

 

public class ExpressionTree extends LinkedBinaryTree { /** * Creates an empty expression tree. */ public ExpressionTree() { super(); } /** * Constructs a expression tree from the two specified expression * trees. * * @param element the expression tree for the center * @param leftSubtree the expression tree for the left subtree * @param rightSubtree the expression tree for the right subtree */ public ExpressionTree (ExpressionTreeObj element, ExpressionTree leftSubtree, ExpressionTree rightSubtree) { root = new BinaryTreeNode (element); count = 1; if (leftSubtree != null) { count = count + leftSubtree.size(); root.left = leftSubtree.root; } else root.left = null; if (rightSubtree !=null) { count = count + rightSubtree.size(); root.right = rightSubtree.root; } else root.right = null; } /** * Evaluates the expression tree by calling the recursive * evaluateNode method. * * @return the integer evaluation of the tree */ public int evaluateTree() { return evaluateNode(root); } /** * Recursively evaluates each node of the tree. * * @param root the root of the tree to be evaluated * @return the integer evaluation of the tree */ public int evaluateNode(BinaryTreeNode root) { int result, operand1, operand2; ExpressionTreeObj temp; if (root==null) result = 0; else { temp = (ExpressionTreeObj)root.element; if (temp.isOperator()) { operand1 = evaluateNode(root.left); operand2 = evaluateNode(root.right); result = computeTerm(temp.getOperator(), operand1, operand2); } else result = temp.getValue(); } return result; } /** * Evaluates a term consisting of an operator and two operands. * * @param operator the operator for the expression * @param operand1 the first operand for the expression * @param operand2 the second operand for the expression */ private static int computeTerm(char operator, int operand1, int operand2) { int result=0; if (operator == '+') result = operand1 + operand2; else if (operator == '-') result = operand1 - operand2; else if (operator == '*') result = operand1 * operand2; else result = operand1 / operand2; return result; } } 

 

ExpressionTreeObj:

 

 

1.该类为ExpressionTree所对应的泛型类型

 

public class ExpressionTreeObj { private int termType; private char operator; private int value; /** * Creates a new expression tree object with the specified data. * * @param type the integer type of the expression * @param op the operand for the expression * @param val the value for the expression */ public ExpressionTreeObj (int type,char op, int val) { termType = type; operator = op; value = val; } /** * Returns true if this object is an operator and false otherwise. * * @return true if this object is an operator */ public boolean isOperator() { return (termType == 1); } /** *Returns the operator of this expression tree object. * * @return the character representation of the operator */ public char getOperator() { return operator; } /** * Returns the value of this expression tree object. * * @return the value of this expression tree object */ public int getValue() { return value; } }  

 

PostfixEvaluator2

 

1.用一个treeStack存放每一个构造的ExpressionTree

   所以有:LinkedStack treeStack

2.根据输入的后缀表达式最终构造出一个完整的ExpressionTree

3.如果是操作数则 用操作数构造ExpressionTree并压入栈中

   如果是操作符则 弹出栈中两个ExpressionTree

   以操作符为新树的root operand1,operand2分别作为树的

   左子树与右子树。

/** * PostfixEvaluator2 this modification of our stack example uses a pair of * stacks to create an expression tree from a VALID postfix integer expression * and then uses a recursive method from the ExpressionTree class to * evaluate the tree. * */ public class PostfixEvaluator2 { /** * Retrieves and returns the next operand off of this tree stack. * * @param treeStack the tree stack from which the operand will be returned * @return the next operand off of this tree stack */ private ExpressionTree getOperand(LinkedStack treeStack) { ExpressionTree temp; temp = treeStack.pop(); return temp; } /** * Retrieves and returns the next token, either an operator or * operand from the user. * * @return the next string token */ private String getNextToken() { String tempToken = "0", inString; StringTokenizer tokenizer; try { BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); inString = in.readLine(); tokenizer = new StringTokenizer(inString); tempToken = (tokenizer.nextToken()); } catch (Exception IOException) { System.out.println("An input/output exception has occurred"); } return tempToken; } /** * Prompts the user for a valid post-fix expression, converts it to * an expression tree using a two stack method, then calls a * recursive method to evaluate the expression. */ public void solve () { ExpressionTree operand1, operand2; char operator; String tempToken; LinkedStack treeStack = new LinkedStack(); System.out.println("Enter a valid post-fix expression one token " + "at a time pressing the enter key after each token"); System.out.println("Enter an integer, an operator (+,-,*,/) then ! to evaluate "); tempToken = getNextToken(); operator = tempToken.charAt(0); while (!(operator == '!')) { if ((operator == '+') || (operator == '-') || (operator == '*') || (operator == '/')) { operand1 = getOperand(treeStack); operand2 = getOperand(treeStack); treeStack.push(new ExpressionTree (new ExpressionTreeObj(1,operator,0), operand2, operand1)); } else { treeStack.push(new ExpressionTree (new ExpressionTreeObj (2,' ',Integer.parseInt(tempToken)), null, null)); } tempToken = getNextToken(); operator = tempToken.charAt(0); } System.out.print("The result is "); System.out.println(((ExpressionTree)treeStack.peek()).evaluateTree()); } }  

 

Postfix2

/** * Postfix2 uses the PostfixEvaluator class to solve a postfix expression * */ public class Postfix2 { /** * Uses the PostfixEvaluator class to solve a postfix expression. */ public static void main (String[] args) { PostfixEvaluator2 temp = new PostfixEvaluator2(); temp.solve(); } }  

 

 

 

 

 

 

你可能感兴趣的:(数据结构)