1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-07-02 15:27:22 -07:00
Abacus/src/main/java/org/nwapw/abacus/tree/BinaryNode.java

109 lines
2.5 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.
*/
2017-08-02 10:41:52 -07:00
public class BinaryNode 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;
2017-08-02 10:41:52 -07:00
private BinaryNode() {
2017-07-30 21:11:32 -07:00
}
2017-07-26 10:10:37 -07:00
/**
* Creates a new operation node with the given operation
* and no child nodes.
2017-07-30 21:11:32 -07:00
*
2017-07-26 10:10:37 -07:00
* @param operation the operation.
*/
2017-08-02 10:41:52 -07:00
public BinaryNode(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.
2017-07-30 21:11:32 -07:00
*
2017-07-26 10:10:37 -07:00
* @param operation the operation.
2017-07-30 21:11:32 -07:00
* @param left the left node of the expression.
* @param right the right node of the expression.
2017-07-26 10:10:37 -07:00
*/
2017-08-02 10:41:52 -07:00
public BinaryNode(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.
2017-07-30 21:11:32 -07:00
*
2017-07-26 10:10:37 -07:00
* @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.
2017-07-30 21:11:32 -07:00
*
2017-07-26 10:10:37 -07:00
* @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.
2017-07-30 21:11:32 -07:00
*
2017-07-26 10:10:37 -07:00
* @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.
2017-07-30 21:11:32 -07:00
*
2017-07-26 10:10:37 -07:00
* @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.
2017-07-30 21:11:32 -07:00
*
2017-07-26 10:10:37 -07:00
* @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);
2017-07-30 21:11:32 -07:00
if (leftReduce == null || rightReduce == null) return null;
2017-08-05 13:26:29 -07:00
return reducer.reduceNode(this, leftReduce, rightReduce);
}
@Override
public String toString() {
String leftString = left != null ? left.toString() : "null";
String rightString = right != null ? right.toString() : "null";
return "(" + leftString + operation + rightString + ")";
}
}