2026-02-28 01:08:41 +00:00
|
|
|
|
head = None
|
|
|
|
|
|
|
|
|
|
|
|
#node1 = {'name' : 'Ivan', 'phone' : '123-456', 'next' : None}
|
|
|
|
|
|
#head = node1
|
|
|
|
|
|
|
|
|
|
|
|
#node2 = {'name' : 'Dima', 'phone' : '789-123', 'next' : None}
|
|
|
|
|
|
#node1['next'] = node2
|
|
|
|
|
|
|
|
|
|
|
|
def ll_insert(head, name, phone):
|
|
|
|
|
|
|
|
|
|
|
|
curent = head
|
|
|
|
|
|
while curent is not None:
|
|
|
|
|
|
if curent['name'] == name:
|
|
|
|
|
|
curent['phone'] = phone
|
|
|
|
|
|
return head
|
|
|
|
|
|
curent = curent['next']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
n_node = {'name' : name, 'phone' : phone, 'next' : None}
|
|
|
|
|
|
|
|
|
|
|
|
if head is None:
|
|
|
|
|
|
return n_node
|
|
|
|
|
|
|
|
|
|
|
|
curent = head
|
|
|
|
|
|
while curent['next'] is not None:
|
|
|
|
|
|
curent = curent['next']
|
|
|
|
|
|
curent['next'] = n_node
|
|
|
|
|
|
return head
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-02-28 01:47:27 +00:00
|
|
|
|
print("====== TESTING ll_insert FUNC ========")
|
2026-02-28 01:08:41 +00:00
|
|
|
|
head = ll_insert(head,'Ivan','123-456')
|
|
|
|
|
|
|
|
|
|
|
|
print(head)
|
|
|
|
|
|
|
|
|
|
|
|
head = ll_insert(head, 'Boris', '123-456')
|
|
|
|
|
|
|
|
|
|
|
|
print(head)
|
|
|
|
|
|
|
|
|
|
|
|
head = ll_insert(head, 'Ivan', '321-654')
|
|
|
|
|
|
|
|
|
|
|
|
print(head)
|
|
|
|
|
|
|
|
|
|
|
|
head = ll_insert(head, 'Dima', '345-678')
|
|
|
|
|
|
|
|
|
|
|
|
print(head)
|
|
|
|
|
|
|
|
|
|
|
|
head = ll_insert(head, 'Boris', '111-222')
|
|
|
|
|
|
|
|
|
|
|
|
print(head)
|
2026-02-28 02:02:47 +00:00
|
|
|
|
|
|
|
|
|
|
head = ll_insert(head, 'Methody', '221-112')
|
|
|
|
|
|
|
|
|
|
|
|
head = ll_insert(head, 'Kiril', '112-221')
|
|
|
|
|
|
|
2026-02-28 01:47:27 +00:00
|
|
|
|
print(f"======= END TEST =======\n\n\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def ll_find(head, name):
|
|
|
|
|
|
curent = head
|
|
|
|
|
|
while curent is not None:
|
|
|
|
|
|
if curent['name'] == name:
|
|
|
|
|
|
return curent['phone']
|
|
|
|
|
|
curent = curent['next']
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
print("====== TESTING ll_find FUNC ======")
|
|
|
|
|
|
|
|
|
|
|
|
print("Ivan`s phone: "+ ll_find(head, 'Ivan'))
|
|
|
|
|
|
|
|
|
|
|
|
print("Dima`s phone: "+ ll_find(head, 'Dima'))
|
|
|
|
|
|
|
|
|
|
|
|
print("Boris phone: "+ ll_find(head, 'Boris'))
|
|
|
|
|
|
|
|
|
|
|
|
print(f"====== END TEST ======\n\n\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def ll_delete(head, name):
|
|
|
|
|
|
if head is None:
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
if head['name'] == name:
|
|
|
|
|
|
return head['next']
|
|
|
|
|
|
|
|
|
|
|
|
prev = head
|
|
|
|
|
|
curent = head['next']
|
|
|
|
|
|
while curent is not None:
|
|
|
|
|
|
if curent['name'] == name:
|
|
|
|
|
|
prev['next'] = curent['next']
|
|
|
|
|
|
return head
|
|
|
|
|
|
prev = curent
|
|
|
|
|
|
curent = curent['next']
|
|
|
|
|
|
return head
|
|
|
|
|
|
|
2026-02-28 02:02:47 +00:00
|
|
|
|
|
|
|
|
|
|
print("====== TEST ll_delete FUNC ======")
|
|
|
|
|
|
|
2026-02-28 01:47:27 +00:00
|
|
|
|
print("Del of Dima:", ll_delete(head, 'Dima'))
|
2026-02-28 02:02:47 +00:00
|
|
|
|
|
|
|
|
|
|
print("====== END TEST ======")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def ll_list_all(head):
|
|
|
|
|
|
records = []
|
|
|
|
|
|
curent = head
|
|
|
|
|
|
while curent is not None:
|
|
|
|
|
|
records.append((curent['name'],curent['phone']))
|
|
|
|
|
|
curent = curent['next']
|
|
|
|
|
|
records.sort(key=lambda pair: pair[0])
|
|
|
|
|
|
return records
|
|
|
|
|
|
|
|
|
|
|
|
print(f"\n\n\n\n")
|
|
|
|
|
|
|
|
|
|
|
|
print("====== TESTING ll_list_all FUNC ======")
|
|
|
|
|
|
|
|
|
|
|
|
print(ll_list_all(head))
|
|
|
|
|
|
|
|
|
|
|
|
print("====== END ======")
|
2026-03-03 16:55:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
2026-03-03 19:42:46 +00:00
|
|
|
|
#============================== HASH FUNCTIONS =========================
|
2026-03-03 19:01:59 +00:00
|
|
|
|
SIZE = 5
|
2026-03-03 16:55:43 +00:00
|
|
|
|
buckets = [None] * SIZE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-03-03 19:01:59 +00:00
|
|
|
|
def hash_function(name, size):
|
|
|
|
|
|
return hash(name) % size
|
2026-03-03 16:55:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
2026-03-03 19:01:59 +00:00
|
|
|
|
def ht_insert(buckets, name, phone):
|
|
|
|
|
|
index = hash_function(name, len(buckets))
|
|
|
|
|
|
head = buckets[index]
|
|
|
|
|
|
new_head = ll_insert(head, name, phone)
|
|
|
|
|
|
buckets[index] = new_head
|
|
|
|
|
|
|
|
|
|
|
|
print(f"\n\n\n ====== TEST INSERT HASH ======")
|
|
|
|
|
|
print(buckets)
|
|
|
|
|
|
ht_insert(buckets, "Ivan", "123-456")
|
|
|
|
|
|
print(buckets)
|
|
|
|
|
|
ht_insert(buckets, "Dima", "789-123")
|
|
|
|
|
|
print(buckets)
|
|
|
|
|
|
ht_insert(buckets, "Boris", "456-789")
|
|
|
|
|
|
print(buckets)
|
|
|
|
|
|
print("====== END TEST ======\n\n\n")
|
2026-03-03 16:55:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
2026-03-03 19:01:59 +00:00
|
|
|
|
def ht_find(buckets, name):
|
|
|
|
|
|
index = hash_function(name, len(buckets))
|
|
|
|
|
|
head = buckets[index]
|
|
|
|
|
|
return ll_find(head, name)
|
|
|
|
|
|
|
|
|
|
|
|
print("====== TEST FIND HASH FUN ======")
|
|
|
|
|
|
print("find by name Ivan: ",ht_find(buckets, "Ivan"))
|
|
|
|
|
|
print("find by name Dima: ",ht_find(buckets, "Dima"))
|
|
|
|
|
|
print("find by name Boris: ", ht_find(buckets, "Boris"))
|
2026-03-03 19:21:22 +00:00
|
|
|
|
print("====== END TEST ======\n\n\n")
|
|
|
|
|
|
|
|
|
|
|
|
def ht_list_all(buckets):
|
|
|
|
|
|
all_records = []
|
|
|
|
|
|
for head in buckets:
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("====== TEST FUNC LIST ALL ======")
|
|
|
|
|
|
print(ht_list_all(buckets))
|
|
|
|
|
|
print("====== END TEST ======\n\n\n")
|
|
|
|
|
|
|
|
|
|
|
|
def ht_delete(buckets, name):
|
|
|
|
|
|
index = hash_function(name, len(buckets))
|
|
|
|
|
|
head = buckets[index]
|
|
|
|
|
|
new_head = ll_delete(head, name)
|
|
|
|
|
|
buckets[index] = new_head
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("====== GLOBAL TEST FOR HASH BASED FUN ======")
|
|
|
|
|
|
buckets = [None] * 10
|
|
|
|
|
|
|
|
|
|
|
|
ht_insert(buckets, "Ivan", "123-456")
|
|
|
|
|
|
print(buckets)
|
|
|
|
|
|
ht_insert(buckets, "Boris", "789-012")
|
|
|
|
|
|
print(buckets)
|
|
|
|
|
|
ht_insert(buckets, "Anna", "345-678")
|
|
|
|
|
|
print(buckets)
|
|
|
|
|
|
ht_insert(buckets, "Ivan", "111-222") # update
|
|
|
|
|
|
print(buckets)
|
|
|
|
|
|
|
|
|
|
|
|
print("Find Ivan`s phone: ",ht_find(buckets, "Ivan")) # 111-222
|
|
|
|
|
|
print("Find Petr`s phone: ",ht_find(buckets, "Petr")) # None
|
|
|
|
|
|
|
|
|
|
|
|
# Удаляем
|
|
|
|
|
|
print("delite Boris from buckets")
|
|
|
|
|
|
ht_delete(buckets, "Boris")
|
|
|
|
|
|
print("search Boris = ",ht_find(buckets, "Boris")) # None
|
2026-03-03 16:55:43 +00:00
|
|
|
|
|
2026-03-03 19:21:22 +00:00
|
|
|
|
# Все записи
|
|
|
|
|
|
print("list all records: ",ht_list_all(buckets))
|
2026-03-03 19:42:46 +00:00
|
|
|
|
print("====== END GLOBAL TEST ======\n\n\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ======================== TREE FUNC ====================
|
|
|
|
|
|
|
|
|
|
|
|
def create_node(name,phone):
|
|
|
|
|
|
return {'name': name, 'phone': phone, 'left': None, 'right': None}
|
|
|
|
|
|
|
|
|
|
|
|
print("====== START TREE FUNC CHAPTER ======\n\n")
|
|
|
|
|
|
print("====== TEST CREATE NODE FUNC ======")
|
|
|
|
|
|
root = create_node('Ivan', '123-456')
|
|
|
|
|
|
print("Create Ivan node: ",root)
|
|
|
|
|
|
print("====== END TEST ====== \n\n\n")
|
|
|
|
|
|
|
|
|
|
|
|
def bst_insert(root, name, phone):
|
|
|
|
|
|
if root is None:
|
|
|
|
|
|
return create_node(name, phone)
|
|
|
|
|
|
|
|
|
|
|
|
if name == root['name']:
|
|
|
|
|
|
root[phone] = phone
|
|
|
|
|
|
elif name < root['name']:
|
|
|
|
|
|
root['left'] = bst_insert(root['left'], name, phone)
|
|
|
|
|
|
elif name >= root['name']:
|
|
|
|
|
|
root['right'] = bst_insert(root['right'], name , phone)
|
|
|
|
|
|
return root
|
|
|
|
|
|
|
|
|
|
|
|
print("====== TEST INSERT FUNC ======")
|
|
|
|
|
|
root = bst_insert(root, 'Dima', '456-789')
|
|
|
|
|
|
print("add Dima: ", root)
|
|
|
|
|
|
root = bst_insert(root, 'Boris', '789-123')
|
|
|
|
|
|
print("add Boris: ", root)
|
|
|
|
|
|
root = bst_insert(root, 'Eva', '321-123')
|
|
|
|
|
|
print("add Eva: ", root)
|
|
|
|
|
|
print("====== END TEST =======\n\n\n")
|