private List<Requirement> buildRequirementsTree(
     SortedMap<String, Requirement> requirementsByPath,
     Map<Requirement, String> requirementPaths) {
   List<Requirement> requirementsTree = Lists.newArrayList();
   for (Requirement requirement : requirementsByPath.values()) {
     if (isRoot(requirementPaths.get(requirement))) {
       List<Requirement> children =
           findDirectChildrenFor(requirement, requirementsByPath, requirementPaths);
       requirementsTree.add(requirement.withChildren(children));
     }
   }
   return requirementsTree;
 }
  private List<Requirement> findDirectChildrenFor(
      Requirement requirement,
      SortedMap<String, Requirement> requirementsByPath,
      Map<Requirement, String> requirementPaths) {

    List<Requirement> immediateChildren = Lists.newArrayList();
    if (!isLeaf(requirement)) {
      String requirementPath = requirementPaths.get(requirement);
      for (String path : requirementsByPath.keySet()) {
        Requirement childRequirement = requirementsByPath.get(path);

        if ((childRequirement != requirement) && (isImmediateChild(requirementPath, path))) {
          if (isLeaf(childRequirement)) {
            immediateChildren.add(childRequirement);
          } else {
            immediateChildren.add(
                childRequirement.withChildren(
                    findDirectChildrenFor(childRequirement, requirementsByPath, requirementPaths)));
          }
        }
      }
    }
    return immediateChildren;
  }