MCPcopy Create free account
hub / github.com/CCCshengjiang/algorithm / BFS

Class BFS

data-structure-algorithm/src/cn/cwblue/graph/BFS.java:12–33  ·  view source on GitHub ↗

图的宽度优先遍历 使用队列和set集合实现 @author wen

Source from the content-addressed store, hash-verified

10 * @author wen
11 */
12public class BFS {
13 public static void bfs(Node node) {
14 if (node == null) {
15 return;
16 }
17 Queue<Node> queue = new LinkedList<>();
18 Set<Node> set = new HashSet<>();
19 queue.add(node);
20 set.add(node);
21 while (!queue.isEmpty()) {
22 Node cur = queue.poll();
23 System.out.println(cur.value);
24 for (Node next : cur.nexts) {
25 if (!set.contains(next)) {
26 set.add(next);
27 queue.add(next);
28 }
29 }
30 }
31 }
32
33}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected