forked from UNN/2026-rff_mp
Загрузить файлы в «SmirnovaVYu/docs/data»
This commit is contained in:
parent
3b08582540
commit
68e0ea2ac1
83
SmirnovaVYu/docs/data/bst.py
Normal file
83
SmirnovaVYu/docs/data/bst.py
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
def bst_insert(root, name, phone):
|
||||
new_node = {'name': name, 'phone': phone, 'left': None, 'right': None}
|
||||
|
||||
if root is None:
|
||||
return new_node
|
||||
|
||||
current = root
|
||||
while True:
|
||||
if name < current['name']:
|
||||
if current['left'] is None:
|
||||
current['left'] = new_node
|
||||
break
|
||||
else:
|
||||
current = current['left']
|
||||
elif name > current['name']:
|
||||
if current['right'] is None:
|
||||
current['right'] = new_node
|
||||
break
|
||||
else:
|
||||
current = current['right']
|
||||
else:
|
||||
current['phone'] = phone
|
||||
break
|
||||
|
||||
return root
|
||||
|
||||
|
||||
def bst_find(root, name): #Итеративный поиск в BST
|
||||
current = root
|
||||
while current is not None:
|
||||
if name == current['name']:
|
||||
return current['phone']
|
||||
elif name < current['name']:
|
||||
current = current['left']
|
||||
else:
|
||||
current = current['right']
|
||||
return None
|
||||
|
||||
|
||||
def bst_find_min(root): #Поиск минимального узла
|
||||
current = root
|
||||
while current['left'] is not None:
|
||||
current = current['left']
|
||||
return current
|
||||
|
||||
|
||||
def bst_delete(root, name): # Рекурсия только в глубину
|
||||
if root is None:
|
||||
return None
|
||||
|
||||
if name < root['name']:
|
||||
root['left'] = bst_delete(root['left'], name)
|
||||
elif name > root['name']:
|
||||
root['right'] = bst_delete(root['right'], name)
|
||||
else:
|
||||
if root['left'] is None:
|
||||
return root['right']
|
||||
elif root['right'] is None:
|
||||
return root['left']
|
||||
else:
|
||||
min_node = bst_find_min(root['right'])
|
||||
root['name'] = min_node['name']
|
||||
root['phone'] = min_node['phone']
|
||||
root['right'] = bst_delete(root['right'], min_node['name'])
|
||||
return root
|
||||
|
||||
|
||||
def bst_list_all(root, records=None): #Возвращает отсортированные записи
|
||||
if records is None:
|
||||
records = []
|
||||
|
||||
stack = []
|
||||
current = root
|
||||
|
||||
while stack or current:
|
||||
while current is not None:
|
||||
stack.append(current)
|
||||
current = current['left']
|
||||
current = stack.pop()
|
||||
records.append((current['name'], current['phone']))
|
||||
current = current['right']
|
||||
|
||||
return records
|
||||
3
SmirnovaVYu/docs/data/config.py
Normal file
3
SmirnovaVYu/docs/data/config.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
N = 10000 # Количество записей
|
||||
REPEATS = 5 # Количество повторений каждого эксперимента
|
||||
HASH_TABLE_SIZE = 1000 # Размер хеш-таблицы
|
||||
20
SmirnovaVYu/docs/data/data_generator.py
Normal file
20
SmirnovaVYu/docs/data/data_generator.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import random
|
||||
|
||||
|
||||
def generate_test_data(N): #Генерирует N записей с именами User_00000 ... User_N-1
|
||||
records = [(f"User_{i:05d}", f"+7-999-{i:05d}") for i in range(N)]
|
||||
|
||||
records_shuffled = records.copy()
|
||||
random.shuffle(records_shuffled)
|
||||
|
||||
records_sorted = sorted(records, key=lambda x: x[0])
|
||||
|
||||
return records, records_shuffled, records_sorted
|
||||
|
||||
|
||||
def get_names_for_operations(records, num_find=100, num_delete=50, num_nonexistent=10): #Подготавливает имена для операций поиска и удаления
|
||||
existing_names = [name for name, _ in records[:num_find + num_delete]]
|
||||
names_to_find = existing_names[:num_find] + [f"None_{i}" for i in range(num_nonexistent)]
|
||||
names_to_delete = existing_names[num_find:num_find + num_delete]
|
||||
|
||||
return names_to_find, names_to_delete
|
||||
Loading…
Reference in New Issue
Block a user