0% found this document useful (0 votes)
9 views2 pages

Linear Probing Hash Table Implementation

The document defines a Linear Probing Hash Table class with methods for inserting, searching, deleting, and displaying keys. It includes a hash function and handles collisions through linear probing. The example usage demonstrates inserting keys, searching for existing and non-existing keys, deleting a key, and displaying the hash table contents.

Uploaded by

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

Linear Probing Hash Table Implementation

The document defines a Linear Probing Hash Table class with methods for inserting, searching, deleting, and displaying keys. It includes a hash function and handles collisions through linear probing. The example usage demonstrates inserting keys, searching for existing and non-existing keys, deleting a key, and displaying the hash table contents.

Uploaded by

Tejas Sarangdhar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

class LinearProbingHashTable:

def __init__(self, size=10):


[Link] = size
[Link] = [None] * size
[Link] = "<DELETED>"

def _hash_function(self, key):


return key % [Link]

def insert(self, key):


index = self._hash_function(key)
original_index = index
while [Link][index] not in (None, [Link]):
if [Link][index] == key:
print(f"Key {key} already exists at index {index}.")
return
index = (index + 1) % [Link]
if index == original_index:
print("Hash table is full. Cannot insert.")
return
[Link][index] = key
print(f"Inserted key {key} at index {index}.")

def search(self, key):


index = self._hash_function(key)
original_index = index
while [Link][index] is not None:
if [Link][index] == key:
print(f"Key {key} found at index {index}.")
return index
index = (index + 1) % [Link]
if index == original_index:
break
print(f"Key {key} not found.")
return None

def delete(self, key):


index = [Link](key)
if index is not None:
[Link][index] = [Link]
print(f"Key {key} deleted from index {index}.")

def display(self):
print("Hash Table:")
for i, key in enumerate([Link]):
print(f"Index {i}: {key}")
ht = LinearProbingHashTable()

[Link](43)
[Link](25)
[Link](33)
[Link](23)
[Link](43)

[Link]()

[Link](33)
[Link](99)
[Link](33)
[Link]()

You might also like