Public feed
#602115
public class ExpressionTree implements ExpressionTreeInterface{ Integer num; char operator; ExpressionTreeInterface leftT; ExpressionTreeInterface rightT; public ExpressionTree(int term) { this.num = term; } public ExpressionTree(ExpressionTreeInterface left, char op, ExpressionTreeInterface right) { this.leftT = left; this.operator = op; this.rightT = right; } // post: returns true if this is a term (leaf); false otherwise. public boolean isTerm() { return (this.num instanceof Integer); } // pre: op is one of {'+', '-', '*', '/'}; b is not null // post: returns an expression tree representing (this op b) public ExpressionTreeInterface combine(char op, ExpressionTreeInterface b) { return DataFactory.makeExpressionTree(this, op, b); } // post: returns the number of terms (leaves) in this public int numTerms() { if(this.isTerm()) { return 1; } int right = this.leftT.numTerms(); int left = this.rightT.numTerms(); return right + left; } // post: returns a String representing this. Each subexpression // (an operator together with its children) must be enclosed // in parentheses. public String toString() { String left,right; if(this.isTerm()) { return this.num.toString(); } else { left = "(" + this.leftT.toString(); String op = " " + this.operator + " "; right = this.rightT.toString() + ")"; return left + op + right; } } // post: returns a string representing the expression in RPN notation. public String toRPN() { String left,right; if(this.isTerm()) { return this.num.toString(); } else { left = this.leftT.toRPN() + " "; right = this.rightT.toRPN() + " "; String op = "" + this.operator; return left + right + op; } } // pre: All subtrees of this have the same implementation. // post: returns a list including all of the operators in this // The operators will be in the list in the same order as they // would appear in an in-fix expression. That is, the expression // tree corresponding to (3 + 5) * 12 would have the operators // listed as <+ *> and not <* +> public SubListInterface listOfOperators() { SubListInterface sl = DataFactory.makeSubList(); SubListInterface slL = this.leftT.listOfOperators(); this.listOfOperators(sl); SubListInterface slR = this.rightT.listOfOperators(); return this.listOfOperators(sl); } private SubListInterface listOfOperators(SubListInterface sl) { while(!sl2.isEmpty()) { sl2 = sl2.getRemainder(); } sl2.setRootItem(this.operator); sl2.attachRemainder(DataFactory.makeSubList()); return sl2; } // pre: no subexpression in this results in a division by zero // post: returns the result of evaluating this. public int evaluate() { if(this.isTerm()) { return this.num; } int left = this.leftT.evaluate(); int right = this.rightT.evaluate(); if(this.operator == '+') { return left + right; } else if(this.operator == '*') { return left * right; } else if(this.operator == '-') { return left - right; } else { return left / right; } } // pre: n != 0 // post: returns a new expression tree which is the same as this except // that every term t = i*n such that i is an integer larger than 1 // is replaced by the expression (i * n). public ExpressionTreeInterface factorBy(int n) { if (this.isTerm() && (this.num % n != 0 || this.num / n == 1)) { ExpressionTreeInterface root = new ExpressionTree(this.num); return root; } if (!this.isTerm()) { ExpressionTreeInterface s = new ExpressionTree(this.leftT.factorBy(n), this.operator, this.rightT.factorBy(n)); return s; } else { ExpressionTreeInterface a = new ExpressionTree(this.num / n); ExpressionTreeInterface s = new ExpressionTree(a.factorBy(n), '*', new ExpressionTree(n)); return s; } }}