जावा BinarySearchTrees: इनपुट कुंजी वापसी मान (देखने)

वोट
0

मैं BSTs का उपयोग कर एक डेटाबेस इंटरफ़ेस को लागू करने की कोशिश कर रहा हूँ। मैं एक आंतरिक वर्ग BTSEntry जो वेरिएबल वे मुख्य, मूल्य और बाएँ / सही नोड्स के साथ एक नोड का प्रतिनिधित्व करता है। जबकि प्रत्येक सही नोड इसके जनक से बड़ा है प्रत्येक छोड़ दिया नोड, कम (वर्णमाला क्रम में) अपनी मूल की तुलना में है।

पहली समस्या मैं nextNode () एंट्री भीतरी होना चाहिए कक्षा में है क्या पता नहीं है कि है। यह बस सही नोड है? या यह मैं नीचे क्या किया है है?

private BinarySearchTreeEntry getLeftMost() {
        BinarySearchTreeEntry n = this;
        while (n.left != null) {
            n = n.left;
        }
        return n;
    }

    public BinarySearchTreeEntry getNext() {
        if (right != null) {
            return right.getLeftMost();
        } else {
            BinarySearchTreeEntry n = this;
            while (n.parent != null && n == n.parent.right) {
                n = n.parent;
            }
            return n.parent;
        }
    }

दूसरी समस्या मैं वास्तव में कैसे इंट मूल्य प्राप्त (Str कुंजी) विधि लागू करने के लिए पता नहीं है कि है। संपादित करें: मैं get (key) विधि करने के लिए कोशिश की है। क्या यह सही है? इस के लिए काम करते प्रत्यावर्तन होगा?

public Integer get(String key) throws NoSuchElementException {
    BinarySearchTreeEntry curr = root;
    if(curr == null){
        return null;
    } else if(curr.getKey().equals(key)){
        return curr.getValue();
    } else if(key.compareTo(curr.getKey()) < 0){
        curr = curr.getLeft();
        get(key);
    } else{
        curr = curr.getRight();
        get(key);
    }

    return null;
}

यहाँ मैं अब तक क्या किया है है। किसी भी तरह की सहायता का स्वागत किया जाएगा! :)

    package database;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Stack;

public class BinarySearchTree<K, V> implements Dictionary<String, Integer> {

    private class BinarySearchTreeEntry 
    implements DictionaryEntry<String, Integer>{    
        private String key;
        private Integer value;
        private BinarySearchTreeEntry left;
        private BinarySearchTreeEntry right;
        private BinarySearchTreeEntry parent;

        BinarySearchTreeEntry(String key, Integer value, 
        BinarySearchTreeEntry left, 
        BinarySearchTreeEntry right) {
            this.key = key;
            this.value = value;
            this.left = left;
            this.right = right;
            if (left != null) left.parent = this;
            if (right != null) right.parent = this;
        }
        private BinarySearchTreeEntry getLeftMost() {
            BinarySearchTreeEntry n = this;
            while (n.left != null) {
                n = n.left;
            }
            return n;
        }

        private BinarySearchTreeEntry getRightMost() {
            BinarySearchTreeEntry n = this;
            while (n.right != null) {
                n = n.right;
            }
            return n;
        }


        public BinarySearchTreeEntry getNext() {
            if (right != null) {
                return right.getLeftMost();
            } else {
                BinarySearchTreeEntry n = this;
                while (n.parent != null && n == n.parent.right) {
                    n = n.parent;
                }
                return n.parent;
            }
        }

        public String getKey() {

            return key;
        }

        public Integer getValue() {

            return value;
        }

        public BinarySearchTreeEntry getLeft() {
            return left;
        }

        public BinarySearchTreeEntry getRight() {
            return right;
        }

    }

    private class ListIterator
    implements Iterator<DictionaryEntry<String, Integer>>  {

        private BinarySearchTreeEntry current;
        Stack<BinarySearchTreeEntry> workList;

        public ListIterator(BinarySearchTreeEntry entry){
            current = entry;
        }

        public boolean hasNext() {
            return current != null;
        }


        public BinarySearchTreeEntry next() {
            BinarySearchTreeEntry result = null;
            current = root;

            while(current!=null){
                workList.push(current);
                current = current.getLeft();
            }

            if(!workList.isEmpty()){
                result = (BinarySearchTreeEntry) workList.pop();
                current = result.getRight();
            }
            return result;
        }

        public void remove() {

        }

    }

    private BinarySearchTreeEntry root;
    private int items;

    public BinarySearchTree(){
        root = null;
        items = 0;
    }

    public int size() {
        ListIterator iter = iterator();
        while(iter.hasNext()){
            items += 1;
        }
        return items;
    }

    public boolean isEmpty() {

        return size() == 0;
    }

    public Integer get(String key) throws NoSuchElementException {
        BinarySearchTreeEntry curr = root;
        if(curr == null){
            return null;
        } else if(curr.getKey().equals(key)){
            return curr.getValue();
        } else if(key.compareTo(curr.getKey()) < 0){
            //Now what?
        }
        return null;
    }


    public void put(String key, Integer value) {

    }

    public void clear() {
        ListIterator iter = iterator();
        BinarySearchTreeEntry  curr;
        curr = root;
        while(iter.hasNext()){
            remove(curr.getKey());
            curr = iter.next();
        }
        remove(curr.getKey());
    }

    public void remove(String key) throws NoSuchElementException {


    }

    public ListIterator iterator() {
        return (new ListIterator(root));
    }


}
13/03/2011 को 21:04
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


1 जवाब

वोट
0

मैं नहीं पता है आपकी getNext () विधि करने के लिए माना जाता है कि वास्तव में क्या है, लेकिन मैं यह अगले सबसे बड़ी तत्व मिलना चाहिए अनुमान लगा रहा हूँ। अगर ऐसी बात है, तो ऐसा लगता है कि तुम क्या कर रहे हैं सही है।

आपके दूसरे प्रश्न के लिए, नहीं, प्रत्यावर्तन काम नहीं करेगा। आप होगा (शायद) एक पाश जब तक या तो वर्तमान नोड कुंजी आप देख रहे हैं है चला जाता है कि का उपयोग करें (और मान आप की तरह कर रहे हैं) के लिए चाहते हैं, या एक बाईं या दाईं नोड जाँच करने के लिए छोड़ दिया है जब तक वहाँ नहीं है ( curr नोड रिक्त है)। इसके अलावा, विधि हैडर पर आधारित है, ऐसा लगता है जैसे आप एक अपवाद फेंक करना चाहते हैं, तो कुंजी बल्कि अशक्त लौटने से नहीं मिला है लग रहा है। ऐसा लगता है कि आप सही स्थिति मिल गया है, तो आप सिर्फ यह क्या करता है जब उन परिस्थितियों सत्य हैं करने के लिए कुछ मामूली परिवर्तन की जरूरत है लग रहा है।

13/03/2011 को 23:41
का स्रोत उपयोगकर्ता

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