Java 如何用递归简化算术表达式树?
如何使用递归来简化表达式树?例如: 我尝试了一些不同的方法,但我无法找出如何以一种我能够简化它的方式在树中递归。谢谢Java 如何用递归简化算术表达式树?,java,recursion,tree,Java,Recursion,Tree,如何使用递归来简化表达式树?例如: 我尝试了一些不同的方法,但我无法找出如何以一种我能够简化它的方式在树中递归。谢谢 public static LinkedBinaryTree<String> simplify(LinkedBinaryTree<String> tree) throws IllegalArgumentException { // TODO: Implement this method if (!isArithmeticExpressio
public static LinkedBinaryTree<String> simplify(LinkedBinaryTree<String> tree) throws IllegalArgumentException {
// TODO: Implement this method
if (!isArithmeticExpression(tree) || tree == null) {
throw new IllegalArgumentException("Invalid arithmetic expression or Tree is null");
}
return simplify(tree.root(), tree);
}
public static LinkedBinaryTree<String> simplify(Position<String> p, LinkedBinaryTree<String> tree) {
// if (tree.isExternal(p)) {
// return tree;
// }
if (isAlphaNumeric(p.getElement())) {
}
if (p.getElement().equals("+") || p.getElement().equals("+") || p.getElement().equals("+")) {
}
String element = p.getElement();
char charLeft = tree.left(p).getElement().charAt(0);
char charRight = tree.right(p).getElement().charAt(0);
String stringLeft = tree.left(p).getElement();
String stringRight = tree.right(p).getElement();
// if (stringLeft.equals("+") || stringLeft.equals("-") || stringLeft.equals("*")) {
// simplify(tree.left(p), tree);
// }
// if (!(Character.isLetter(charLeft) || Character.isDigit(charLeft))) {
//
// }
return null;
}
公共静态LinkedBinaryTree simplify(LinkedBinaryTree树)抛出IllegalArgumentException{
//TODO:实现此方法
如果(!IsarithMetricExpression(tree)| | tree==null){
抛出新的IllegalArgumentException(“无效的算术表达式或树为空”);
}
返回simplify(tree.root(),tree);
}
公共静态LinkedBinaryTree简化(位置p,LinkedBinaryTree树){
//if(树等外部(p)){
//回归树;
// }
if(isAlphaNumeric(p.getElement())){
}
如果(p.getElement().equals(“+”)| p.getElement().equals(“+”)| p.getElement().equals(“+”){
}
字符串元素=p.getElement();
char charLeft=tree.left(p.getElement().charAt(0);
char charRight=tree.right(p.getElement().charAt(0);
字符串stringLeft=tree.left(p.getElement();
String stringRight=tree.right(p.getElement();
//if(stringLeft.equals(“+”)| | stringLeft.equals(“-”| | stringLeft.equals(“*”)){
//简化(tree.left(p),tree);
// }
//if(!(Character.islitter(charLeft)| | Character.isDigit(charLeft))){
//
// }
返回null;
}
以下是一些伪代码,它们将为您指明正确的方向
public int calculate() {
return calculateNode(rootNode)
}
private int calculateNode(Node node) {
if (node.isOperator()){
int operandLeft = calculateNode(node.leftChild();
int operandRight = calculateNode(node.leftChild();
return node.getOperator().operateOn(operandLeft, operandRight);
} else {
return node.getValue()
}
}
函数calculate()
递归函数calculateNode(Node Node)
最重要的一点是查看when-a-Node-is-a-number的结束条件,但有时意识到递归函数应该在一次调用中调用自己两次也很棘手。这只是一个例子,但您可以根据自己的需要进一步调整:
int simplify(Node root) {
if(root == null) {
return 0;
}
int result;
switch (root.value) {
case "-":
result = simplify(root.left) - simplify(root.right);
break;
case "+":
result = simplify(root.left) + simplify(root.right);
break;
default:
return Integer.parseInt(root.value);
}
return result;
}
因此,通过查看您的树
,您可以有两种情况:
- 节点的值是一个操作数,在这种情况下,当应用给定的操作数时,递归地转到右节点和左节点
- 节点的值是number,然后只返回它的值