Expressionspublic class ExpressionTree extends TreeNode implements Expressions
| Modifier and Type | Field | Description |
|---|---|---|
private static java.lang.String |
CLOSED_PAREN |
A closed parentheses
|
private static java.lang.String |
EMPTY |
An empty string
|
private static java.lang.String |
OPEN_PAREN |
An open parentheses
|
private static java.lang.String |
PLUS_SIGN |
A plus sign
|
private static java.lang.String |
SPACE |
A space
|
private static java.lang.String |
TIMES_SIGN |
A multiplication sign
|
| Constructor | Description |
|---|---|
ExpressionTree(java.lang.String initValue) |
Constructs a leaf, an ExpressionTree with a value but no left or right
node, using the superclass's similar constructor method.
|
ExpressionTree(java.lang.String initValue,
TreeNode initLeft,
TreeNode initRight) |
Constructs an ExpressionTree with a value, and using a left and right node,
by using the superclass's constructor.
|
ExpressionTree(TreeNode treeNode) |
Constructs an ExpressionTree by copying the values of the TreeNode
passed to it
|
| Modifier and Type | Method | Description |
|---|---|---|
TreeNode |
buildTree(java.lang.String[] exp) |
Builds a tree to represent the expression using a String[], where each item
of the array is an element in a postfix expression.
|
int |
evalTree() |
Recursively evaluates the tree starting with the root.
|
int |
postfixEval(java.lang.String[] exp) |
Uses a stack to evaluate the expression given by exp, rather than building a tree
and evaluating the tree.
|
java.lang.String |
toInfixNotation() |
Recursively converts the ExpressionTree to a String in infix notation (including
parentheses around each individual expression within the wider expression).
|
java.lang.String |
toPostfixNotation() |
Recursively converts the ExpressionTree to a String in postfix notation.
|
java.lang.String |
toPrefixNotation() |
Recursively converts the ExpressionTree to a String in prefix notation.
|
private static final java.lang.String PLUS_SIGN
private static final java.lang.String TIMES_SIGN
private static final java.lang.String EMPTY
private static final java.lang.String SPACE
private static final java.lang.String OPEN_PAREN
private static final java.lang.String CLOSED_PAREN
public ExpressionTree(java.lang.String initValue)
initValue - the value of the ExpressionTree createdpublic ExpressionTree(java.lang.String initValue,
TreeNode initLeft,
TreeNode initRight)
initValue - the value of this ExpressionTreeinitLeft - the left node of this ExpressionTreeinitRight - the right node of this ExpressionTreepublic ExpressionTree(TreeNode treeNode)
treeNode - the TreeNode to be copiedpublic TreeNode buildTree(java.lang.String[] exp)
buildTree in interface Expressionsexp - a String[] holding the elements of an expressionpublic int evalTree()
evalTree in interface Expressionspublic java.lang.String toPrefixNotation()
toPrefixNotation in interface Expressionspublic java.lang.String toInfixNotation()
toInfixNotation in interface Expressionspublic java.lang.String toPostfixNotation()
toPostfixNotation in interface Expressionspublic int postfixEval(java.lang.String[] exp)
postfixEval in interface Expressionsexp - the expression to be evaluated where each item of the String[] represents
an item in the expression