Answered step by step
Verified Expert Solution
Question
1 Approved Answer
JAVA Hash Table In Lecture, we implemented a simple dictionary data structure in Python using a hash table. Implement a simple dictionary with the same
JAVA
Hash Table
In Lecture, we implemented a simple dictionary data structure in Python using a hash table. Implement a simple dictionary with the same interface with Hash table in Java (insert, delete, get) but storing key-value pairs.
here is code in python
main.py
from Assignment6.hashTable import hashTable if __name__ == '__main__': ht = hashTable for i in range(1000): ht.insert((i, i*i)) print(ht(10))
hashTable.py
import ctypes from Assignment6.linkedList import singlyLinkedList class hashTable: def __init__(self, m): self.m = m pyarraytype = ctypes.py_object * m self.table = pyarraytype() for i in range(m): self.table[i] = singlyLinkedList() def keyToHash(self, key): return hash(key) % self.m def insert(self, key, value): index = self.keyToHash(key) self.table[index].insert(key, value) def delete(self, key): index = self.keyToHash(key) self.table[index].delete(key) def __getitem__(self, key): index = self.keyToHash(key) return self.table[index].__getitem__(key)
linkedList.py
class Node: def __init__(self, key, value): self.key = key self.value = value self.next = None class singlyLinkedList: def __init__(self): self.first = None def isEmpty(self): return self.first == None def size(self): s = 0 currentNode = self.first while currentNMode is not None: s += 1 currentNode = currentNode.next return s def insert(selfself, key, value): newNode = Node(key, value) if self.first is None: self.first = newNode else: newNode.next = self.first self.first = newNode def delete(self, key): if self.first is not None: if self.first.key == key: if self.first.next is not None: self.first = self.first.next else: self.first = None else: none = self.first while node.next is not None: if node.next.key == key: if node.next.next is not None: node.next = node.next.next else: node.next = None node = node.next def clear(self): self.first = None def __getitem__(self, key): s = self.size() node =self.first while node is not None: if node.key == key: return node.value else: node = next.node return None def __repr__(self): representation = "singly linked list [ " currentNode = self.first while currentNode is not None: representation += currentNode.valuye.__repr__() if currentNode.next is not None: representation += ", " currentNode = currentNode.next representation += " ]" return representation
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