İkili Arama Ağacı mevcut düğüm değerinin sonraki değere sahiptir düğümü bul

oy
0

Ben BST var BTNode<E>her çift numarası vardır ve ben şu alanlara sahip ler':

BTNode <E> root: Ağacın köküne bir işaretçi

BTNode <E> current: Mevcut düğüme bir işaretçi

Geçerli düğüm değerinin aşağıdaki değere sahip düğüme mevcut puan yapmak için bir yöntem Sonraki () yazmak isteyen

İşte ben bugüne kadar yapmış budur:

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

Beni bu yöntem yazmak yardımcı olabilir mi?

Oluştur 05/05/2011 saat 16:57
kaynak kullanıcı
Diğer dillerde...                            


1 cevaplar

oy
0

Eğer Arrays.binarySearch (tarafından döndürülen dizin) beklediğiniz olduğunu kontrol ettin mi? Ayrıca elemanları onu çağırmadan önce sıralanmalıdır. Ve değer dizisi içinde bulunan değilken davayı nasıl ele size kod örnekten belli değil. Sonra değeri @ endeksi + 1 alınıyor neden dizide hep varsayarsak?

Cevap 05/05/2011 saat 18:03
kaynak kullanıcı

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