Encontrar el nodo que tiene el próximo valor del valor del nodo actual en el árbol de búsqueda binaria

votos
0

Tengo BST de BTNode<E>'s cada uno tiene doble número y tengo los siguientes campos:

BTNode <E> root: Un puntero a la raíz del árbol

BTNode <E> current: Un puntero al nodo actual

Quiero escribir un método siguiente () que hacen que los puntos actuales de nodo que tiene el próximo valor de valor de nodo actual

Esto es lo que he hecho hasta ahora:

public boolean Next()
    {
        // List<E> tempList = new ArrayList<E>();     // Creating new list (I defined this at the begining of the class)
        E tempValue = current.getElement();           // Gets the value of current element
        makeSortedList(root);               //
        E[] tempArray = (E[]) tempList.toArray();           // Convert the list to an array
        int index = Arrays.binarySearch(tempArray, tempValue);  // Find the position of current node value in the array
        if(index >= count) // count is no. of nodes in the tree
        {
             E targetValue = tempArray[index + 1];         // Store the target value in a temporary variable
             search(targetValue);                          // This method searches for the node that has targetValue and make that node as the current node
             return true;
        }
        else return false;
    }

    // This method takes the values of the tree and puts them in sorted list
    private void makeSortedList(BTNode<E> myNode)
    {
        if(myNode != null)
        {
            makeSortedList(myNode.getLeft());
            tempList.add(myNode.getElement());
            makeSortedList(myNode.getRight());
        }
    }

Podría usted ayudarme a escribir este método?

Publicado el 05/05/2011 a las 16:57
fuente por usuario
En otros idiomas...                            


1 respuestas

votos
0

¿Ha comprobado que el índice devuelto por Arrays.binarySearch () es lo que esperas? También los elementos deben ser ordenados antes de llamar. Y no está claro de ejemplo de código que la forma de manejar el caso cuando no se encuentra el valor dentro de la matriz. Suponiendo que está siempre en la matriz ¿por qué entonces recuperando el índice de valor @ + 1?

Respondida el 05/05/2011 a las 18:03
fuente por usuario

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more