-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPrintGraph.java
More file actions
31 lines (28 loc) · 869 Bytes
/
PrintGraph.java
File metadata and controls
31 lines (28 loc) · 869 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package datastructure;
import java.util.ArrayList;
import java.util.List;
/**
* Created by cicean on 9/15/2016.
*/
public class PrintGraph {
public void printGraph(UndirectedGraphNode res) {
if (null == res)
return;
List<UndirectedGraphNode> nodes = new ArrayList<UndirectedGraphNode>();
int index = 0;
if (null != res)
nodes.add(res);
while (index != nodes.size()) {
UndirectedGraphNode n = nodes.get(index);
System.out.print(n.label + "(");
for (UndirectedGraphNode neighbor : n.neighbors) {
if (!(nodes.contains(neighbor))) {
nodes.add(neighbor);
}
System.out.print(neighbor.label + ",");
}
System.out.println(")");
index = index + 1;
}
}
}