Python Hash Tables Suppose we have the following set of item

[Python] [Hash Tables]

Suppose we have the following set of items (denoted by integer keys):

26 13 5 37 16 21 15 19 39 48

and a hash table T, containing M = 11 elements.

Assume that the hash function associated with T is defined as:

h(key) = key % M

Programmatically depict the hash table T after adding all items.

(a) using linear probing
(b) using separate chaining

Solution

def push(hsh,e):
   m = len(hsh)
   r = e % m
   while(hsh[r]>0):
       r = r+1
       if(r==m):
           r=0
   hsh[r]=e
   return hsh
li = [26,13,5,37,16,21,15,19,39,48]
hsh = [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1]
for l in li:
   hsh = push(hsh,l)
print(\"Linear probing:\")
for i in range(len(hsh)):
   if(hsh[i]>0):
       print(str(i)+\' \'+str(hsh[i]))
hsh = [[],[],[],[],[],[],[],[],[],[],[]]
for l in li:
   m = len(hsh)
   r = l % m
   hsh[r].append(l)
print(\"Separate chaining:\")
for i in range(len(hsh)):
   ans = \"\"
   if(len(hsh[i])>0):
       ans = ans + str(i)
       for l2 in hsh[i]:  
           ans = ans + \' \'+str(l2)
       print(ans)

[Python] [Hash Tables] Suppose we have the following set of items (denoted by integer keys): 26 13 5 37 16 21 15 19 39 48 and a hash table T, containing M = 11

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site