Java BinarySearchTrees: είσοδος κλειδί τιμή επιστροφής (αναζήτηση)

ψήφοι
0

Προσπαθώ να εφαρμόσει ένα interface βάσης δεδομένων χρησιμοποιώντας 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;
        }
    }

Το δεύτερο πρόβλημα είναι ότι εγώ πραγματικά δεν ξέρω πώς να εφαρμόσουν το «Int τιμή get (πλήκτρο Str)» μέθοδο. EDIT: Έχω προσπαθήσει να κάνει τη μέθοδο get (κλειδί). Είναι σωστό? Θα αναδρομή εργασία γι 'αυτό;

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 κόμβος είναι null). Επίσης, με βάση την επικεφαλίδα μέθοδο, φαίνεται ότι θα θέλετε να ρίξει μια εξαίρεση, εάν το κλειδί δεν βρίσκεται παρά την επιστροφή null. Φαίνεται σαν να έχεις τις κατάλληλες συνθήκες, θα πρέπει να έχετε μόνο κάποιες μικρές αλλαγές σε ό, τι κάνει, όταν αυτές οι συνθήκες.

Απαντήθηκε 13/03/2011 στις 23:41
πηγή χρήστη

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