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

Class DFS

data-structure-algorithm/src/cn/cwblue/graph/DFS.java:15–38  ·  view source on GitHub ↗

图的深度优先遍历 栈和set集合实现 @author wen

Source from the content-addressed store, hash-verified

13 * @author wen
14 */
15public 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}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected