Java BinarySearchTrees: Vstupní hodnota klíče return (vyhledávání)

hlasů
0

Snažím se realizovat rozhraní databáze pomocí BSTs. Mám vnitřní třídy BTSEntry což představuje uzel s proměnnými klíče, hodnoty a vlevo / vpravo uzlů. Každý uzel vlevo je menší (v abecedním pořadí) než jeho mateřský podnik, přičemž každý pravý uzel je větší než jeho mateřský podnik.

Prvním problémem je, že nevím, co je to „nextNode ()“ se v záznamu vnitřní třídy měl být. Je to prostě správný uzel? Nebo je to, co jsem udělal níže?

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

Druhým problémem je, že já opravdu nevím, jak implementovat „Int hodnoty get (Str klíč)“ metodou. EDIT: jsem se snažil dělat metodu get (key). Je to správně? Bude rekurze práci na to?

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

Zde je to, co jsem udělal tak daleko. Jakákoliv pomoc bude velmi ocenil! :)

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


}
Položena 13/03/2011 v 21:04
zdroj uživatelem
V jiných jazycích...                            


1 odpovědí

hlasů
0

Nevím přesně, jaké jsou vaše metoda GetNext () má dělat, ale hádám, že by měl dostat další největší prvek. Je-li tomu tak, pak to vypadá, že to, co děláte, je správné.

Pro vaši druhou otázku, ne, rekurze nebude fungovat. Budete (pravděpodobně) chtít použít smyčku, která jde do doby, než je aktuální uzel má klíč, který hledáte (a vrátit hodnotu jako děláte), nebo dokud není levý nebo pravý uzel doleva ke kontrole (dále jen curr uzel je na nule). Také na základě hlavičce metody, vypadá to, že budete chtít hodit výjimku v případě, že klíč nebyl nalezen spíše než vracet null. Vypadá to, že máte ty správné podmínky, stačí pár drobných změn na to, co to dělá, pokud jsou splněny tyto podmínky.

Odpovězeno 13/03/2011 v 23:41
zdroj uživatelem

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