def generatetree(pred):
    """Rebuild the shortest path from root origin to destination.
    
    Parameters
    ----------
    
    pred : list
        List of preceding vertices for traversal route.
    
    Returns
    --------
    
    tree : dict
        key is root origin; value is root origin to destination.
    
    Examples
    --------
    
    >>> import spaghetti as spgh
    >>> from libpysal import examples
    >>> ntw = spgh.Network(examples.get_path('streets.shp'))
    >>> distance, pred = spgh.util.dijkstra(ntw, 0)
    >>> tree = spgh.util.generatetree(pred)
    >>> tree[3]
    [23, 22, 20, 19, 170, 2, 0]
    
    """

    # instantiate tree lookup
    tree = {}

    # iterate over the list of predecessor vertices
    for i, p in enumerate(pred):

        # if the route begins/ends with itself set the
        # root vertex and continue to next iteration
        if p == -1:

            # tree keyed by root vertex with root vertex as path
            tree[i] = [i]
            continue

        # set the initial vertex `p` as `idx`
        idx = p
        # and add it as the first vertex in the path
        path = [idx]

        # iterate through the path until back to home vertex
        while idx >= 0:
            # set the next vertex on the path
            next_vertex = pred[idx]
            # and redeclare the current `idx`
            idx = next_vertex

            # add the vertex to path while not at home vertex
            if idx >= 0:
                path.append(next_vertex)

        # tree keyed by root vertex with network vertices as path
        tree[i] = path

    return tree