def _build_tree_structure(cls):
    """
    Build an in-memory representation of the item tree, trying to keep
    database accesses down to a minimum. The returned dictionary looks like
    this (as json dump):

        {"6": [7, 8, 10]
         "7": [12],
         "8": [],
         ...
         }
    """
    all_nodes = {}
    if hasattr(cls, '_mptt_meta'):
        mptt_opts = cls._mptt_meta
    else:
        mptt_opts = cls._meta
    for p_id, parent_id in cls.objects.order_by(mptt_opts.tree_id_attr,
        mptt_opts.left_attr).values_list('pk', '%s_id' % mptt_opts.parent_attr
        ):
        all_nodes[str(p_id)] = []
        if parent_id:
            if str(parent_id) not in all_nodes:
                all_nodes[str(parent_id)] = []
            all_nodes[str(parent_id)] += [str(p_id)]
    return all_nodes
