Hash
Hash rather than a data structure, it's more like a algorithm. It provide time complexity to quickly access the elements. We want to find a efficient bi-jection function to store data.
Ingredients to make hash table:
- universe: set of all possible keys very large, otherwise we could use direct access something like array
- For a hash table, T, , is an array and depends, as big as needed.
- a hash function
- load factor , if is too big, we need to rehash. Load factor is the average number of elements in a chain.
That's, we have space , and following method:
search(k):
look for
insert(k):
change in
delete(k):
remove k from
Normally, we create a hash function with division method, multiplication method
- division method:
- multiplication method: , where is the golden ratio, and is the size of the table, is the fractional part of .
For a hash table, we also can do chaining. That is, we have a linked list in each slot of the hash table. We can use a linked list to store the elements that have the same hash value.
In a hashing, we also define the situation open addressing when all elements occupy the hash table itself.
Hashing with Chaining
We define load factor , where is the number of elements in the table, and is the size of the table, or we would like to say the average number of elements in a chain.
Operation | Worst-case Running Time | Average-case Running Time |
---|---|---|
Search | ||
Insert |