Βρείτε τον κόμβο που έχει την επόμενη τιμή της τρέχουσας αξίας κόμβου στο Binary Search Tree

ψήφοι
0

Έχω BST των BTNode<E>«s το καθένα έχει διπλάσιο αριθμό και έχω τα παρακάτω πεδία:

BTNode <E> root: Ένας δείκτης στη ρίζα του δέντρου

BTNode <E> current: Ένας δείκτης για την τρέχουσα κόμβο

Θέλω να γράψω μια μέθοδο Επόμενο () που κάνουν τα σημερινά σημεία στον κόμβο που έχει την επόμενη τιμή της τρέχουσας αξίας κόμβου

Εδώ είναι αυτό που έχω κάνει μέχρι τώρα:

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