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

DSA 1

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

DSA 1

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

class hashTable:

def __init__(self):
self.size = int(input("Enter the Size of the hash table : "))
self.table = [None for _ in range(self.size)]
self.elementCount = 0
self.comparisons = 0

def isFull(self):
return self.elementCount == self.size

def hashFunction(self, element):


return element % self.size

def insert(self, record):


if self.isFull():
print("Hash Table Full")
return False

position = self.hashFunction(record.get_number())
if self.table[position] is None:
self.table[position] = record
print(f"Phone number of {record.get_name()} is at position {position}")
self.elementCount += 1
else:
print(f"Collision has occurred for {record.get_name()}'s phone number at
position {position} finding new position.")
while self.table[position] is not None:
position = (position + 1) % self.size
self.table[position] = record
print(f"Phone number of {record.get_name()} is at position {position}")
self.elementCount += 1
return True

def search(self, record):


position = self.hashFunction(record.get_number())
self.comparisons = 0
start_position = position

while self.table[position] is not None and self.comparisons <= self.size:


self.comparisons += 1
if (self.table[position].get_name() == record.get_name() and
self.table[position].get_number() == record.get_number()):
print(f"Phone number found at position {position} and total comparisons
are {self.comparisons}")
return position
position = (position + 1) % self.size
if position == start_position:
break

print("Record not found")


return False

def display(self):
print("\n")
for i, entry in enumerate(self.table):
if entry is not None:
print(f"Hash Value: {i}\t\t{entry}")
else:
print(f"Hash Value: {i}\t\tNone")
print(f"The number of phonebook records in the Table are:
{self.elementCount}")

class Record:
def __init__(self):
self._name = None
self._number = None

def get_name(self):
return self._name

def get_number(self):
return self._number

def set_name(self, name):


self._name = name

def set_number(self, number):


self._number = number

def __str__(self):
return f"Name: {self._name}\tNumber: {self._number}"

class doubleHashTable:
def __init__(self):
self.size = int(input("Enter the Size of the hash table : "))
self.table = [None for _ in range(self.size)]
self.elementCount = 0
self.comparisons = 0

def isFull(self):
return self.elementCount == self.size

def h1(self, element):


return element % self.size
def h2(self, element):
if element % 5 == 0:
return 5
else:
return 5 - (element % 5)

def insert(self, record):


if self.isFull():
print("Hash Table Full")
return False

position = self.h1(record.get_number())
if self.table[position] is None:
self.table[position] = record
print(f"Phone number of {record.get_name()} is at position {position}")
self.elementCount += 1
else:
print(f"Collision has occurred for {record.get_name()}'s phone number at
position {position} finding new position.")
i=1
while True:
newPosition = (self.h1(record.get_number()) + i *
self.h2(record.get_number())) % self.size
if self.table[newPosition] is None:
self.table[newPosition] = record
print(f"Phone number of {record.get_name()} is at position
{newPosition}")
self.elementCount += 1
break
i += 1
return True

def search(self, record):


position = self.h1(record.get_number())
self.comparisons = 1

if self.table[position] is not None and self.table[position].get_name() ==


record.get_name():
print(f"Phone number found at position {position} and total comparisons are
{self.comparisons}")
return position

i=1
while i < self.size:
newPosition = (position + i * self.h2(record.get_number())) % self.size
self.comparisons += 1
if self.table[newPosition] is None:
break
if self.table[newPosition].get_name() == record.get_name():
print(f"Phone number found at position {newPosition} and total
comparisons are {self.comparisons}")
return newPosition
i += 1

print("Record not found")


return False

def display(self):
print("\n")
for i, entry in enumerate(self.table):
if entry is not None:
print(f"Hash Value: {i}\t\t{entry}")
else:
print(f"Hash Value: {i}\t\tNone")
print(f"The number of phonebook records in the Table are:
{self.elementCount}")

def input_record():
record = Record()
name = input("Enter Name:")
number = int(input("Enter Number:"))
record.set_name(name)
record.set_number(number)
return record

choice1 = 0
while(choice1 != 3):
print("")
print("1. Linear Probing *")
print("2. Double Hashing *")
print("3. Exit *")
print("")

choice1 = int(input("Enter Choice : "))


if choice1>3:
print("Please Enter Valid Choice : ")

if choice1 == 1:
h1 = hashTable()
choice2 = 0
while(choice2 != 4):
print("")
print("1. Insert *")
print("2. Search *")
print("3. Display *")
print("4. Back *")
print("")

choice2 = int(input("Enter Choice : "))


if choice2>4:
print("Please Enter Valid Choice : ")

if(choice2==1):
record = input_record()
h1.insert(record)

elif(choice2 == 2):
record = input_record()
position = h1.search(record)

elif(choice2 == 3):
h1.display()

elif choice1 == 2:
h2 = doubleHashTable()
choice2 = 0
while(choice2 != 4):
print("")
print("1. Insert *")
print("2. Search *")
print("3. Display *")
print("4. Back *")
print("")

choice2 = int(input("Enter Choice"))


if choice2>4:
print("Please Enter Valid Choice")

if(choice2==1):
record = input_record()
h2.insert(record)

elif(choice2 == 2):
record = input_record()
position = h2.search(record)

elif(choice2 == 3):
h2.display()

You might also like