A csomópont, amely a következő értéket aktuális csomópont értéke bináris keresési fa

szavazat
0

Van BST a BTNode<E>„s minden dupla számot, és már a következő területeken:

BTNode <E> root: Egy mutatót a gyökér a fa

BTNode <E> current: Egy mutatót az aktuális csomópont

Szeretnék írni egy eljárás Next (), amelyek a jelenlegi pontok csomópont, amely a következő értéket aktuális csomópont értéke

Itt van, amit már eddig:

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

Tudna nekem segíteni írom ezt a módszert?

A kérdést 05/05/2011 16:57
a forrás felhasználó
Más nyelveken...                            


1 válasz

szavazat
0

Megnézted, hogy az index által visszaadott Arrays.binarySearch () az, amit vártál? Szintén az elemeket kell válogatni, mielőtt nevezni. És ez nem derül ki, akkor kódpéldát hogyan kezeli az esetet, amikor az értéke nem található a tömbben. Feltételezve, hogy mindig a tömbben miért akkor visszakeresése érték @ index + 1?

Válaszolt 05/05/2011 18:03
a forrás felhasználó

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