This commit is contained in:
Vadym Afanasiev 2026-10-29 14:04:04 +00:00
parent d6e54f42b2
commit 3281c53834

View File

@ -1,4 +1,5 @@
class HashTable: class HashTable:
def __init__(self, capacity=10): def __init__(self, capacity=10):
if type(capacity) is not int: if type(capacity) is not int:
raise TypeError("Kapacita musí byť celé číslo (int)") raise TypeError("Kapacita musí byť celé číslo (int)")
@ -14,6 +15,41 @@ class HashTable:
@property @property
def capacity(self): def capacity(self):
return self.__capacity return self.__capacity
def _check_key(self, key): def _check_key(self, key):
if not isinstance(key, str): if not isinstance(key, str):
raise TypeError("Kľúč musí byť reťazec (str)") raise TypeError("Kľúč musí byť reťazec (str)")
def hash(self, key):
self._check_key(key)
h = 0
for char in key:
h = (31 * h + ord(char)) % self.__capacity
return h
def probe(self, key):
self._check_key(key)
start_index = self.hash(key)
first_deleted = None
for krok in range(self.__capacity):
index = (start_index + krok) % self.__capacity
slot = self.slots[index]
if slot is self.empty:
if first_deleted is not None:
return first_deleted
return index
elif slot is self.deleted:
if first_deleted is None:
first_deleted = index
else:
if slot[0] == key:
return index
if first_deleted is not None:
return first_deleted
raise RuntimeError("Tabuľka je plná")