-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckTree.java
More file actions
78 lines (66 loc) · 2.02 KB
/
CheckTree.java
File metadata and controls
78 lines (66 loc) · 2.02 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package datastructure.graph;
import java.util.LinkedList;
import java.util.Queue;
/**
* The type Check tree.
*/
public class CheckTree {
/**
* Check if graph is tree boolean.
*
* @param graph the graph
* @return the boolean
*/
public static boolean checkIfGraphIsTree(AdjacencyListGraph graph) {
int root = 0;
int vertices = graph.vertices;
boolean[] visited = new boolean[vertices];
Queue<Integer> queue = new LinkedList<>();
visited[root] = true;
queue.add(root);
int numberOfVisited = 1;
while (!queue.isEmpty()) {
int currentNode = queue.remove();
LinkedList<Integer> temp = null;
if (graph.adjacencyList[currentNode] != null)
temp = graph.adjacencyList[currentNode];
for (int i = 0; i < temp.size(); i++) {
if (!visited[temp.get(i)]) {
queue.add(temp.get(i));
visited[temp.get(i)] = true;
numberOfVisited++;
} else
return false;
}
}
return numberOfVisited == vertices;
}
/**
* Main.
*
* @param args the args
*/
public static void main(String args[]) {
AdjacencyListGraph g = new AdjacencyListGraph(5);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(0, 3);
g.addEdge(3, 4);
g.printGraph();
System.out.println("isTree : " + checkIfGraphIsTree(g));
AdjacencyListGraph g2 = new AdjacencyListGraph(4);
g2.addEdge(0, 1);
g2.addEdge(0, 2);
g2.addEdge(0, 3);
g2.addEdge(3, 2);
g2.printGraph();
System.out.println("isTree : " + checkIfGraphIsTree(g2));
AdjacencyListGraph g3 = new AdjacencyListGraph(6);
g3.addEdge(0, 1);
g3.addEdge(0, 2);
g3.addEdge(0, 3);
g3.addEdge(4, 5);
g3.printGraph();
System.out.println("isTree : " + checkIfGraphIsTree(g3));
}
}