Binary tree to calculate simple operations

1

This tree represents the following logical operation:

(((5 + 3) / 2) + ((1 - 5) * 2))

Does anyone know how to calculate it? You can use any data structure I have used the following:

public class Elem{
   char operacion;
   int numero;
   boolean esOperacion;
}
public class Nodo{
   Elem contenido;
   Nodo izq;
   Nodo der;
   Nodo padre;
}
public class ArbolBinario{
   Nodo raiz;
   public int calcular(){
      return(calcular(0,root));
   }
   private int calcular(int resultado, Nodo a){
       if(a == null){
          return(resultado);
       }else if(a.esOperacion){
            if(a.left != null && a.rigth != null){
                if(operacion.equals('+'){
                    return(calcular(resultado,a.izq) + calcular(resultado, a.der);
                }else if(operacion.equals('-'){
                    return(calcular(resultado,a.izq) - calcular(resultado, a.der);
                }else if(operacion.equals('*'){
                    return(calcular(resultado,a.izq) * calcular(resultado, a.der);
                }else{
                    return(calcular(resultado,a.izq) / calcular(resultado, a.der);
                }
              //POR AQUI SEGUIRIA PERO ESTOY BASTANTE SEGURO DE QUE HAY FORMAS MEJORES DE HACERLO O DE QUE ESTA MAL
            }
       }
   }
}
    
asked by UnaiLopez 21.11.2017 в 17:20
source

1 answer

1

I would use a different structure, defining a common Nodo interface, with 2 implementations. One that represents a number, and the other that represents an arithmetic operation:

public interface Nodo {}

public class NumeroNodo implements Nodo {
    public int numero;
}

public class OperacionNodo implements Nodo {
    public char operacion;
    public Nodo expresionIzquierda;
    public Nodo expresionDerecha;
}

With this structure, the recursive logic to make the cell from the root of a tree of nodes is very simple:

public int calcular(Nodo nodo) {
    if (nodo instanceof NumeroNodo) {
        return ((NumeroNodo)nodo).numero;
    } else {
        OperacionNodo operacionNodo = (OperacionNodo)nodo;
        int valorIzquierda = calcular(operacionNodo.expresionIzquierda);
        int valorDerecha = calcular(operacionNodo.expresionDerecha);

        switch(operacionNodo.operacion) {
            case '+':
                return valorIzquierda + valorDerecha;
            case '-':
                return valorIzquierda - valorDerecha;
            case '*':
                return valorIzquierda * valorDerecha;
            case '/':
                return valorIzquierda / valorDerecha;
            default:
                throw new RuntimeException("Operación inválida: " + operacionNodo.operacion);
        }
    }
}

Take into account that when you use the int to manipulate the values, all arithmetic operations will lose the fractional parts of the calculations if there are any, and may end up giving you an unexpected result. To avoid this problem, you can use a float or a double in all places where you have a int .

    
answered by 21.11.2017 / 18:55
source