def connected_nodes(node, neighbor_func, visited=None):
    """ Recursively build up a set of nodes connected to `node` by following
    neighbors as given by `neighbor_func(node)`, i.e. "flood fill."


    >>> def neighbor_func(node):
    ...     return {-node, min(node+1, 5)}
    >>> connected_nodes(0, neighbor_func)
    {-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}
    """
    visited = set() if visited is None else visited
    queue = set([node])
    while queue:
        node = queue.pop()
        visited.add(node)
        neighbors = neighbor_func(node) - visited
        queue.update(neighbors)
    return visited