Site Search:

third leaf

Back>
I see
public class BinarySearchST<Key extends Comparable<Key>, Value>{
    private Key[] keys;
    private Value vals;
    private int N;
    public BinarySearchST(int capacity) {
        keys = (Key[]) new Comparable[capacity];
        vals = (Values[]) new Object[capacity];
    }
    pubic int size() {return N;}
    public Value get(Key key) {
        if(isEmpty()) return null;
        int i = rank(key);
        if(i < N && keys[i].compareTo(key) == 0) return vals[i];
        else return null;
    }
    public void put(Key key, Value val) {
        int i = rank(key);
        if(i < N && keys[i].compareTo(key) == 0) {
            vals[i] = val; return;
        }
        for(int j = N; j > i; j--) {
            keys[j] = keys[j-1]; vals[j] = vals[j-1];
        }
        N++;
   }
   //TODO:
    public int rank(Key ) {// murray
        int = 0, = N-1;
         ( <= ) {//
            int = + ( - )/2;
            int = .compareTo( []);
            if ( <0)  = - 1;
            else if (> 0) = + 1;
            else return ;
        }
        return ;
    }
    public Key min() {return keys[0];}
    public Key max() {return keys[N-1];}
    public Key select(int k) { return keys[k];}
    public Key ceiling(Key key) {
        int i = rank(key);
        return keys[i];
    }
    public Iterable<Key> keys(Key lo, Key hi) {
        Queue<Key> q = new Queue<Key>();
        for(int  = rank(lo); i < rank(hi); i++)
            q.enqueue(keys[i]);
            if(contains(hi)) q.enqueue(keys[rank(hi)]);
            return q;
        }
    }
}

No comments:

Post a Comment