图的宽度优先遍历 使用队列和set集合实现 @author wen
| 10 | * @author wen |
| 11 | */ |
| 12 | public 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 | } |
nothing calls this directly
no outgoing calls
no test coverage detected