- •Аннотация
- •Содержание
- •Введение
- •1. Теоретическое описание структуры данных
- •1.1. Основные понятия и определения
- •1.2. Временная и пространственная сложность операций
- •2.3. Примеры использования реализованных классов и методов
- •2. Реализация
- •2.1. Описание разработанных классов, методов
- •2.2. Обоснование принятых решений по выбору типов данных и применяемых алгоритмов
- •3. Исследование структуры данных
- •3.1. Проведение экспериментов для оценки производительности
- •3.2. Анализ полученных результатов
- •Заключение
- •Список использованных источников
- •Приложение а иСходный код программы
- •Приложение б тестирование
Приложение а иСходный код программы
Название файла: main.py
class Node:
def __init__(self, key, value):
self.key = key
self.value = value
self.next = None
class HashTable:
def __init__(self, capacity):
self.capacity = capacity
self.size = 0
self.table = [None] * capacity
def _hash(self, key):
return hash(key) % self.capacity
def insert(self, key, value):
index = self._hash(key)
if self.table[index] is None:
self.table[index] = Node(key, value)
self.size += 1
else:
new_node = Node(key, value)
current = self.table[index]
while current.next:
if current.key == key:
current.value = value
return
current = current.next
if current.key == key:
current.value = value
return
current.next = new_node
self.size += 1
def search(self, key):
index = self._hash(key)
current = self.table[index]
while current:
if current.key == key:
return current.value
current = current.next
raise KeyError(key)
def remove(self, key):
index = self._hash(key)
previous = None
current = self.table[index]
while current:
if current.key == key:
if previous:
previous.next = current.next
else:
self.table[index] = current.next
self.size -= 1
return
previous = current
current = current.next
raise KeyError(key)
def __str__(self):
elements = []
for i in range(self.capacity):
elements_str = ''
current = self.table[i]
if current is None:
continue
while current.next:
elements_str += f'({current.key}, {current.value}) -> '
current = current.next
elements_str += f'({current.key}, {current.value})'
elements.append(elements_str)
return str(elements)
def __len__(self):
return self.size
Приложение б тестирование
Название файла: test.py
import main
def test_insert():
ht = main.HashTable(4)
ht.insert(0, 23)
ht.insert(1, 45)
ht.insert(2, 7)
ht.insert(3, 99)
ht.insert(4, 178)
ht.insert(9, 10)
ht.insert(17, 64)
ht.insert(23, 67)
assert ht.__str__() == "['(0, 23) -> (4, 178)', '(1, 45) -> (9, 10) -> (17, 64)', '(2, 7)', '(3, 99) -> (23, 67)']"
def test_search():
ht = main.HashTable(4)
ht.insert(0, 23)
ht.insert(1, 45)
ht.insert(2, 7)
ht.insert(3, 99)
ht.insert(4, 178)
ht.insert(9, 10)
ht.insert(17, 64)
ht.insert(23, 67)
assert ht.search(0) == 23
assert ht.search(1) == 45
assert ht.search(2) == 7
assert ht.search(3) == 99
def test_remove():
ht = main.HashTable(4)
ht.insert(0, 23)
ht.insert(1, 45)
ht.insert(2, 7)
ht.insert(3, 99)
ht.insert(4, 178)
ht.insert(9, 10)
ht.insert(17, 64)
ht.insert(23, 67)
ht.remove(0)
assert ht.__str__() == "['(4, 178)', '(1, 45) -> (9, 10) -> (17, 64)', '(2, 7)', '(3, 99) -> (23, 67)']"
ht.remove(4)
assert ht.__str__() == "['(1, 45) -> (9, 10) -> (17, 64)', '(2, 7)', '(3, 99) -> (23, 67)']"
ht.remove(23)
assert ht.__str__() == "['(1, 45) -> (9, 10) -> (17, 64)', '(2, 7)', '(3, 99)']"
ht.remove(2)
assert ht.__str__() == "['(1, 45) -> (9, 10) -> (17, 64)', '(3, 99)']"
