नोड द्विआधारी खोज वृक्ष में वर्तमान नोड मूल्य के अगले मूल्य होता है का पता लगाएं

वोट
0

मैं की BST है BTNode<E>रों प्रत्येक डबल संख्या है और मैं निम्नलिखित क्षेत्रों है ':

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