mirror of
https://github.com/DanilaFe/abacus
synced 2024-12-24 00:10:09 -08:00
Implement toString in child nodes of TreeNode.
This commit is contained in:
parent
51ca0a9ae2
commit
c88a0cbdc5
|
@ -49,4 +49,9 @@ public class NumberNode extends TreeNode {
|
||||||
public <T> T reduce(Reducer<T> reducer) {
|
public <T> T reduce(Reducer<T> reducer) {
|
||||||
return reducer.reduceNode(this);
|
return reducer.reduceNode(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return number != null ? number.toString() : "null";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,4 +88,19 @@ public class OpNode extends TreeNode {
|
||||||
T rightReduce = right.reduce(reducer);
|
T rightReduce = right.reduce(reducer);
|
||||||
return reducer.reduceNode(this, leftReduce, rightReduce);
|
return reducer.reduceNode(this, leftReduce, rightReduce);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
String leftString = left != null ? left.toString() : "null";
|
||||||
|
String rightString = right != null ? right.toString() : "null";
|
||||||
|
|
||||||
|
if(right != null && right instanceof OpNode){
|
||||||
|
if(TreeNode.precedenceMap.get(((OpNode) right).getOperation()) >
|
||||||
|
TreeNode.precedenceMap.get(operation)) {
|
||||||
|
rightString = "(" + rightString + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return leftString + operation + rightString;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user