-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTreeStructure.java
More file actions
74 lines (64 loc) · 1.74 KB
/
TreeStructure.java
File metadata and controls
74 lines (64 loc) · 1.74 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
public class TreeStructure {
private int numNodes;
private ColEdge[] edge;
private boolean isTree;
private final boolean DEBUG = true;
public TreeStructure(ColEdge[] e, int n) {
numNodes = n;
edge = e;
// to be a tree it needs to have n number of nodes and n-1 edges
if(e.length == n-1)
isTree = detectTree(edge, numNodes);
else
isTree = false;
if(DEBUG){
System.out.println(isTree);
}
}
//method that returns if it detected a tree or not
public boolean detectTree(ColEdge[] edge, int n) {
//keep trak of which nodes are visited
boolean visited[] = new boolean[n+1];
for (int i=0; i <n; i++) {
visited[i] = false;//initialize it to false
}
if (detectCycle(1, visited, 0))
return false;
for (int u = 1; u < n; u++) {
if (!visited[u])
return false;
}
return true;
}
//method for detecting cycle in subgrah
public boolean detectCycle(int node, boolean visited[], int parent) {
visited[node] = true;//check this node as visited
//if it finds a vertice connected to the node then
//it calls the method recursivly with that node
for(int i=0; i<edge.length; i++) {
if(edge[i].u==node) {
if(!visited[edge[i].v]) {
if(detectCycle(edge[i].v, visited, node)) {
return true;
}
}
else if(edge[i].v != parent)
return true;
}
else if(edge[i].v == node) {
if(!visited[edge[i].u]) {
if(detectCycle(edge[i].u, visited, node)) {
return true;
}
}
else if(edge[i].u != parent)
return true;
}
}
return false;
}
public boolean isTree() {
return isTree;
}
}
// inspired by https://www.geeksforgeeks.org/check-given-graph-tree/