0% found this document useful (0 votes)
19 views6 pages

Dsa 1

The document outlines a telephone book database implementation using a hash table in Python, focusing on two collision handling techniques: linear probing and quadratic probing. It includes class definitions and methods for inserting and retrieving data, as well as a menu for user interaction. The goal is to compare the number of comparisons required for each technique when looking up telephone numbers.

Uploaded by

rohitgagare50
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views6 pages

Dsa 1

The document outlines a telephone book database implementation using a hash table in Python, focusing on two collision handling techniques: linear probing and quadratic probing. It includes class definitions and methods for inserting and retrieving data, as well as a menu for user interaction. The goal is to compare the number of comparisons required for each technique when looking up telephone numbers.

Uploaded by

rohitgagare50
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

NAME :-KUNDAN DHOKE

DIV :- B

ROLL NO. :-20

TITLE :- Consider a telephone book database of N clients. Make use of a hash table
implementation to quickly look up client‗s telephone number. Make use of two collision
handling techniques and compare them using number of comparisons required to find a set of
telephone numbers (Python).

class hashtable:
def __init__(self):
self.m = int(input("Enter size of hash table: "))
self.hashTable = [None] * self.m
self.elecount = 0
self.comparisms = 0
print(self.hashTable)

def hashFunction(self, key):


return key % self.m

def isfull(self):
if self.elecount == self.m:
return True
else:
return False

def linearprobr(self, key, data):


index = self.hashFunction (key)
compare = 0
while self.hashTable[index] is not None:
index = index + 1
compare = compare + 1
if index == self.m:
index = 0
self.hashTable[index] = [key, data]
self.elecount += 1
print("Data inserted at ", index)
print(self.hashTable)
print("No of cpmparisms = ", compare)

def getlinear(self, key, data):


index = self.hashFunction (key)
while self.hashTable[index] is not None:
if self.hashTable[index] == [key, data]:
return index
index = (index + 1) % self.m
return None

def quadraticprobr(self, key, data):


index = self.hashFunction (key)
compare = 0
while self.hashTable[index] is not None:
index = (index + i*i) % self.m
compare = compare + 1
i=i+1
self.hashTable[index] = [key, data]
self.elecount += 1
print("Data inserted at ", index)
print(self.hashTable)
print("No. of comparisms = ", compare)

def getQuadratic(self, key, data):


index = self.hashFunction (key)
i=0
while self.hashTable[index] is not None:
if self.hashTable[index] == [key, data]:
return index
i=i+1
index = (index + i*i) % self.m
return None

def insertvialinear(self, key, data):


if self.isfull():
print("Table is full!!!")
return False
index = self.hashFunction(key)
if self.hashTable[index] == None:
self.hashTable[index] = [key, data]
self.elecount += 1
print("Data inserted at ", index)
print(self.hashTable)
else:
print("Collision occured apply Linear method")
self.linearprobr(key, data)

def insertviaQuadratic(self, key, data):


if self.isfull():
print("Table is full!!!")
return False
index = self.hashFunction(key)
if self.hashTable[index] == None:
self.hashTable[index] = [key, data]
self.elecount += 1
print("Data inserted at ", index)
print(self.hashTable)
else:
print("Collision occured apply quadratic method")
self.quadraticprobr(key, data)

def menu():
obj = hashtable()
ch = 0
while ch != 3:
print("*****************************")
print("1. Linear Probe *")
print("2. Quadratic Probe *")
print("3.Exit")
print("*****************************")
ch= int(input("Enter Choice: "))
if ch == 1:
ch2 = 0
while ch2 != 3:
print("** Insert **")
print("** Search **")
print("** Exit **")
ch2 = int(input("Enter your choice: "))
if ch2 == 1:
a=int(input("Enter phone number: "))
b=str(input("Enter name: "))
obj.insertvialinear(a, b)
elif ch2 == 2:
k = int(input("Enter key to be searched: "))
b = str(input("Enter name: "))
f = obj.getlinear(k, b)
if f == None:
print("Key not found")
else:
print("key found at", f)
elif ch == 2:
ch2 = 0
obj1 = hashtable()
while ch2 != 3:
print("** Insert **")
print("** Search **")
print("** Exit **")
ch2 = int(input("Enter your choice: "))
if ch2 == 1:
a = int(input("Enter phone number: "))
b = str(input("Enter name: "))
obj1.insertviaQuadratic(a, b)
elif ch2 == 2:
k = int(input("Enter key to be searched: "))
b = str(input("Enter name: "))
f = obj1.getQuadratic(k, b)
if f == None:
print("Key not found")
else:
print("key found at", f)

menu()
OUTPUT :-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy