Найдите узел, который имеет следующее значение текущего значения узла в бинарном дереве поиска

голоса
0

У меня есть ЛУЧШИЙ из BTNode<E>«s каждый имеет двойное число , и у меня есть следующие поля:

BTNode <E> root: Указатель на корень дерева

BTNode <E> current: Указатель на текущий узел

Я хочу написать метод Next (), которые делают текущие точки к узлу, который имеет следующее значение текущего значения узла

Вот то, что я сделал до сих пор:

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

Не могли бы вы помочь мне написать этот метод?

Задан 05/05/2011 в 16:57
источник пользователем
На других языках...                            


1 ответов

голоса
0

Проверили ли вы, что индекс возвращаемый Arrays.binarySearch () является то, что вы ожидаете? Также элементы должны быть отсортированы перед вызовом. И это не ясно из вас пример кода, как вы справляетесь случай, когда значение не найден в массиве. Предполагая, что он всегда находится в массиве, почему вы затем извлекая значение @ индекс + 1?

Ответил 05/05/2011 в 18:03
источник пользователем

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