title: How to simplify an arithmetic expression tree with recursion?
body: <p>How would i use recursion to simplify an expression tree? For example:</p>
  
  <p><a href="https://i.stack.imgur.com/e9mzo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/e9mzo.png" alt="enter image description here"></a></p>
  
  <p>I've tried a few different things but I cannot figure out how to recurse through the tree in a way that i am able to simplify it. Thanks.</p>
  
  <pre><code>public static LinkedBinaryTree&lt;String&gt; simplify(LinkedBinaryTree&lt;String&gt; 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&lt;String&gt; simplify(Position&lt;String&gt; p, LinkedBinaryTree&lt;String&gt; 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;
  
  }
  </code></pre>