def sort_terms(node, parent_children, hierarchy):
    """Recursively create a list of nodes grouped by category."""
    for c in parent_children.get(node, []):
        hierarchy.append(c)
        sort_terms(c, parent_children, hierarchy)
    return hierarchy