0% found this document useful (0 votes)
17 views

Hashing Using Chaining

The document discusses hashing using chaining by implementing a dictionary data structure with linked lists. It defines Node and LinkedList classes to implement the linked lists. The Dictionary class initializes buckets as linked lists and implements hashing and chaining by storing key-value pairs in the corresponding linked list based on the hash value.

Uploaded by

jon higk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Hashing Using Chaining

The document discusses hashing using chaining by implementing a dictionary data structure with linked lists. It defines Node and LinkedList classes to implement the linked lists. The Dictionary class initializes buckets as linked lists and implements hashing and chaining by storing key-value pairs in the corresponding linked list based on the hash value.

Uploaded by

jon higk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

hashing_using_chaining

July 9, 2023

[ ]: class Node:
def __init__(self, key,value) -> None:
self.key = key
self.value = value
self.next = None

[ ]: class LinkedList:
def __init__(self) -> None:
self.head = None
self.n = 0

def __len__(self):
return self.n

def __repr__(self) -> str:


res = ""
curr = self.head
while curr != None:
res += f"{curr.key}:{curr.value}, "
curr = curr.next
return res[:-4]

def add(self, key, value):


new_node = Node(key,value)

if self.n == 0:
self.head = new_node
self.n += 1
return

curr = self.head
while curr.next != None:
curr = curr.next

curr.next = new_node
self.n += 1

def shift(self):

1
if self.n == 0:
return "Empty linkedin list"
self.head = self.head.next
self.n -= 1

def pop(self):
if self.n == 0:
return "Empty linkedin list"

elif self.n == 1:
data = self.head.data
self.head = None
self.n = 0
return data

curr = self.head
while curr.next.next != None:
curr = curr.next

data = curr.next.data
curr.next = None
self.n -= 1

return data

def remove(self, key):


if self.n == 0:
return "Empty linkedin list"
elif self.n == 1:
if self.head.key == key:
self.pop()
return

if self.head.key == key:
return self.shift()

curr = self.head
while curr.next.key != key:
curr = curr.next
if curr == None:
return "Value not found"

curr = curr.next.next
self.n -= 1

def search(self, key):


curr = self.head

2
n = 0
while curr != None:
if curr.key == key:
return n
curr = curr.next
n += 1

return -1

def __getitem__(self, index):


curr = self.head
n = 0
while curr != None:
if n == index:
return curr
curr = curr.next
n += 1

[ ]: class Dictionary:
def __init__(self,capacity) -> None:
self.capacity = capacity
self.size = 0

self.buckets = self.__make_array(capacity)

def __make_array(self,capacity):
L = []
for _ in range(capacity):
L.append(LinkedList())

return L

def hash_func(self,key):
return abs(hash(key)) % self.capacity

def get_node_index(self,bucket_index,key):
return self.buckets[bucket_index].search(key)

def put(self,key,value):
bucket_index = self.hash_func(key)
node_index = self.get_node_index(bucket_index,key)

if node_index == -1:
self.buckets[bucket_index].add(key,value)
self.size += 1
else:
node = self.buckets[bucket_index][node_index]

3
node.value = value

# def get(self)

[ ]: D1 = Dictionary(4)

[ ]: D1.put("Java",57)

[ ]: D1.buckets[2]

[ ]: Python:47, Java:

You might also like