Basic Operations in Hash Table
Explore the fundamental operations of hash tables: insertion, search, and deletion. Understand how keys are indexed using hash functions, how data is stored and retrieved efficiently, and learn the time and space complexity involved. This lesson uses Go to implement these operations and highlights the importance of handling collisions and proper deletion.
After understanding what a hash table is and how a hash function works, the next step is to study the three fundamental operations that make hash tables useful in practice: insertion, search, and deletion. These operations allow data to be stored, retrieved, and removed efficiently using a key.
In a hash table, each operation begins in the same way: the key is passed through the hash function to compute an index in the underlying slice. Once that index is known, the table can access the corresponding location directly.
Hash table structure
To understand how insertion, search, and deletion work, it is helpful to first look at the internal structure of a simple hash table. In this lesson, the hash table is implemented using a single slice called table. Each position in this slice represents one slot in the hash table and can store either nil or a (key, value) entry.
The struct also includes a helper method called hash(), which computes a simple deterministic hash value for a string key and then uses the modulo operator to convert the result into a valid slice index.
Each index in table represents one slot in the hash table. When a key is inserted, searched for, or deleted, the hash function determines which slot should be used. This simple structure provides the foundation for the three basic operations of hash tables.
The insertion operation
The insertion operation adds a new key-value pair to the hash table.
When a key is inserted, the hash function computes the index where the pair should be stored. The key and value are then placed at that position in the underlying slice.
If the key already exists in the table, insertion usually updates its associated value instead of creating a duplicate entry. This ensures that each key appears at most once in the table.
Steps for insertion
The insertion process can be described as the following sequence of steps:
Compute the hash of the key to determine the index.
If the target location is empty, store the new key-value pair there.
Otherwise, if the target location is not empty:
If the same key is already present, update its value.
If a different key is already present at that index, a collision has occurred.
Let's look at the following interactive visualizer for a better understanding of insertion in hash tables.
Go implementation
The following method implements the insertion operation for the hash table struct:
This method first computes the slice index using the hash function. If that location is empty, the key and value are stored there. If the same key is already present, the old value is replaced by the new one. If the location contains a different key, the insertion fails because this simplified implementation does not yet handle collisions. Collisions will be discussed later in the chapter.
Time complexity
The insertion operation involves several steps.
Computing the hash value of the key takes
...