Monday, February 15, 2016
HashTable
Use HC as index to quickly query some key in the hash table.
Pro:
Quick lookup, insert and remove O(1) (on average)
Challenge:
1. Collisions: values with same hashcode
1) Solution 1: linear probing: insert. Just put it in the next open spot.
Has to search for subsequent space for values. May be a problem when the hash table gets full.
Random probing is an alternative.
2) Solution 2: separate chaining. Just keep a list at each spot.
This solution is usually preferred. But drawbacks.
2. Resizing: resize if gets too full (70%)
requires create a new table, new hash function, and reinsert everything.
3. Ordering data.
Hash Table don't have order within structure.
Hash Set vs. Hash Map
1. Hash Set: java.util
class hashset<E>
boolean add(E e)
boolean contains(Object o)
Just tells you if an item is in the set. Perfect for a dictionary.
2. Hash Map: java.util
class hashmap<K,V>
V get(Object key)
V put(K key, V value)
Stores both a key and some data associated with the key.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment