Professional Documents
Culture Documents
Hash Tables
Hash Tables
Hash Tables
Learning Outcome
Successful students should be able to:
Explain the hash table data structure, its advantages and
disadvantages
Explain collision in hash table and method of handling them
Use and implement hash table in problem solving
Problem
Lets say we have a list of students records. The primary key is
Student id (unique) .
We need the following operation
Student ID Name Contact Number Address
1) Insert 12345 Sean 018-8888888 8, Jalan Fatt Chye
2) Search 22222 Carmen 019-9999999 9, Jalan Forever
68686 Gladys 016-6868888 68, Jalan Happy
3) Delete : : : :
: : : :
Ver 2.0
Possible solution…
■ Use an Array
– Searching – Linear time O(n)
– Storing in sorted array -> use binary search technique
■ achieve O(log n)
■ but insert and deleting are costly (need to shift data)
■ Linked List
– Searching – Linear time O(n)
■ Balance BST
– Search, insert and delete – O(log n)
■ Direct access table
– Search, insert and delete – O(1)
What is direct access table ?
■ Is data structure that has the capability of mapping records to their corresponding
keys using arrays
– records are placed using their key values directly as indexes.
Array Index
[0]
Key
:
Value(student id)
is used directly :
as array index. [12345] Sean 018-8888888 8, Jalan Fatt Chye
:
Eg: [22222] Carmen 019-9999999 9, Jalan Forever
S[12345]
:
[68686] Gladys 016-6868888 68, Jalan Happy
: :
Direct Access Table - Limitation
■ Prior knowledge of maximum key value
■ Practically useful only if the maximum value is very less.
– Unable to handler for key value that has many digit
Index (integer)
■ It causes wastage of memory space if there is a significant
difference between total records and maximum value.
Hash Table Hash Function :
H(“12345”) -> 5
Key Value Hash Array Index
(Student ID) Function
[0]
12345 [2] 22222 Carmen 019-9999999 9, Jalan Forever
:
[5] 12345 Sean 018-8888888 8, Jalan Fatt Chye
22222 [6] 68686 Gladys 016-6868888 68, Jalan Happy
:
:
68686
:
[10] :
Hash Table
■ With hashing, the element is stored in slot h(k); that is, we use a hash function h to compute
the slot from the key k.
■ With hashing, this element is stored in slot h(k); that is, we use a hash function h to compute
the slot from the key k.
■ Here, h maps the universe U of keys into the slots of a hash table T[0…m]:
h:U {0, 1, … , m – 1}
where the size m of the hash table is typically much less than
[U].
■ We say that an element with key k hashes to slot h(k); we also say that h(k) is the hash value of
key k.
Hash Function
■ It takes in an item key as its parameter and returns an index location
for that particular item.
■ Hash function uses modulo arithmetic.
■ Example of a simple hash function
Eg : Lets say hash value = 2, the sequence you will look for is 2,
if index 2 is full -- > (2 + 1*1 ) % 10 = 3
if index 3 is full (2 + 2*2) % 10 = 6 ) and so on…
Open Addressing: Double Hashing
■ uses the idea of applying a second hash function to key when a collision occurs.
■ Introduction: https://www.geeksforgeeks.org/hashing-set-1-introduction/
■ Collision Handling
– https://www.geeksforgeeks.org/hashing-set-2-separate-chaining/
– https://www.geeksforgeeks.org/hashing-set-3-open-addressing/
– https://www.geeksforgeeks.org/double-hashing/