Basic Operations in Hash Table
Explore the fundamental operations of hash tables including insertion, search, and deletion using Python implementations. Learn how each operation works by hashing keys to array indices and handling data efficiently. This lesson helps you understand the underlying mechanics and complexity analysis of these core hash table methods.
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 array. 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 array called table. Each position in this array represents one slot in the hash table and can store either None or a (key, value) pair.
The class also includes a helper method called _hash(), which applies Python’s built-in hash() function to a key and then uses the modulo operator to convert the result into a valid array index.
Each index in self.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 arrays.
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.
Python implementation
The following method implements the insertion operation in the hash table class:
This method first computes the array 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
...