Implement toString in child nodes of TreeNode.

This commit is contained in:
Danila Fedorin 2017-07-26 17:26:55 -07:00
parent 8754871556
commit d7caf1cdc7
2 changed files with 20 additions and 0 deletions

View File

@ -49,4 +49,9 @@ public class NumberNode extends TreeNode {
public <T> T reduce(Reducer<T> reducer) {
return reducer.reduceNode(this);
}
@Override
public String toString() {
return number != null ? number.toString() : "null";
}
}

View File

@ -88,4 +88,19 @@ public class OpNode extends TreeNode {
T rightReduce = right.reduce(reducer);
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;
}
}