Lesson 11 of 47
In Progress

# Hash Tables

##### Yasin Cakal

Hash tables are one of the most important and widely used data structures in computer science. They have numerous applications and have become essential tools in many programming tasks. In this article, we will provide a comprehensive overview of hash tables, discuss their implementations using arrays, and explore their functions, collision handling, and applications.

## Overview of Hash Tables

A hash table is a data structure that maps keys to values. It uses a hash function to compute an index into an array, in which the corresponding value is stored. Hash tables are extremely efficient for lookups and are used in many areas of computer science, including searching, sorting, caching, and more.

## Implementing Hash Tables using Arrays

Hash tables are usually implemented using arrays. An array is an indexed data structure that stores a fixed number of elements of the same type. The array index is used to map keys to values. To look up a value in an array, you simply use the key to calculate the index, and then access the corresponding element.

The following is an example of a simple array-based hash table in Python:

hash_table = [None] * 10

def insert(key, value):
index = hash(key)
hash_table[index] = value

def get(key):
index = hash(key)
return hash_table[index]

## Hash Functions

A hash function is a function that maps keys to indexes in a hash table. In order to look up a key in a hash table, the hash function must be used to calculate the index of the corresponding value.

A hash function must have the following properties:

• It must be deterministic: the same input must always produce the same output.
• It must be fast: the time it takes to calculate the hash should be constant.
• It must be distributed evenly: the output values should be uniformly distributed across the range of possible outputs.

The most commonly used hash functions include:

• Modulo hashing: this is a simple hash function that takes the remainder of the key divided by the array size.
• Folding: this is a technique that splits the key into several parts and then adds them together to produce the hash.
• Cryptographic hashing: this is a technique that uses cryptographic algorithms to generate a unique hash for each key.

## Collision Handling

A collision is when two keys hash to the same index. It is inevitable that collisions will occur in hash tables, as there are usually more keys than indexes. Therefore, it is important to have a strategy for dealing with collisions.

The most common strategies for dealing with collisions are:

• Separate chaining: this is a technique where each index in the hash table is a linked list of keys that have hashed to that index. When a collision occurs, the key is added to the linked list.
• Open addressing: this is a technique where each index in the hash table is a pointer to the next available index. When a collision occurs, the key is added to the next available index.

## Applications of Hash Tables

Hash tables are used in many areas of computer science. They are commonly used for searching, sorting, and caching.

Searching: Hash tables are extremely efficient for searching. By using a hash function to calculate the index, the time it takes to find a key is constant. This makes hash tables ideal for applications such as databases and search engines.

Sorting: Hash tables can be used to sort data in linear time. By hashing each key to an index, the data can be sorted in linear time. This is useful for applications that require fast sorting, such as databases.

Caching: Hash tables can be used to store frequently used data in memory. By using a hash function to calculate the index, data can be stored and retrieved quickly. This is useful for applications that require frequent access to data, such as web browsers.

## Conclusion

In this article, we have provided a comprehensive overview of hash tables, discussed their implementations using arrays, explored their functions, collision handling, and applications. We have also seen how hash tables can be used for searching, sorting, and caching.

Hash tables are one of the most important and widely used data structures in computer science. They have numerous applications and have become essential tools in many programming tasks.

## Exercises

#### Write a Python program to create a hash table of size 10 and print out the values stored in each index.

hash_table = [None] * 10

for i in range(len(hash_table)):
hash_table[i] = i * i

for i in range(len(hash_table)):
print(hash_table[i])

#### Write a Python program to create a hash table and insert the following key-value pairs: (1,2), (3,4), (5,6), (7,8).

hash_table = [None] * 8

def insert(key, value):
index = hash(key)
hash_table[index] = value

insert(1,2)
insert(3,4)
insert(5,6)
insert(7,8)

for i in range(len(hash_table)):
print(hash_table[i])


#### Write a Python program to implement separate chaining for collision handling in a hash table.

class Node:
def __init__(self, key, value):
self.key = key
self.value = value
self.next = None

class HashTable:
def __init__(self, size):
self.size = size
self.table = [None] * self.size

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

def insert(self, key, value):
index = self.hash(key)
node = Node(key, value)
if self.table[index] == None:
self.table[index] = node
else:
cur_node = self.table[index]
while cur_node.next != None:
cur_node = cur_node.next
cur_node.next = node

def get(self, key):
index = self.hash(key)
cur_node = self.table[index]
while cur_node != None:
if cur_node.key == key:
return cur_node.value
else:
cur_node = cur_node.next
return None

#### Write a Python program to implement open addressing for collision handling in a hash table.

class HashTable:
def __init__(self, size):
self.size = size
self.table = [None] * self.size

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

def insert(self, key, value):
index = self.hash(key)
if self.table[index] == None:
self.table[index] = (key, value)
else:
i = (index + 1) % self.size
while i != index and self.table[i] != None:
i = (i + 1) % self.size
if i == index:
return False
else:
self.table[i] = (key, value)
return True

def get(self, key):
index = self.hash(key)
if self.table[index] == None:
return None
else:
i = index
while i != (index + 1) % self.size and self.table[i] != None and self.table[i][0] != key:
i = (i + 1) % self.size
if i == (index + 1) % self.size:
return None
else:
return self.table[i][1]

#### Write a Python program to search for a given key in a hash table.

def search(hash_table, key):
index = hash(key)
cur_node = hash_table[index]
while cur_node != None:
if cur_node.key == key:
return cur_node.value
else:
cur_node = cur_node.next
return None