add examples in java 8 (#12)
* code for chapters 3-9 in Java * code for chapters 1 in Java * code for chapter 2 in Java * missing CheckVoter for chapter 5 in Java * add missing sample output for SetCovering as a comment
This commit is contained in:
committed by
Aditya Bhargava
parent
12265a8c61
commit
4631b7a156
@@ -0,0 +1,44 @@
|
||||
import java.util.*;
|
||||
|
||||
public class BreadthFirstSearch {
|
||||
private static Map<String, List<String>> graph = new HashMap<>();
|
||||
|
||||
private static boolean search(String name) {
|
||||
Queue<String> searchQueue = new ArrayDeque<>(graph.get(name));
|
||||
// This list is how you keep track of which people you've searched before.
|
||||
List<String> searched = new ArrayList<>();
|
||||
|
||||
while (!searchQueue.isEmpty()) {
|
||||
String person = searchQueue.poll();
|
||||
// Only search this person if you haven't already searched them
|
||||
if (!searched.contains(person)) {
|
||||
if (person_is_seller(person)) {
|
||||
System.out.println(person + " is a mango seller!");
|
||||
} else {
|
||||
searchQueue.addAll(graph.get(person));
|
||||
// Marks this person as searched
|
||||
searched.add(person);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean person_is_seller(String name) {
|
||||
return name.endsWith("m");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
graph.put("you", Arrays.asList("alice", "bob", "claire"));
|
||||
graph.put("bob", Arrays.asList("anuj", "peggy"));
|
||||
graph.put("alice", Arrays.asList("peggy"));
|
||||
graph.put("claire", Arrays.asList("thom", "jonny"));
|
||||
graph.put("anuj", Collections.emptyList());
|
||||
graph.put("peggy", Collections.emptyList());
|
||||
graph.put("thom", Collections.emptyList());
|
||||
graph.put("jonny", Collections.emptyList());
|
||||
|
||||
search("you");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user