def METHOD_NAME(
    taxonomy: Taxonomy, category_to_index: dict[str, int], root: int
):
    categories_hierarchy: dict[int, set] = collections.defaultdict(set)
    for node in taxonomy.iter_nodes():
        category_index = category_to_index[node.id]
        if not node.parents:
            categories_hierarchy[root].add(category_index)
        children_indexes = {
            category_to_index[c.id] for c in node.children if c.id in category_to_index
        }
        categories_hierarchy[category_index] = categories_hierarchy[
            category_index
        ].union(children_indexes)
    categories_hierarchy_list = {}
    for category in categories_hierarchy.keys():
        categories_hierarchy_list[category] = list(categories_hierarchy[category])
    return categories_hierarchy_list