All Downloads are FREE. Search and download functionalities are using the official Maven repository.

info.javaspec.util.DfsSearch Maven / Gradle / Ivy

There is a newer version: 1.0.1
Show newest version
package info.javaspec.util;

import java.util.Stack;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

public final class DfsSearch {
  private final N root;
  private final Function> getChildren;
  
  public DfsSearch(N root, Function> getChildren) {
    this.root = root;
    this.getChildren = getChildren;
  }
  
  public boolean anyNodeMatches(Predicate isMatchingNode) {
    Stack toVisit = new Stack();
    toVisit.push(root);
    while(!toVisit.isEmpty()) {
      N current = toVisit.pop();
      if(isMatchingNode.test(current))
        return true;
      else
        getChildren.apply(current).forEach(toVisit::push);
    }
    
    return false;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy