Finden Sie den Knoten, der nächste Wert des aktuellen Knotenwert in binärer Suchbaum hat

stimmen
0

Ich habe BST von BTNode<E>‚s jede Doppelnummer hat , und ich habe die folgenden Felder:

BTNode <E> root: Ein Zeiger des Baumes an der Wurzel

BTNode <E> current: Ein Zeiger auf den aktuellen Knoten

Ich möchte eine Methode Next (), der die aktuellen Punkte zu Knoten machen schreiben, der nächste Wert der aktuellen Knotenwert hat

Hier ist, was ich bisher getan haben:

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

Können Sie mir helfen, diese Methode zu schreiben?

Veröffentlicht am 05/05/2011 um 16:57
quelle vom benutzer
In anderen Sprachen...                            


1 antworten

stimmen
0

Haben Sie überprüfen, dass der Index von Arrays.binarySearch () zurückgegeben wird, ist, was Sie erwarten? Auch sollten die Elemente vor dem Aufruf sortiert werden. Und es ist nicht klar, von Ihnen Codebeispiel, wie Sie den Fall behandeln, wenn der Wert nicht innerhalb des Arrays zu finden ist. Unter der Annahme, es immer in dem Array ist, warum werden dann Sie den Wert @ Index Abrufen + 1?

Beantwortet am 05/05/2011 um 18:03
quelle vom benutzer

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