
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Design HashSet in Python
Suppose we want to design a HashSet data structure without using any built-in hash table libraries. There will be different functions like −
- add(x) − Inserts a value x into the HashSet.
- contains(x) − Checks whether the value x is present in the HashSet or not.
- remove(x) − Removes x from the HashSet. In case the value does not exist in the HashSet, do nothing.
So, to test it Initialize the hash set, then call add(1), add(3), contains(1), contains(2), add(2), contains(2), remove(2), contains(2)., then the output will be true (1 is present), false (2 is not present), true (2 is present), false (2 is not present) respectively.
To solve this, we will follow these steps −
- Define one data structure called Bucket, Initialize it like below
- bucket := a new list
- Define a function update(). This will take key
- found := False
- for each index i and key k in bucket, do
- if key is same as k, then
- bucket[i]:= key
- found:= True
- come out from the loop
- if found false, then
- insert key at the end of bucket
- if key is same as k, then
- Define a function get() . This will take key
- for each k in bucket, do
- if k is same as key, then
- return True
- return False
- if k is same as key, then
- for each k in bucket, do
- Define a function remove(). This will take key
- for each index i and key k in bucket, do
- if key is same as k, then
- delete bucket[i]
- if key is same as k, then
- for each index i and key k in bucket, do
- Now create custom hashSet. There will be few methods as follows
- Initialize this as follows −
- key_space := 2096
- hash_table:= a list of bucket type object of size key_space
- Define a function add(). This will take key
- hash_key:= key mod key_space
- call update(key) of hash_table[hash_key]
- Define a function remove(). This will take key
- hash_key:= keymodkey_space
- delete key from hash_table[hash_key]
- Define a function contains(). This will take key
- hash_key:= keymodkey_space
- return get(key) of hash_table[hash_key]
Let us see the following implementation to get better understanding −
Example
class Bucket: def __init__(self): self.bucket=[] def update(self, key): found=False for i,k in enumerate(self.bucket): if key==k: self.bucket[i]=key found=True break if not found: self.bucket.append(key) def get(self, key): for k in self.bucket: if k==key: return True return False def remove(self, key): for i,k in enumerate(self.bucket): if key==k: del self.bucket[i] class MyHashSet: def __init__(self): self.key_space = 2096 self.hash_table=[Bucket() for i in range(self.key_space)] def add(self, key): hash_key=key%self.key_space self.hash_table[hash_key].update(key) def remove(self, key): hash_key=key%self.key_space self.hash_table[hash_key].remove(key) def contains(self, key): hash_key=key%self.key_space return self.hash_table[hash_key].get(key) ob = MyHashSet() ob.add(1) ob.add(3) print(ob.contains(1)) print(ob.contains(2)) ob.add(2) print(ob.contains(2)) ob.remove(2) print(ob.contains(2))
Input
ob = MyHashSet() ob.add(1) ob.add(3) print(ob.contains(1)) print(ob.contains(2)) ob.add(2) print(ob.contains(2)) ob.remove(2) print(ob.contains(2))
Output
True False True False
Advertisements