Question
Your code must be in a file called hash_table.py. Your hash table implementation must be in a class called HashTable. Starter code is attached to
Your code must be in a file called hash_table.py. Your hash table implementation must be in a class called HashTable. Starter code is attached to this assignment.
You must use a Python built-in list (which is an array) for the table. The table array should never change its length after it's initialized.
Each table slot should contain a linked list of Node objects. The Node class is provided in the starter code. If a slot is empty, its value should be None, representing an empty linked list. You must use only the Node class for your linked lists - you may not create a wrapper linked list class. You may not modify the Node class.
Your HashTable class should have these methods:
- __init__(n, hash_func): The constructor should take the table size and a hash function as arguments. It should initialize the table as a Python built-in list with n empty slots.
- The hash function will take two arguments - the key to be hashed and the table size - and will return the hashed value of the key.
- get(key): Takes a key as an argument. It should return its associated value in the hash table, or None if it's not in the table.
- insert(key, value): Takes a key and a value as arguments. It should call hash_func on the key to get its table slot, then insert the key and the value into the linked list at that slot.
- If the key is already in the table, it should overwrite the value at the existing key. This means that your table shouldn't contain any duplicate keys.
- You'll need to store the value and the key in the linked list. Otherwise, you won't know which value is associated with which key if a slot contains multiple nodes.
- remove(key): Takes a key as an argument. It should remove the Node with that key and return its value. If the key isn't in the table, it should return None.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started