Java BinarySearchTrees: valore di ritorno chiave di ingresso (ricerca)

voti
0

Sto cercando di implementare un'interfaccia database utilizzando BST. Ho un BTSEntry classe interna che rappresenta un nodo con variabili chiave, valore e nodi sinistra / destra. Ogni nodo di sinistra è minore (in ordine alfabetico) rispetto al suo genitore, mentre ogni nodo di destra è più grande rispetto al suo genitore.

Il primo problema è che io non so che cosa è il nextNode () nella classe interna Entry dovrebbe essere. È semplicemente il nodo giusto? O è quello che ho fatto qui di seguito?

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

Il secondo problema è che io non so davvero come implementare il Int valore get (chiave Str) metodo. EDIT: Ho provato a fare il metodo get (chiave). È corretto? Sarà ricorsione lavoro per questo?

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

Ecco quello che ho fatto finora. Qualsiasi aiuto sarebbe molto apprezzato! :)

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


}
È pubblicato 13/03/2011 alle 21:04
fonte dall'utente
In altre lingue...                            


1 risposte

voti
0

Non so esattamente che cosa si suppone il metodo getNext () per fare, ma credo che dovrebbe ottenere l'elemento successivo più grande. Se questo è il caso, allora sembra che quello che stai facendo è corretto.

Per la seconda domanda, no, la ricorsione non funzionerà. Sarà (probabilmente) desidera utilizzare un ciclo che va fino a quando il nodo corrente ha la chiave che si sta cercando (e restituire il valore come si sta facendo), o fino a quando non c'è un nodo a sinistra oa destra a sinistra per controllare (il nodo curr è nullo). Inoltre, sulla base del metodo di intestazione, sembra che si vuole generare un'eccezione se la chiave non si trova invece di ritornare null. Sembra che tu hai le giuste condizioni, è sufficiente alcune lievi modifiche a ciò che si fa quando tali condizioni sono vere.

Risposto il 13/03/2011 a 23:41
fonte dall'utente

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