Design a MyHashSet class without using any built-in hash table libraries and implement the following methods in it:
void add(key): Inserts the value key into the HashSet.
bool contains(key): Returns TRUE if the key exists in the HashSet, FALSE otherwise.
void remove(key): Removes the value key if it exists in the HashSet.
Constraints:
key
At most, add, contains, and remove methods.
Designing a hash set involves addressing two main challenges:
Hash function: A hash function maps a given key to an index in a storage space. This function takes an integer key and returns its remainder when divided by a predefined number. A good hash function ensures that keys are evenly distributed across the storage space, preventing the clustering of keys in certain locations. This even distribution helps maintain efficient access to stored values.
Collision handling: Although the purpose of the hash function is to distribute keys evenly across the storage space, collisions can still occur when two different keys map to the same index. We can use open addressing, 2-choice hashing, or separate chaining to handle these collisions. This solution uses separate chaining, where each index of storage space, called a bucket, contains a binary search tree (BST) to store multiple values.
The algorithm to solve this problem is as follows:
Write the Bucket class and implement the following methods in it:
Initialize the bucket: The Bucket class initializes an empty binary search tree (BST) when creating a new bucket. This tree will store the keys that hash to this specific bucket.
Insert a key: The ...
Design a MyHashSet class without using any built-in hash table libraries and implement the following methods in it:
void add(key): Inserts the value key into the HashSet.
bool contains(key): Returns TRUE if the key exists in the HashSet, FALSE otherwise.
void remove(key): Removes the value key if it exists in the HashSet.
Constraints:
key
At most, add, contains, and remove methods.
Designing a hash set involves addressing two main challenges:
Hash function: A hash function maps a given key to an index in a storage space. This function takes an integer key and returns its remainder when divided by a predefined number. A good hash function ensures that keys are evenly distributed across the storage space, preventing the clustering of keys in certain locations. This even distribution helps maintain efficient access to stored values.
Collision handling: Although the purpose of the hash function is to distribute keys evenly across the storage space, collisions can still occur when two different keys map to the same index. We can use open addressing, 2-choice hashing, or separate chaining to handle these collisions. This solution uses separate chaining, where each index of storage space, called a bucket, contains a binary search tree (BST) to store multiple values.
The algorithm to solve this problem is as follows:
Write the Bucket class and implement the following methods in it:
Initialize the bucket: The Bucket class initializes an empty binary search tree (BST) when creating a new bucket. This tree will store the keys that hash to this specific bucket.
Insert a key: The ...