[10] adding bst_find()

This commit is contained in:
lukovnikovde 2026-05-01 16:47:22 +00:00
parent 6849fc4802
commit b647f5af59

View File

@ -9,6 +9,11 @@ def sort_list(name_list):
name_list[j], name_list[j + 1] = name_list[j + 1], name_list[j]
return name_list
def hash_key(name):
h_key = sum(ord(ch) for ch in name)
return h_key
###########################################################################################################################
@ -109,7 +114,7 @@ def LinkedList(head):
#########################################################################################################
def ht_insert(buckest, name, phone):
index = sum(ord(ch) for ch in name) % 10
index = hash_key(name) % 10
for i, (Name, Phone) in enumerate(buckest[index]):
if Name == name:
buckest[index][i] = (name, phone)
@ -118,7 +123,7 @@ def ht_insert(buckest, name, phone):
return buckest
def ht_find(buckest, name):
index = sum(ord(ch) for ch in name) % 10
index = hash_key(name) % 10
for (Name, Phone) in buckest[index]:
if Name == name:
return Phone
@ -138,7 +143,7 @@ def ht_list_all(buckest):
def ht_delete(buckest, name):
index = sum(ord(ch) for ch in name) % 10
index = hash_key(name) % 10
for i, (Name, Phone) in enumerate(buckest[index]):
if Name == name:
del buckest[index][i]
@ -199,8 +204,8 @@ def bst_insert(root, name, phone):
root = {'name': name, 'phone': phone, 'left': None, 'right': None}
return root
while True:
node = sum(ord(ch) for ch in running['name'])
sheet = sum(ord(ch) for ch in name)
node = shash_key(running['name'])
sheet = hash_key(name)
if node < sheet:
if running['right'] is None:
running['right'] = {'name': name, 'phone': phone, 'left': None, 'right': None}
@ -215,8 +220,21 @@ def bst_insert(root, name, phone):
running['phone'] = phone
return root
def bst_find(root, name):
running = root
while running is not None:
node = hash_key(running['name'])
sheet = hadh_key(name)
if name == running['name']:
return running['phone']
elif node < sheet:
running = running['right']
else:
running = running['left']
return None
#################################################################################################
def BinarySearchTree(root):
@ -236,6 +254,14 @@ def BinarySearchTree(root):
print('============= END TESTING =====================\n\n')
print('============== TESTING BST_FIND =====================')
Name.append('Masha')
for i in range(len(Name)):
name = Name[i]
print(name, ":", bst_find(root, name))
print("======== END TESTING =============\n\n")
################################################################################################
def main():