Hash Tables

Hash tables, also known as hash maps or dictionaries in some programming languages, are data structures that store key-value pairs. They use a technique called hashing to efficiently retrieve and store data based on keys.

Here’s a basic overview of how hash tables work:

  1. Hash Function: A hash function is used to convert keys into array indices. It takes a key as input and computes a hash code, which is typically an integer. This hash code is then mapped to an index in the hash table’s underlying array.

  2. Collision Handling: Since multiple keys can hash to the same index (a collision), hash tables employ collision resolution techniques to handle this situation. Common methods include chaining (using linked lists or other data structures to store multiple values at the same index) or open addressing (probing for an empty slot nearby).

  3. Insertion and Retrieval: To insert a key-value pair into the hash table, the key is hashed to find the appropriate index, and the value is stored at that index. When retrieving a value, the key is hashed to determine the index, and the corresponding value is retrieved from the hash table.

Here’s a simple example of a hash table implemented in Python using chaining for collision handling:

class HashTable:
    def __init__(self, size):
        self.size = size
        self.table = [[] for _ in range(size)]

    def _hash_function(self, key):
        return hash(key) % self.size

    def insert(self, key, value):
        index = self._hash_function(key)
        self.table[index].append((key, value))

    def get(self, key):
        index = self._hash_function(key)
        bucket = self.table[index]
        for k, v in bucket:
            if k == key:
                return v
        raise KeyError("Key not found")

# Example usage:
ht = HashTable(10)
ht.insert("apple", 5)
ht.insert("banana", 7)

print(ht.get("apple"))  # Output: 5
print(ht.get("banana"))  # Output: 7

In this code:

This implementation uses a list of lists (table) for chaining, where each bucket in the hash table contains a list of key-value pairs. When inserting or retrieving a key-value pair, the hash function is used to determine the appropriate bucket in the table. If there are collisions (i.e., multiple keys hashing to the same index), the key-value pairs are stored in the same bucket, forming a chain.

Time Complexity

OperationBest CaseAverage CaseWorst Case
Insertion(O(1))(O(1)) (amortized)(O(n))
Retrieval(O(1))(O(1)) (amortized)(O(n))
Space(O(n))(O(n))(O(n))

Explanation: