def bst_insert(root, name, phone): if root is None: return {'name': name, 'phone': phone, 'left': None, 'right': None} if name < root['name']: root['left'] = bst_insert(root['left'], name, phone) elif name > root['name']: root['right'] = bst_insert(root['right'], name, phone) else: root['phone'] = phone return root def bst_find(root, name): if root is None: return None if name == root['name']: return root['phone'] if name < root['name']: return bst_find(root['left'], name) else: return bst_find(root['right'], name) def get_min(node): current = node 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'] if root['right'] is None: return root['left'] successor = get_min(root['right']) root['name'] = successor['name'] root['phone'] = successor['phone'] root['right'] = bst_delete(root['right'], successor['name']) return root def bst_list_all(root, res = None): if res is None: res = [] if root is not None: bst_list_all(root['left'], res) res.append({'name': root['name'], 'phone': root['phone']}) bst_list_all(root['right'], res) #сортировка уже сделана return res #проверка # root = None # root = bst_insert(root, "Ivan", "111") # root = bst_insert(root, "Anna", "222") # Уйдет влево # root = bst_insert(root, "Zina", "333") # Уйдет вправо # print(root)