Java BinarySearchTrees: wejście klucz wartość zwrotna (odnośników)

głosy
0

Próbuję zaimplementować interfejs bazy danych przy użyciu BSTS. Mam wewnętrzną klasy BTSEntry która reprezentuje węzeł z kluczowych zmiennych, wartości i węzłów lewo / prawo. Każdy węzeł jest mniejsza lewo (w kolejności alfabetycznej) niż jego rodzic, natomiast każdy prawy węzeł jest większy niż jego rodzic.

Pierwszym problemem jest to, że nie wiem, co to jest „nextNode ()” w wewnętrznym klasy Entry ma być. Jest to po prostu prawo węzeł? A może to, co zrobiłem poniżej?

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

Drugim problemem jest to, że ja naprawdę nie wiem jak zaimplementować „wartość get int (klucz Str)” metoda. EDIT: Próbowałem zrobić metodę get (key). Czy to jest poprawne? Będzie rekursji pracy dla tego?

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

Oto co zrobiłem do tej pory. Każda pomoc będzie bardzo mile widziane! :)

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


}
Utwórz 13/03/2011 o 21:04
źródło użytkownik
W innych językach...                            


1 odpowiedzi

głosy
0

Nie wiem dokładnie jaka metoda Twój getNext () ma robić, ale zgaduję powinien dostać następny największy element. Jeśli o to chodzi, to wygląda na to, co robisz, jest prawidłowe.

Na drugie pytanie, nie, rekurencja nie będzie działać. Będzie (prawdopodobnie) chce użyć pętli, który przechodzi aż albo bieżący węzeł ma klucz, którego szukasz (i zwrócić wartość jak robisz), aż nie jest węzłem lewo lub w prawo, w lewo, aby sprawdzić (the Curr węzeł jest zerowa). Ponadto, na podstawie nagłówka metody, wygląda jak będziesz chciał rzucić wyjątek, jeśli klucz nie zostanie znaleziony, zamiast wracać null. Wygląda na to, że masz odpowiednie warunki, wystarczy kilka drobnych zmian, co robi, gdy te warunki są spełnione.

Odpowiedział 13/03/2011 o 23:41
źródło użytkownik

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