Q: Reduce number of parentheses for a binary expression tree I have a function that receives a binary expression tree and returns a String with the expression in-order. The only "problem" is that the resulting expression have too many parentheses,
e.g.: The function returns (a + (b * c)), but it can be reduced to a + b * c.
It is defined with the binary operators +, -, *, /, and the unary operator _ (negative).
What I really want to know is if I can modify the already existing function to reduce the number of parentheses in an efficient way, or create another function that operates with the String of the in-order expression.
The function is as follows:
private static String infijo(ArbolB t){
    String s = "";
    if (t != null) {
        String info = String.valueOf(t.info);
        if ("+-*/".contains(info)) s += "(";
        if ("_".contains(info)) s += "-(";
        s += infijo(t.left) + (info.equals("_") ? "" : info) + infijo(t.right);
        if ("+-*/_".contains(String.valueOf(t.info))) s += ")";
    }
    return s;
}

Where ArbolB is a binary tree defined by:
public class ArbolB {

    ArbolB right;
    ArbolB left;
    Object info;

    public ArbolB(Object info, ArbolB right, ArbolB left){
        this.info = info;
        this.right = right;
        this.left = left;
    }
}


A: After writing this whole thing out, I realized that I didn't really answer your question properly (my solution ignores PEMDAS and just matches pairs, d'oh!). So, take from this what you can... I'm not throwing it out :P
I think you COULD solve this either way, but here would be my preferred method, using and trusting what you already have. There's probably a good way to use nodes to do this, but why not use what you have, right?
Starting from the point where you have your expression as a string (e.g. "((2*2) + _(3+3))" you could try something like:
public string RemoveUnnecessaryParens(string expression)
{
    readonly string openParen = "(";
    readonly string closeParen = ")";
    // char array would also work for this
    // multiple ways to track "balance" of parens, lets use int
    int unclosedParenCount = 0;

    string toReturn = "";

    // iterate through the expression 
    for (int i = 0; i < expression.Length; i++)
    {
        string current = expression.Substring(i,1);
        if (openParen == current)
            unclosedParenCount++;
        else if (closedParen == current)
            unclosedParenCount--;
        else
            toReturn += current;

        if (unclosedParenCount < 0) throw new UnbalancedExpressionException(); // One more close than open! Uh oh!
    }

    if (0 != unclosedParenCount) throw new UnbalancedExpressionException(); // One more open than close at the end! Uh oh!
    else return toReturn;
}

Make sense?

A: Well, after thinking it a while, I got to a solution myself, by adding a priority function for determining when parentheses were necessary, and a variable that indicates if the operation was on the left or the right side of the formula, this because a-b+c don't need parentheses, but c+(a-b) do need them.
private static String infijo(ArbolB t, int priority, boolean right) {
    String s = "";
    int oP = 0;
    if (t != null) {
        String info = String.valueOf(t.info);
        int pi = priority(info);
        if ("+-*/".contains(info)) {
            /* this only adds parentheses if the operation has higher priority or if the 
               operation on the right side should be done before the one on the left side*/
            if ("+-*/".contains(info)) {
                if (pi/2 < priority/2) s += "(";
                else s += pi/2 == priority/2 && pi != priority && right ? "(" : "";
                oP = priority; //stores the old priority
                priority= pi; //priority of the new operator
            }
        }
        if ("_".contains(info)) {
            s += "-";
            oP = priority;
            priority = pi;
        }
        s += infijo(t.left, priority, false) + (info.equals("_") ? "" : info) 
              + infijo(t.right, priority, true);
        if ("+-*/".contains(info)) {
         // adds the closing parentheses following the same rules as for the opening ones
            if (priority / 2 < oP / 2) s += ")";
            else s += priority / 2 == oP / 2 && priority != oP && right ? ")" : "";
        }
    }
    return s;
}

private static int priority(String op) {
    if ("_".contains(op))   return 4;
    if ("/".contains(op))   return 3;
    if ("*".contains(op))   return 2;
    if ("-".contains(op))   return 1;
                            return 0;
}

@Override
public String toString() {
    ArbolB f = getFormula(); //this returns the Binary Expression Tree
    return infijo(f, Integer.MIN_VALUE, false);
}

