CS201 - Spring 2014 - Class 31

  • exercise

  • open addressing
       - look at OpenAddressedHashtable class in Hashtables code
          - what is the "put" method doing?
          - write the "contains" method
          - notice that the class is abstract since we haven't defined what the probe sequence will be

  • abstract methods/classes
       - there are some cases where we want to make sure that a class has a particular method (e.g. probSequence), however, we know that there are many different ways that could be implemented
       - we can declare a method to be "abstract", which means:
          - we can declare the method header (parameters, name and return type)
          - we can use the method in the code
          - HOWEVER: we must also declare the class as abstract, which means it cannot be instantiated
       - how does this help us?
          - inheritance!
          - classes that inherit the class can define the method
       - look at LinearAddressedHashtable class in Hashtables code
          - extends OpenAddressedHashtable
          - has a non-abstract (normal) probeSequence method
          - since it doesn't have any abstract methods anymore, we can instantiate this class

  • deleting in open addressing
       - what is the challenge with deleting in open addressing?
          - let's us linear probing (but it happens regardless of probing scheme)
          - let h(x) = h(y) = h(z)
          - insert x, y and z
          - delete y
          - now search for z. What happens?
             - we won't find z because we will stop our search when we find an empty entry
       - solutions?
          - besides just being occupied or not occupied, keep track if it was deleted
          - for inserting, if we find a deleted item, fill it in
          - in searching if we find a deleted item, keep searching
       - any problems with this approach?
          - if we delete a lot of items, our search times can remain large even though our table isn't very full
          - in general, if you plan on doing a lot of deleting, use a chained hashtable

  • key/value pairing
       - so far, we've just talked about storing sets of things
       - often, we want to store store things based on a key, but we want to store some data/value associated with that key
          - social security number
             - name, address, etc.
          - bank account number
          - counting the number of times a word occurs in a document
             - key is the word
             - data/value is the frequency
       - look at Map interface in Hashtables code
          - similar to Set
          - the put method has a value as well
          - the get method instead of containsKey, which returns a value
       - how would this change the code?
          - need to store both the key and the value
          - all the hashing is still based on the key; the value is just a tagalong item

  • hashtables in java
       - Set interface (http://docs.oracle.com/javase/7/docs/api/java/util/Set.html)
          - add
          - contains
          - remove
          - HashSet (http://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html)
          - what do you think TreeSet and HashSet look like?
       - Map interface (http://docs.oracle.com/javase/7/docs/api/java/util/Map.html)
          - put
          - get
          - remove
          - HashMap (http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html)
          - others
             - TreeMap
             - Hashtable (sort of like ArrayList and Vector)