ExpressionTree
public abstract class ATree
extends java.lang.Object
Modifier and Type | Class | Description |
---|---|---|
class |
ATree.Node |
Modifier and Type | Field | Description |
---|---|---|
ATree.Node |
root |
Constructor | Description |
---|---|
ATree() |
Modifier and Type | Method | Description |
---|---|---|
abstract void |
build(java.util.List<java.lang.String> postfix) |
Calls a recursive helper method to build an expression tree from a postfix
List . |
abstract java.util.List<java.lang.String> |
convert(java.util.Queue<java.lang.String> infix) |
Convert an infix expression into postfix order.
|
java.util.ArrayList<java.lang.String> |
display() |
Displays the expression tree in graphical format.
|
void |
displayRecursive(ATree.Node current,
java.util.ArrayList<java.lang.String> graph,
java.lang.String name) |
|
abstract int |
evaluate() |
Calls a recursive helper method to evaluate expression tree and return the result.
|
abstract java.lang.String |
infix() |
Calls a recursive helper method to traverse the expression tree in infix order and build the expression.
|
static boolean |
isInteger(java.lang.String token) |
|
static boolean |
isOperator(java.lang.String token) |
|
abstract java.util.Queue<java.lang.String> |
parse(java.lang.String expression) |
Parse an infix expression into an
ArrayList of tokens. |
abstract java.lang.String |
postfix() |
Calls a recursive helper method to traverse the expression tree in postfix order and build the expression.
|
static int |
precedence(java.lang.String operator) |
Returns operator precedence.
|
abstract java.lang.String |
prefix() |
Calls a recursive helper method to traverse the expression tree in prefix order and build expression.
|
static int |
valueOf(java.lang.String token) |
Converts an
String into an int . |
public ATree.Node root
public abstract java.util.Queue<java.lang.String> parse(java.lang.String expression)
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.
expression
- a valid expressionpublic abstract java.util.List<java.lang.String> convert(java.util.Queue<java.lang.String> infix)
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 isOperator(String)
,
isInteger(String)
, and precedence(String)
.
Our implementation is ~21 lines of code.
infix
- expression in infix orderpublic abstract void build(java.util.List<java.lang.String> postfix)
List
.postfix
- the expression treepublic abstract java.lang.String prefix()
public abstract java.lang.String infix()
public abstract java.lang.String postfix()
public abstract int evaluate()
public java.util.ArrayList<java.lang.String> display()
public void displayRecursive(ATree.Node current, java.util.ArrayList<java.lang.String> graph, java.lang.String name)
current
- graph
- name
- public static boolean isOperator(java.lang.String token)
token
- a tokenpublic static boolean isInteger(java.lang.String token)
token
- a tokenpublic static int valueOf(java.lang.String token)
String
into an int
.
For example the string "11" is converted to the int 11.token
- a string representing a numberpublic static int precedence(java.lang.String operator)
operator
- the operator to evaluate