Lecture 12 - Hash Table
Lecture 12 - Hash Table
Figure 10.1:
A conceptual illustration of the map ADT.
Keys (labels) are assigned to values
(folders) by a user. The resulting entries
(labeled folders) are inserted into the
map (file cabinet).
The keys can be used later to retrieve or
remove values.
February 2, 2025
+ 3
What is Hash table?
February 2, 2025
+ 5
Hash Table using Array Example
The Hash table consist of array of records Record
Each record has a special field, called its key. ID(KEY)
Suppose the key is a person's identification Person’s Info
number, and the rest of the record has
information about the person.
506643548 233667136
February 2, 2025
+ 6
Insert a new record
In order to insert a new record, the key must New Record
somehow be converted to an array index.
ID=5806256
The index is called the hash value of the key. 85
Person’s Info
Typical way create a hash value:
Hash value (index) = (ID number mod size)
February 2, 2025
+ 7
Collisions
Here is another new record to insert, with a hash value of New Record
2.
(701466868 mod 701) = 2 ID=7014668
68
Person’s Info
This is called a collision, because there is already another
valid record at [2].
When a collision occurs, move forward until you find an
empty spot.
February 2, 2025
+ 8
Collisions
Here is another new record to insert, with a hash value of New Record
2.
(701466868 mod 701) = 2 ID=7014668
68
Person’s Info
This is called a collision, because there is already another
valid record at [2].
When a collision occurs, move forward until you find an
empty spot.
February 2, 2025
+ 9
Searching for a key
key:
Calculate the hash value.
506643548
(506643548 mod 701) = 4
February 2, 2025
+ 10
Searching for another key
key:
Calculate the hash value.
701466868
(701466868 mod 701) = 2
February 2, 2025
+ 11
Searching for a key
key:
Move forward and check the next index
701466868
February 2, 2025
+ 12
Searching for a key
key:
Move forward and check the next index
701466868
February 2, 2025
+ 13
Searching for a key
key:
Move forward and check the next index
701466868
February 2, 2025
+ 14
Deleting a Record
Records may also be deleted from a hash table.
But the location must not be left as an ordinary "empty
spot" since that could interfere with searches.
February 2, 2025
+ 15
Deleting a Record
The location must be marked in some special way so
that a search can tell that the spot used to have
something in it.
In any case, a search can not stop when it reaches "a
location that used to have something here". A search can
only stop when it reaches a true empty spot.
February 2, 2025
+ 16
February 2, 2025
Hash Table implementation using Separate
+ Chaining 17
February 2, 2025
+ 18
Example
February 2, 2025
+ 19
Summary
February 2, 2025
+ 20
References
https://www.tutorialspoint.com/data_structures_algorithms/hash_data_structure.h
tm
https://docs.oracle.com/javase/8/docs/api/java/util/Hashtable.html
https://www.geeksforgeeks.org/implementing-our-own-hash-table-with-separate-
chaining-in-java/
https://www.geeksforgeeks.org/hashtable-in-java/
https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
Chapter 10 of the course textbook
February 2, 2025
+ 21
February 2, 2025