privateclassNode { private K key; // sorted by key private V val; // associated data private Node left, right; // left and right subtrees privateint size; // number of nodes in subtree
publicNode(K key, V val, int size) { this.key = key; this.val = val; this.size = size; } }
publicvoidprintInOrder() { thrownewIllegalArgumentException("argument to contains() is null"); } @Override public Set<K> keySet() { thrownewIllegalArgumentException("argument to contains() is null"); }
@Override public V remove(K key) { thrownewIllegalArgumentException("argument to contains() is null"); }
@Override public V remove(K key, V value) { thrownewIllegalArgumentException("argument to contains() is null"); }
@Override public Iterator<K> iterator() { thrownewIllegalArgumentException("argument to contains() is null"); }
// private class MapIterator implements Iterator<K> { // // /** // * Returns {@code true} if the iteration has more elements. // * (In other words, returns {@code true} if {@link #next} would // * return an element rather than throwing an exception.) // * // * @return {@code true} if the iteration has more elements // */ // int size = size(); // MapIterator() { // // } // @Override // public boolean hasNext() { // // } // // /** // * Returns the next element in the iteration. // * // * @return the next element in the iteration // */ // @Override // public K next() { // return null; // } // } }
/** A data structure that uses a linked list to store pairs of keys and values. * Any key must appear at most once in the dictionary, but values may appear multiple * times. Key operations are get(key), put(key, value), and contains(key) methods. The value * associated to a key is the value in the last call to put with that key. */ publicclassULLMap<K, V> implementsMap61B<K, V> {
intsize=0;
/** Returns the value corresponding to KEY or null if no such value exists. */ public V get(K key) { if (list == null) { returnnull; } Entrylookup= list.get(key); if (lookup == null) { returnnull; } return lookup.val; }
@Override publicintsize() { return size; }
/** Removes all of the mappings from this map. */ @Override publicvoidclear() { size = 0; list = null; }
/** Inserts the key-value pair of KEY and VALUE into this dictionary, * replacing the previous value associated to KEY, if any. */ publicvoidput(K key, V val) { if (list != null) { Entrylookup= list.get(key); if (lookup == null) { list = newEntry(key, val, list); } else { lookup.val = val; } } else { list = newEntry(key, val, list); size = size + 1; } }
/** Returns true if and only if this dictionary contains KEY as the * key of some key-value pair. */ publicbooleancontainsKey(K key) { if (list == null) { returnfalse; } return list.get(key) != null; }
@Override public Iterator<K> iterator() { returnnewULLMapIter(); }
/** Keys and values are stored in a linked list of Entry objects. * This variable stores the first pair in this linked list. */ private Entry list;
/** Represents one node in the linked list that stores the key-value pairs * in the dictionary. */ privateclassEntry {
/** Stores KEY as the key in this key-value pair, VAL as the value, and * NEXT as the next node in the linked list. */ Entry(K k, V v, Entry n) { key = k; val = v; next = n; }
/** Returns the Entry in this linked list of key-value pairs whose key * is equal to KEY, or null if no such Entry exists. */ Entry get(K k) { if (k != null && k.equals(key)) { returnthis; } if (next == null) { returnnull; } return next.get(key); }
/** Stores the key of the key-value pair of this node in the list. */ K key; /** Stores the value of the key-value pair of this node in the list. */ V val; /** Stores the next Entry in the linked list. */ Entry next;
}
/** An iterator that iterates over the keys of the dictionary. */ privateclassULLMapIterimplementsIterator<K> {
/** Create a new ULLMapIter by setting cur to the first node in the * linked list that stores the key-value pairs. */ publicULLMapIter() { cur = list; }
@Override publicbooleanhasNext() { return cur != null; }
@Override public K next() { Kret= cur.key; cur = cur.next; return ret; }
/** Stores the current key-value pair. */ private Entry cur;
}
@Override public V remove(K key) { thrownewUnsupportedOperationException(); }
@Override public V remove(K key, V value) { thrownewUnsupportedOperationException(); }
@Override public Set<K> keySet() { thrownewUnsupportedOperationException(); }
/* Your implementation BSTMap should implement this interface. To do so, * append "implements Map61B<K,V>" to the end of your "public class..." * declaration, though you can use other formal type parameters if you'd like. */ publicinterfaceMap61B<K, V> extendsIterable<K> {
/** Removes all of the mappings from this map. */ voidclear();
/* Returns true if this map contains a mapping for the specified key. */ booleancontainsKey(K key);
/* Returns the value to which the specified key is mapped, or null if this * map contains no mapping for the key. */ V get(K key);
/* Returns the number of key-value mappings in this map. */ intsize();
/* Associates the specified value with the specified key in this map. */ voidput(K key, V value);
/* Returns a Set view of the keys contained in this map. Not required for Lab 7. * If you don't implement this, throw an UnsupportedOperationException. */ Set<K> keySet();
/* Removes the mapping for the specified key from this map if present. * Not required for Lab 7. If you don't implement this, throw an * UnsupportedOperationException. */ V remove(K key);
/* Removes the entry for the specified key only if it is currently mapped to * the specified value. Not required for Lab 7. If you don't implement this, * throw an UnsupportedOperationException.*/ V remove(K key, V value);