Calculation predecessor of a node

0

First I will explain the code so that the answers can be precise.

The ancestor of a node is defined as the minimum difference between the node from which you want to calculate the ancestor and the other nodes of the tree.

Launcher method calculateAssessor:

1) I check that the object x that I pass as a parameter, effectively, is in the tree.   2) Once the verification is done, I call the recursive method.

Accurate Calculator Method:

First the node is searched within the tree. The search is based on the principles of ABB trees. Once found, two cases are differentiated:

1) The node from which your predecessor is to be calculated does not have a left subtree. Since it is a binary search tree, the ancestor will be the parent of that node. (Recover parent method)

2) The node in question has a left subtree. Then, the largest element of the subtree with root node is calculated, the node from which the predecessor is to be calculated. (Major calculation method)

Now, it seems to me that the code itself is a bit tricky and inefficient (given the multiple recursive calls). Any ideas to improve it?

 @Override
    public NodoABB calcularAntecesor(Object x) throws ElementoNoEncontrado
    {
        boolean flag = false;
        NodoABB res;
        String cadena = super.toStringInOrden();
        for(int i = 0; i < cadena.length(); i++)
            if(x.toString().compareTo(x.toString()) == 0)
                flag = true;
        if(flag == true)
        {
            res = calcularAntecesor(x, super.raiz);

        }
            else 
            throw new ElementoNoEncontrado("Elemento no encontrado");

        return res;
    }

    protected NodoABB calcularAntecesor(Object x, NodoABB actual)
        { 
            int res = x.toString().compareTo(actual.dato.toString());
            if(res > 0)
                actual = calcularAntecesor(x, actual.der);
            if(res < 0)
                actual = calcularAntecesor(x, actual.izq);
            if(res == 0)
            {
                if(actual.izq != null)
                {
                    return calcularMayor(actual, actual, actual);
                }
                else
                    try{
                        return recuperarPadre(actual, super.raiz);
                    }catch(ElementoNoEncontrado excp){System.out.println(excp.getMessage());}
            }
            return actual;

        }

        private NodoABB recuperarPadre(NodoABB resul, NodoABB actual ) throws ElementoNoEncontrado
        {   
            if(actual == null)
                throw new ElementoNoEncontrado("No está el elemento");
            if(actual.izq != null && actual.izq.dato.toString().compareTo(resul.dato.toString()) == 0)
                return actual;
            if(actual.der != null && actual.der.dato.toString().compareTo(resul.dato.toString()) == 0)
                return actual;
            if(actual.izq == null && actual.dato.toString().compareTo(resul.dato.toString()) == 0)
                return actual;
            if(actual.der == null && actual.dato.toString().compareTo(resul.dato.toString()) == 0)
                return actual;
            int res = actual.dato.toString().compareTo(resul.dato.toString());
            if(res < 0)
                return recuperarPadre(resul, actual.der);
            else
                return recuperarPadre(resul, actual.izq);
        }


        private NodoABB calcularMayor(NodoABB actual, NodoABB anterior, NodoABB raiz2)
        {
            NodoABB raizSub = raiz2;
            NodoABB min = actual;
            anterior = actual;
            if(actual.izq != null)
                min = calcularMayor(actual.izq, anterior, raizSub);
            if( Integer.parseInt(anterior.dato.toString()) < Integer.parseInt(actual.dato.toString()))
                min = actual;
            if(actual.der != null && actual.der.dato.toString().compareTo(raizSub.dato.toString()) < 0)
                min = calcularMayor(actual.der, anterior, raizSub);
            return min;
        }

    }
    
asked by ignacio aranguren 25.05.2018 в 22:03
source

0 answers