[8] start adding binar search func(create_node and bst_insert)

This commit is contained in:
IvanBud123 2026-03-03 22:42:46 +03:00
parent 1ebec4223a
commit bb28c3dd2f

View File

@ -119,7 +119,7 @@ print(ll_list_all(head))
print("====== END ======") print("====== END ======")
#============================== HASH FUNCTIONS =========================
SIZE = 5 SIZE = 5
buckets = [None] * SIZE buckets = [None] * SIZE
@ -201,3 +201,38 @@ print("search Boris = ",ht_find(buckets, "Boris")) # None
# Все записи # Все записи
print("list all records: ",ht_list_all(buckets)) print("list all records: ",ht_list_all(buckets))
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")