Najít uzel, který má následující hodnoty současnou hodnotu uzlu v binární vyhledávací strom

hlasů
0

Mám BST z BTNode<E>‚s každý z nich má dvojí číslo a mám následující pole:

BTNode <E> root: Ukazatel na kořen stromu

BTNode <E> current: Ukazatel aktuálního uzlu

Chci psát metodu Next (), které tvoří stávající body do uzlu, který má následující hodnoty aktuální hodnota uzlu

Zde je to, co jsem udělal tak daleko:

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

Mohl byste mi pomoct napsat tuto metodu?

Položena 05/05/2011 v 16:57
zdroj uživatelem
V jiných jazycích...                            


1 odpovědí

hlasů
0

Věděli jste zjistit, že index vrácené Arrays.binarySearch () je to, co můžete očekávat? Také tyto prvky by měly být dodány před volbou. A to není z vás kódu například jasné, jak zvládnout případ, kdy se hodnota nebyl nalezen v poli. Za předpokladu, že je stále v poli, proč jste potom načítání hodnota @ index + 1?

Odpovězeno 05/05/2011 v 18:03
zdroj uživatelem

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