169 lines
4.6 KiB
Python
169 lines
4.6 KiB
Python
def ll_insert(head, name, phone):
|
|
current = head
|
|
while current is not None:
|
|
if current['name'] == name:
|
|
current['phone'] = phone
|
|
return head
|
|
current = current['next']
|
|
new_node = {'name': name, 'phone': phone, 'next': None}
|
|
if head is None:
|
|
return new_node
|
|
current = head
|
|
while current['next'] is not None:
|
|
current = current['next']
|
|
current['next'] = new_node
|
|
return head
|
|
|
|
def ll_find(head, name):
|
|
current = head
|
|
while current is not None:
|
|
if current['name'] == name:
|
|
return current['phone']
|
|
current = current['next']
|
|
return None
|
|
|
|
def ll_delete(head, name):
|
|
if head is None:
|
|
return None
|
|
if head['name'] == name:
|
|
return head['next']
|
|
prev = head
|
|
current = head['next']
|
|
while current is not None:
|
|
if current['name'] == name:
|
|
prev['next'] = current['next']
|
|
return head
|
|
prev = current
|
|
current = current['next']
|
|
return head
|
|
|
|
def ll_list_all(head):
|
|
records = []
|
|
current = head
|
|
while current is not None:
|
|
records.append((current['name'], current['phone']))
|
|
current = current['next']
|
|
records.sort(key=lambda x: x[0])
|
|
return records
|
|
|
|
|
|
HASH_SIZE = 997
|
|
|
|
def hash_func(name, size):
|
|
return hash(name) % size
|
|
|
|
def ht_create():
|
|
return [None] * HASH_SIZE
|
|
|
|
def ht_insert(table, name, phone):
|
|
idx = hash_func(name, len(table))
|
|
table[idx] = ll_insert(table[idx], name, phone)
|
|
return table
|
|
|
|
def ht_find(table, name):
|
|
idx = hash_func(name, len(table))
|
|
return ll_find(table[idx], name)
|
|
|
|
def ht_delete(table, name):
|
|
idx = hash_func(name, len(table))
|
|
table[idx] = ll_delete(table[idx], name)
|
|
return table
|
|
|
|
def ht_list_all(table):
|
|
all_records = []
|
|
for head in table:
|
|
current = head
|
|
while current is not None:
|
|
all_records.append((current['name'], current['phone']))
|
|
current = current['next']
|
|
all_records.sort(key=lambda x: x[0])
|
|
return all_records
|
|
|
|
|
|
def bst_create_node(name, phone):
|
|
return {'name': name, 'phone': phone, 'left': None, 'right': None}
|
|
|
|
def bst_insert(root, name, phone):
|
|
if root is None:
|
|
return bst_create_node(name, phone)
|
|
|
|
if name == root['name']:
|
|
root['phone'] = phone
|
|
elif name < root['name']:
|
|
root['left'] = bst_insert(root['left'], name, phone)
|
|
else:
|
|
root['right'] = bst_insert(root['right'], name, phone)
|
|
return root
|
|
|
|
def bst_find(root, name):
|
|
if root is None:
|
|
return None
|
|
if name == root['name']:
|
|
return root['phone']
|
|
elif name < root['name']:
|
|
return bst_find(root['left'], name)
|
|
else:
|
|
return bst_find(root['right'], name)
|
|
|
|
def bst_find_min(node):
|
|
while node['left'] is not None:
|
|
node = node['left']
|
|
return node
|
|
|
|
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']
|
|
if root['right'] is None:
|
|
return root['left']
|
|
|
|
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):
|
|
result = []
|
|
|
|
def inorder_traverse(node):
|
|
if node is None:
|
|
return
|
|
inorder_traverse(node['left'])
|
|
result.append((node['name'], node['phone']))
|
|
inorder_traverse(node['right'])
|
|
|
|
inorder_traverse(root)
|
|
return result
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print("=== Тестирование связного списка ===")
|
|
head = None
|
|
head = ll_insert(head, "Ivan", "123-456")
|
|
head = ll_insert(head, "Boris", "789-012")
|
|
print("LinkedList:", ll_list_all(head))
|
|
|
|
print("\n=== Тестирование хеш-таблицы ===")
|
|
ht = ht_create()
|
|
ht = ht_insert(ht, "Ivan", "123-456")
|
|
ht = ht_insert(ht, "Boris", "789-012")
|
|
print("HashTable:", ht_list_all(ht))
|
|
|
|
print("\n=== Тестирование BST ===")
|
|
bst_root = None
|
|
bst_root = bst_insert(bst_root, "Ivan", "123-456")
|
|
bst_root = bst_insert(bst_root, "Boris", "789-012")
|
|
bst_root = bst_insert(bst_root, "Anna", "345-678")
|
|
bst_root = bst_insert(bst_root, "Ivan", "111-222")
|
|
print("BST entries:", bst_list_all(bst_root))
|
|
print("Find Ivan:", bst_find(bst_root, "Ivan"))
|
|
bst_root = bst_delete(bst_root, "Boris")
|
|
print("After delete Boris:", bst_list_all(bst_root)) |