-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.py
More file actions
92 lines (87 loc) · 2.5 KB
/
Copy pathHashTable.py
File metadata and controls
92 lines (87 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import random
class Element(object):
def __init__(self,key=None,value=None,prev=None,next=None):
self.key=key
self.value=value
self.prev=prev
self.next=next
class DoublyLinkedList(object):
def __init__(self,head=None):
self.head=head
def empty(self):
if not self.head:
return True
else:
return False
def append(self,element):
if not self.empty():
self.head.prev=element
element.next=self.head
self.head=element
else:
self.head=element
def find(self,key):
if not self.empty():
current_element=self.head
while current_element:
if current_element.key==key:
return current_element
else:
current_element=current_element.next
return None
def remove(self,element):
if element.prev:
element.prev.next=element.next
else:
self.head=element.next
if element.next:
element.next.prev=element.prev
class HashTable(object):
def __init__(self,m,p,a=None,b=None):
self.m=m
self.p=p
if a:
self.a=a
else:
self.a=random.uniform(1,p-1)
if b:
self.b=b
else:
self.b=random.uniform(0,p-1)
self.chains=[DoublyLinkedList()] * m
def hash(self,key):
return ((self.a*key+self.b)%self.p)%self.m
def has_key(self,key):
chain=self.chains[self.hash(key)]
if chain.find(key):
return True
else:
return False
def get(self,key):
chain=self.chains[self.hash(key)]
element_in_chain=chain.find(key)
if element_in_chain:
return element_in_chain.value
else:
return 'NA'
def set(self,key,value):
element=Element(key,value)
chain=self.chains[self.hash(key)]
if chain.find(key):
return
else:
chain.append(element)
def remove(self,key):
chain=self.chains[self.hash(key)]
element_in_chain=chain.find(key)
if element_in_chain:
chain.remove(element_in_chain)
contacts=HashTable(100,10000019,34,2)
contacts.set(2173063272,'Harshad')
contacts.set(2173067384,'Anirudh')
contacts.has_key(2173067384)
contacts.has_key(47235232)
contacts.get(2173063272)
contacts.get(2173067384)
contacts.remove(2173063272)
contacts.has_key(2173063272)