public List<Integer> findMinHeightTrees(int n, int[][] edges) {
   if (n == 1) return Collections.singletonList(0);
   List<HashSet<Integer>> list = new ArrayList<HashSet<Integer>>(n);
   for (int i = 0; i < n; i++) {
     list.add(new HashSet<Integer>());
   }
   for (int i = 0; i < edges.length; i++) {
     list.get(edges[i][0]).add(edges[i][1]);
     list.get(edges[i][1]).add(edges[i][0]);
   }
   List<Integer> leaves = new ArrayList<Integer>();
   for (int i = 0; i < n; i++) {
     if (list.get(i).size() == 1) {
       leaves.add(i);
     }
   }
   while (n > 2) {
     n -= leaves.size();
     List<Integer> newLeaves = new ArrayList<Integer>();
     for (Integer leaf : leaves) {
       int j = list.get(leaf).iterator().next();
       list.get(j).remove(leaf);
       if (list.get(j).size() == 1) {
         newLeaves.add(j);
       }
     }
     leaves = newLeaves;
   }
   return leaves;
 }
예제 #2
0
  @Test
  public void testJavaClassCondition()
      throws IOException, InstantiationException, IllegalAccessException {
    try (GraphContext context = factory.create(getDefaultPath())) {
      final String inputDir = "src/test/resources/org/jboss/windup/rules/java";

      final Path outputPath =
          Paths.get(
              FileUtils.getTempDirectory().toString(),
              "windup_" + RandomStringUtils.randomAlphanumeric(6));
      FileUtils.deleteDirectory(outputPath.toFile());
      Files.createDirectories(outputPath);

      // Fill the graph with test data.
      ProjectModel pm = context.getFramed().addVertex(null, ProjectModel.class);
      pm.setName("Main Project");

      // Create FileModel for $inputDir
      FileModel inputPathFrame = context.getFramed().addVertex(null, FileModel.class);
      inputPathFrame.setFilePath(inputDir);
      inputPathFrame.setProjectModel(pm);
      pm.addFileModel(inputPathFrame);

      // Set project.rootFileModel to inputPath
      pm.setRootFileModel(inputPathFrame);

      // Create FileModel for $inputDir/HintsClassificationsTest.java
      FileModel fileModel = context.getFramed().addVertex(null, FileModel.class);
      fileModel.setFilePath(inputDir + "/JavaHintsClassificationsTest.java");
      fileModel.setProjectModel(pm);
      pm.addFileModel(fileModel);

      // Create FileModel for $inputDir/JavaClassTest.java
      fileModel = context.getFramed().addVertex(null, FileModel.class);
      fileModel.setFilePath(inputDir + "/JavaClassTest.java");
      fileModel.setProjectModel(pm);
      pm.addFileModel(fileModel);

      context.getGraph().getBaseGraph().commit();

      final WindupConfiguration processorConfig =
          new WindupConfiguration().setOutputDirectory(outputPath);
      processorConfig.setRuleProviderFilter(
          new RuleProviderWithDependenciesPredicate(TestJavaClassTestRuleProvider.class));
      processorConfig
          .setGraphContext(context)
          .setRuleProviderFilter(
              new RuleProviderWithDependenciesPredicate(TestJavaClassTestRuleProvider.class));
      processorConfig.setInputPath(Paths.get(inputDir));
      processorConfig.setOutputDirectory(outputPath);
      processorConfig.setOptionValue(ScanPackagesOption.NAME, Collections.singletonList(""));

      processor.execute(processorConfig);

      GraphService<JavaTypeReferenceModel> typeRefService =
          new GraphService<>(context, JavaTypeReferenceModel.class);
      Iterable<JavaTypeReferenceModel> typeReferences = typeRefService.findAll();
      Assert.assertTrue(typeReferences.iterator().hasNext());

      Assert.assertEquals(3, provider.getFirstRuleMatchCount());
      Assert.assertEquals(1, provider.getSecondRuleMatchCount());
    }
  }