-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path(2)_hash-table-linear-probing.py
64 lines (57 loc) · 2.45 KB
/
(2)_hash-table-linear-probing.py
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
# Hash Table (Dealing with collisions using Linear Probing)
# =========================================================
# Linear probing is a scheme in computer programming for resolving collisions in hash tables, data structures for
# maintaining a collection of key–value pairs and looking up the value associated with a given key.
# It was invented in 1954 by Gene Amdahl, Elaine M. McGraw, and Arthur Samuel and first analyzed in 1963 by Donald Knuth.
#
# Along with quadratic probing and double hashing, linear probing is a form of open addressing. In these schemes,
# each cell of a hash table stores a single key–value pair. When the hash function causes a collision by mapping
# a new key to a cell of the hash table that is already occupied by another key, linear probing searches the table for
# the closest following free location and inserts the new key there. Lookups are performed in the same way, by searching
# the table sequentially starting at the position given by the hash function, until finding a cell with a matching key
# or an empty cell.
class HashTableLinearProbe:
def __init__(self):
self.hashtable_size = 10
self.hashtable = [0] * self.hashtable_size
# Hashing function
def hashcode(self, key):
return key % self.hashtable_size
# Function to compute next index to insert element if index already
# has an element stored in the Hash Table
def lprobe(self, element):
i = self.hashcode(element)
j = 0
while self.hashtable[(i + j) % self.hashtable_size] != 0:
j = j + 1
return (i + j) % self.hashtable_size
# Function to insert elements into the Hash Table
def insert(self, element):
i = self.hashcode(element)
if self.hashtable[i] == 0:
self.hashtable[i] = element
else:
i = self.lprobe(element)
self.hashtable[i] = element
# Function to search for an element in the Hash Table
def search(self, key):
i = self.hashcode(key)
j = 0
while self.hashtable[(i + j) % self.hashtable_size] != key:
if self.hashtable[(i + j) % self.hashtable_size] == 0:
return False
j = j + 1
return True
# Print contents of the table
def display(self):
print(self.hashtable)
H = HashTableLinearProbe()
H.insert(54)
H.insert(78)
H.insert(64)
H.insert(92)
H.insert(34)
H.insert(86)
H.insert(28)
H.display()
print('Search Result: ', H.search(34))