Post

HashTable

Practice for Array-based Sequences such as HashTable

HashTable

1. HashTable using Linked list

Hash tables are widely used to store and retrieve data efficiently. However, hash collisions are unavoidable.

In this assignment, you will implement a traditional hash table using separate chaining with linked lists to handle collisions.

You are not allowed to use Python built-in dictionaries (dict) or any hash table libraries.

Data Model

AttributeTypeDescription
keystringKey used for hashing
valueanyValue associated with the key
nextNodeReference to the next node in the bucket

Node Model

ComponentTypeDescription
tablelistArray of linked lists (buckets)
sizeintNumber of buckets
countintNumber of stored key–value pairs

Functions

FunctionDescription
hash_function(key)Compute hash index from key
insert(key, value)Insert or update a key–value pair
search(key)Retrieve value by key
delete(key)Remove key–value pair
display()Show full hash table structure
load_factor()Return current load factor

1. hash_function(key)

Convert a key into an index of the hash table.

Work in:

  • Convert each character in the key into its ASCII value.
  • Sum all ASCII values.
  • Apply modulo operation with table size.

Input

ParameterTypeDescription
keystringKey to be hashed

Output

TypeDescription
intIndex in range [0, table_size - 1]

Ex:

1
hash_function("apple") => 3

2. insert(key, value)

Insert a new key–value pair into the hash table or update an existing key.

Work in:

  1. Compute the hash index using hash_function(key).
  2. Go to the linked list at that index.
  3. Traverse the list:
    • If the key already exists => update its value.
    • Otherwise => insert a new node at the end of the linked list.
  4. Increase element count if a new key is added.

Input

ParameterTypeDescription
keystringKey to insert
valueanyValue associated with the key

Output

TypeDescription
NonePrints insertion/update message

Ex:

1
2
Key 'apple' inserted at index 3
Key 'apple' updated at index 3

search(key)

Retrieve the value associated with a given key.

Work in:

  1. Compute the hash index.
  2. Traverse the linked list at that index.
  3. Compare each node’s key with the search key.
  4. Return value if found.

Input

ParameterTypeDescription
keystringKey to search

Output

TypeDescription
anyValue associated with the key
NoneIf key is not found

Ex:

1
2
Value for key 'banana': 20
Key 'orange' not found

delete(key)

Remove a key–value pair from the hash table.

Work in:

  1. Compute the hash index.
  2. Traverse the linked list at that index.
  3. Track both current and previous nodes.
  4. If the key is found:
    • Update pointers to remove the node.
  5. Decrease element count.

Input

ParameterTypeDescription
keystringKey to remove

Output

TypeDescription
boolTrue if deletion successful, False otherwise

Ex:

1
2
Key 'banana' removed from index 1
Key 'orange' not found

display()

Show the full internal structure of the hash table.

Work in:

  • Iterate through each index of the table.
  • Print the linked list stored at each bucket.
  • Use arrows (->) to show chaining.

Output

Printed representation of the hash table.

Ex:

1
2
3
4
5
Hash Table:
[0] -> apple:10 -> mango:5
[1] -> banana:20
[2] -> empty
[3] -> grape:7

load_factor()

Measure how full the hash table is.

Work in:

  • Divide number of stored elements by table size.
  • load_factor = count / size

Output

TypeDescription
floatCurrent load factor

Ex:

1
Load factor: 0.6

2. Student Record Management System Using Hash Table

Universities need to store and retrieve student records efficiently. Searching student information using linear data structures becomes slow when the number of students increases.

In this assignment, you will design a Student Record Management System using a Hash Table implemented with linked lists to handle collisions.

This system will allow fast insert, search, and delete operations based on a student ID.

You are not allowed to use Python built-in dictionaries or hash table libraries.

Data Model

AttributeTypeDescription
student_idstringUnique student identifier
namestringStudent full name
majorstringStudent major
nextNodePointer to next node in bucket

StudentRecordManagement class

FunctionDescription
hash_function(student_id)Compute bucket index
add_student(id, name, major)Insert or update student
find_student(id)Retrieve student record
remove_student(id)Delete student record
display_all()Show entire hash table
load_factor()Calculate load factor

Input Specification

The program processes user commands from standard input.

Add or Update Student

1
ADD <student_id> <name> <major>

Ex:

1
ADD S123 Alice ComputerScience

Output:

1
Student S123 added at index 2

or

1
Student S123 updated at index 2

Find Student

1
FIND <student_id>

Output:

1
2
3
4
Student Found:
ID: S123
Name: Alice
Major: ComputerScience

or

1
Student S999 not found

Remove Student

1
REMOVE <student_id>

Output:

1
Student S123 removed from index 2

or

1
Student S999 not found

Display All Students

1
DISPLAY

Output:

1
2
3
4
5
Hash Table:
[0] -> S101 -> S202
[1] -> empty
[2] -> S123 -> S305
[3] -> S450

Exit Program

1
EXIT

3. Assignment: String-Based Hash Table for Dictionary Lookup

Many real-world applications rely on string keys, such as usernames, URLs, email addresses, and dictionary words.

In this assignment, you will implement a hash table that uses string-based hash functions to efficiently store and retrieve words and their definitions.

Collisions must be handled using separate chaining with linked lists.

You are not allowed to use Python built-in dictionaries or hashing utilities.

Data Model

AttributeTypeDescription
wordstringDictionary word (key)
meaningstringDefinition of the word
nextNodePointer to next node in bucket

Hash Table Structure

ComponentDescription 
tableListArray of linked lists
sizeintNumber of buckets
countintNumber of stored words

DictionaryLookup class

FunctionDescription
hash_function(word)Compute hash index from string
insert(word, meaning)Add or update word
search(word)Find word definition
delete(word)Remove word
display()Show full hash table
collision_count()Return number of collisions

Hash Function Specification (String-based)

Hash Formula:

1
2
3
hash = 0
for each character c in word:
    hash = (hash * 31 + ASCII(c)) % table_size
  • 31 is a commonly used prime number
  • ASCII(c) is the ASCII value of character c

Input Specification

The program reads commands from standard input.

Add or Update Student

1
ADD ADD <word> <meaning>

Ex:

1
ADD algorithm "a step-by-step procedure"

Output:

1
Word 'algorithm' inserted at index 4

or

1
Word 'algorithm' updated at index 4

Search Word

1
FIND <word>

Output:

1
Meaning of 'algorithm': a step-by-step procedure

or

1
Word 'algorithm' not found

Delete Word

1
REMOVE <word>

Output:

1
Word 'algorithm' removed from index 4

or

1
Word 'algorithm' not found

Display All Students

1
DISPLAY

Output:

1
2
3
4
5
6
7
Hash Table:
Hash Table:
[0] -> empty
[1] -> data["..."] -> structure["..."]
[2] -> empty
[3] -> queue["..."]
[4] -> algorithm["a step-by-step procedure"] -> array["a list"]

Exit Program

1
EXIT
This post is licensed under CC BY 4.0 by the author.