1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-11-18 08:29:33 -08:00
Abacus/src/org/nwapw/abacus/tree/OpNode.java

92 lines
2.1 KiB
Java
Raw Normal View History

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.
*/
public class OpNode extends TreeNode {
2017-07-26 10:10:37 -07:00
/**
* The operation being applied.
*/
private String operation;
2017-07-26 10:10:37 -07:00
/**
* The left node of the operation.
*/
private TreeNode left;
2017-07-26 10:10:37 -07:00
/**
* The right node of the operation.
*/
private TreeNode right;
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.
*/
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.
*/
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.
*/
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.
*/
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.
*/
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.
*/
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.
*/
public void setRight(TreeNode right) {
this.right = right;
}
@Override
public <T> T reduce(Reducer<T> reducer) {
T leftReduce = left.reduce(reducer);
T rightReduce = right.reduce(reducer);
return reducer.reduceNode(this, leftReduce, rightReduce);
}
}