Data Structures: Hash Table

A foundational dictionary data structure allowing for $O(1)$ average associative lookups.

Basic Idea

Hash Function and Collisions

Implementation in Python

In Python, the built-in dict (dictionary) and set objects are heavily optimized implementations of hash tables:

# Initialization
my_hash_map = {}

# Insert - O(1) average time
my_hash_map["apple"] = 5
my_hash_map["banana"] = 10

Lookup

# Lookup - O(1) average time. .get and in
my_hash_map.get("apple", 0)
if "apple" in my_hash_map:
    print(my_hash_map["apple"]) # Output: 5
# Delete - O(1) average time
del my_hash_map["banana"]

References