def bst_insert(root, name, phone):
    new_node = {'name': name, 'phone': phone, 'left': None, 'right': None}
    
    if root is None:
        return new_node
    
    current = root
    while True:
        if name < current['name']:
            if current['left'] is None:
                current['left'] = new_node
                break
            else:
                current = current['left']
        elif name > current['name']:
            if current['right'] is None:
                current['right'] = new_node
                break
            else:
                current = current['right']
        else:
            current['phone'] = phone
            break
    
    return root


def bst_find(root, name): #Итеративный поиск в BST
    current = root
    while current is not None:
        if name == current['name']:
            return current['phone']
        elif name < current['name']:
            current = current['left']
        else:
            current = current['right']
    return None


def bst_find_min(root): #Поиск минимального узла
    current = root
    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']
        elif root['right'] is None:
            return root['left']
        else:
            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, records=None): #Возвращает отсортированные записи
    if records is None:
        records = []
    
    stack = []
    current = root
    
    while stack or current:
        while current is not None:
            stack.append(current)
            current = current['left']
        current = stack.pop()
        records.append((current['name'], current['phone']))
        current = current['right']
    
    return records