public class ExpressionTree extends ATree
ATree.Node
Constructor | Description |
---|---|
ExpressionTree() |
Modifier and Type | Method | Description |
---|---|---|
void |
build(java.util.List<java.lang.String> postfix) |
Calls a recursive helper method to build an expression tree from a postfix
List . |
boolean |
buildRecursive(ATree.Node current,
java.lang.String token) |
Builds an expression tree from the postfix representation returned from the convert method.
|
java.util.List<java.lang.String> |
convert(java.util.Queue<java.lang.String> infix) |
Convert an infix expression into postfix order.
|
int |
evaluate() |
Calls a recursive helper method to evaluate expression tree and return the result.
|
int |
evaluateRecursive(ATree.Node current) |
Traverses the expression tree and produces the correct answer, which should be an integer.
|
java.lang.String |
infix() |
Calls a recursive helper method to traverse the expression tree in infix order and build the expression.
|
java.lang.String |
infixRecursive(ATree.Node current) |
Concatenates the tokens in the expression tree returned from the
build(List) method in infix order. |
java.util.Queue<java.lang.String> |
parse(java.lang.String expression) |
Parse an infix expression into an
ArrayList of tokens. |
java.lang.String |
postfix() |
Calls a recursive helper method to traverse the expression tree in postfix order and build the expression.
|
java.lang.String |
postfixRecursive(ATree.Node current) |
Concatenates the tokens in the expression tree returned from the
build(List) method in postfix order. |
java.lang.String |
prefix() |
Calls a recursive helper method to traverse the expression tree in prefix order and build expression.
|
java.lang.String |
prefixRecursive(ATree.Node current) |
Concatenates the tokens in the expression tree returned from the
build(List) method in prefix order. |
display, displayRecursive, isInteger, isOperator, precedence, valueOf
public java.util.Queue<java.lang.String> parse(java.lang.String expression)
ATree
ArrayList
of tokens.
Reads a string that contains an infix expression, and converts it to a list of tokens. Each token is an operator, operand (integer), or parentheses. We suggest using the StringTokenizer method of lexing from the expressions lab, no changes should be necessary except for the addition of the modulo operator. The parse method should be able to handle an arbitrary number of white spaces in the expression. Our implementation of this method is ~8 lines of code, including brackets but not comments or white space.
public java.util.List<java.lang.String> convert(java.util.Queue<java.lang.String> infix)
ATree
Converts the list of tokens from the parse method from infix to postfix,
using the Shunting-yard algorithm from Edsger Dijkstra, a famous computer
scientist, as documented here.
Our implementation uses Queue
for the queue, and Deque
for the stack. You may want to use the utility methods ATree.isOperator(String)
,
ATree.isInteger(String)
, and ATree.precedence(String)
.
Our implementation is ~21 lines of code.
public void build(java.util.List<java.lang.String> postfix)
ATree
List
.public boolean buildRecursive(ATree.Node current, java.lang.String token)
List<String> postfix
, and places
them at the next available node in the tree.
Here is the exact algorithm:
true
.
true
.
true
.
true
if successful,
otherwise continue.
true
, then the code has failed to add
the token to the tree, so return false
.
current
- the current Node being checkedtoken
- the token to addtrue
, if successfulpublic java.lang.String prefix()
ATree
public java.lang.String prefixRecursive(ATree.Node current)
build(List)
method in prefix order.
Accumulate the operator first, then the string from the left and right subtrees. Add an extra space after each token.
Our implementation of this method is ~2-6 lines of code.
current
- the root nodepublic java.lang.String infix()
ATree
public java.lang.String infixRecursive(ATree.Node current)
build(List)
method in infix order.
Our implementation of this method is ~2-6 lines of code.
current
- public java.lang.String postfix()
ATree
public java.lang.String postfixRecursive(ATree.Node current)
build(List)
method in postfix order.
First accumulate the string from the left and right subtrees, then add the
operator. Add an extra space after each token.
Our implementation of this method is ~2-6 lines of code.
current
- reference to the current node (starts with root)public int evaluate()
ATree
public int evaluateRecursive(ATree.Node current)
Our implementation uses one helper method (~7 lines of code) and is, itself, ~2 lines of code.
current
-