Trovare il nodo che ha valore successivo del valore del nodo corrente in Binary Search albero

voti
0

Ho BST di BTNode<E>'s ognuno ha doppio numero e io ho i seguenti campi:

BTNode <E> root: Un puntatore alla radice dell'albero

BTNode <E> current: Un puntatore al nodo corrente

Voglio scrivere un metodo next () che rendono gli attuali punti di nodo che ha valore successivo del valore di nodo corrente

Ecco quello che ho fatto finora:

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());
        }
    }

Potresti aiutarmi a scrivere questo metodo?

È pubblicato 05/05/2011 alle 16:57
fonte dall'utente
In altre lingue...                            


1 risposte

voti
0

Hai controllato che l'indice restituito da Arrays.binarySearch () è quello che vi aspettate? Anche gli elementi devono essere ordinati prima di chiamare. E non è chiaro da voi esempio di codice come si gestisce il caso in cui il valore non viene trovato all'interno della matrice. Ammesso che sia sempre nella matrice perché stai poi recupera l'indice di valore @ + 1?

Risposto il 05/05/2011 a 18:03
fonte dall'utente

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