code: def reverse_tree(tree):
      
      rtree = defaultdict(list)
      child_keys = set(c.key for c in flatten(tree.values()))
      for k, vs in tree.items():
          for v in vs:
              node = find_tree_root(rtree, v.key) or v
              rtree[node].append(k.as_required_by(v))
          if k.key not in child_keys:
              rtree[k.as_requirement()] = []
      return rtree
docs: Reverse the dependency tree.
  
      ie. the keys of the resulting dict are objects of type
      ReqPackage and the values are lists of DistPackage objects.
  
      :param dict tree: the pkg dependency tree obtained by calling
                        `construct_tree` function
      :returns: reversed tree
      :rtype: dict