2017-07-25 14:21:00 -07:00
|
|
|
package org.nwapw.abacus.tree;
|
|
|
|
|
2017-07-26 10:10:37 -07:00
|
|
|
/**
|
|
|
|
* A tree node that represents an operation being applied to two operands.
|
|
|
|
*/
|
2017-07-25 14:21:00 -07:00
|
|
|
public class OpNode extends TreeNode {
|
|
|
|
|
2017-07-26 10:10:37 -07:00
|
|
|
/**
|
|
|
|
* The operation being applied.
|
|
|
|
*/
|
2017-07-25 14:21:00 -07:00
|
|
|
private String operation;
|
2017-07-26 10:10:37 -07:00
|
|
|
/**
|
|
|
|
* The left node of the operation.
|
|
|
|
*/
|
2017-07-25 14:21:00 -07:00
|
|
|
private TreeNode left;
|
2017-07-26 10:10:37 -07:00
|
|
|
/**
|
|
|
|
* The right node of the operation.
|
|
|
|
*/
|
2017-07-25 14:21:00 -07:00
|
|
|
private TreeNode right;
|
|
|
|
|
2017-07-26 10:58:27 -07:00
|
|
|
private OpNode() {}
|
|
|
|
|
2017-07-26 10:10:37 -07:00
|
|
|
/**
|
|
|
|
* Creates a new operation node with the given operation
|
|
|
|
* and no child nodes.
|
|
|
|
* @param operation the operation.
|
|
|
|
*/
|
2017-07-25 14:21:00 -07:00
|
|
|
public OpNode(String operation){
|
|
|
|
this(operation, null, null);
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:10:37 -07:00
|
|
|
/**
|
|
|
|
* Creates a new operation node with the given operation
|
|
|
|
* and child nodes.
|
|
|
|
* @param operation the operation.
|
|
|
|
* @param left the left node of the expression.
|
|
|
|
* @param right the right node of the expression.
|
|
|
|
*/
|
2017-07-25 14:21:00 -07:00
|
|
|
public OpNode(String operation, TreeNode left, TreeNode right){
|
|
|
|
this.operation = operation;
|
|
|
|
this.left = left;
|
|
|
|
this.right = right;
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:10:37 -07:00
|
|
|
/**
|
|
|
|
* Gets the operation in this node.
|
|
|
|
* @return the operation in this node.
|
|
|
|
*/
|
2017-07-25 14:21:00 -07:00
|
|
|
public String getOperation() {
|
|
|
|
return operation;
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:10:37 -07:00
|
|
|
/**
|
|
|
|
* Gets the left sub-expression of this node.
|
|
|
|
* @return the left node.
|
|
|
|
*/
|
2017-07-25 14:21:00 -07:00
|
|
|
public TreeNode getLeft() {
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:10:37 -07:00
|
|
|
/**
|
|
|
|
* Sets the left sub-expression of this node.
|
|
|
|
* @param left the sub-expression to apply.
|
|
|
|
*/
|
2017-07-25 14:21:00 -07:00
|
|
|
public void setLeft(TreeNode left) {
|
|
|
|
this.left = left;
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:10:37 -07:00
|
|
|
/**
|
|
|
|
* Gets the right sub-expression of this node.
|
|
|
|
* @return the right node.
|
|
|
|
*/
|
2017-07-25 14:21:00 -07:00
|
|
|
public TreeNode getRight() {
|
|
|
|
return right;
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:10:37 -07:00
|
|
|
/**
|
|
|
|
* Sets the right sub-expression of this node.
|
|
|
|
* @param right the sub-expression to apply.
|
|
|
|
*/
|
2017-07-25 14:21:00 -07:00
|
|
|
public void setRight(TreeNode right) {
|
|
|
|
this.right = right;
|
|
|
|
}
|
2017-07-26 10:58:27 -07:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public <T> T reduce(Reducer<T> reducer) {
|
|
|
|
T leftReduce = left.reduce(reducer);
|
|
|
|
T rightReduce = right.reduce(reducer);
|
|
|
|
return reducer.reduceNode(this, leftReduce, rightReduce);
|
|
|
|
}
|
2017-07-25 14:21:00 -07:00
|
|
|
}
|