どのように式ツリーを単純化するために再帰を使用しますか?たとえば:再帰で算術式ツリーを単純化するには?
私はいくつかの異なるものを試してみたが、私はそれを簡素化することができる午前方法でツリーを再帰する方法を見つけ出すことはできません。ありがとう。
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;
}