public class ShuntingYardExpressionTree extends ShuntingYard
ShuntingYardExpressionTree
class provides an implementation of the Shunting Yard algorithm using Expression Tree.Modifier and Type | Class and Description |
---|---|
protected class |
ShuntingYardExpressionTree.Node
The
Node class represents a node of the expression tree. |
Modifier and Type | Field and Description |
---|---|
protected Stack<Operator> |
operatorStack
Stack to hold the operators.
|
protected Stack<Token> |
postfix
Stack to hold the postfix (RPN) expression.
|
protected ShuntingYardExpressionTree.Node |
root
Root node of the expression tree.
|
Constructor and Description |
---|
ShuntingYardExpressionTree()
No-Argument Constructor.
|
Modifier and Type | Method and Description |
---|---|
protected Operand |
evaluate(ShuntingYardExpressionTree.Node node)
Method to recursively evaluate the expression tree and return the result as an operand.
|
double |
evaluate(String expr)
Method to evaluate an expression.
This method acts as the single point of access for expression evaluation. |
protected void |
evaluateParenthesis()
Method to evaluate operators at the top of the operator stack until a left parenthesis is encountered.
|
protected void |
formTree()
Method to form the expression tree.
|
protected boolean |
formTree(ShuntingYardExpressionTree.Node node,
Token token)
Method to form the expression tree recursively.
|
protected void |
init(String expr)
Method to create the postfix expression from the infix expression.
|
protected ShuntingYardExpressionTree.Node root
public ShuntingYardExpressionTree()
protected void init(String expr)
expr
- Expression stringprotected void evaluateParenthesis()
protected boolean formTree(ShuntingYardExpressionTree.Node node, Token token)
node
- Current node of the expression treetoken
- Token to be insertedprotected void formTree()
protected Operand evaluate(ShuntingYardExpressionTree.Node node)
node
- Current node of the expression treepublic double evaluate(String expr)
evaluate
in class ShuntingYard
expr
- Expression stringCopyright © 2019 Pratanu Mandal. All rights reserved.