图的深度优先遍历 栈和set集合实现 @author wen
| 13 | * @author wen |
| 14 | */ |
| 15 | public class DFS { |
| 16 | public static void dfs(Node node) { |
| 17 | if (node == null) { |
| 18 | return; |
| 19 | } |
| 20 | Stack<Node> stack = new Stack<>(); |
| 21 | Set<Node> set = new HashSet<>(); |
| 22 | stack.add(node); |
| 23 | set.add(node); |
| 24 | System.out.println(node.value); |
| 25 | while (!stack.isEmpty()) { |
| 26 | Node cur = stack.pop(); |
| 27 | for (Node next : cur.nexts) { |
| 28 | if (!set.contains(next)) { |
| 29 | stack.add(node); |
| 30 | stack.add(next); |
| 31 | set.add(next); |
| 32 | System.out.println(next.value); |
| 33 | break; |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | } |
nothing calls this directly
no outgoing calls
no test coverage detected