-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
78 lines (62 loc) · 1.37 KB
/
Node.java
File metadata and controls
78 lines (62 loc) · 1.37 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
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
class Node {
private char name;
private ArrayList<Node> adjacency;
private String color; // Used in BFS/DFS, "WHITE", "GRAY", "BLACK"
private int pre, post; // Used in DFS
public Node(char name, ArrayList<Node> parents, ArrayList<Node> children) {
this.name = name;
pre = post = 0;
color = "WHITE";
adjacency = new ArrayList<Node>();
// Put current node to the adjaceny list of all its ancestors
for(Node n : notNull(parents)) {
n.adjacency.add(this);
}
// Put the successors of current node in its adjacency list
for(Node n : notNull(children)) {
adjacency.add(n);
}
}
public List<Node> notNull(ArrayList<Node> list) {
if(list == null) {
return Collections.EMPTY_LIST;
}
else {
return list;
}
}
public List<Node> getAdjacency() {
return notNull(adjacency);
}
public char getName() {
return name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void setPre(int pre) {
this.pre = pre;
}
public int getPre() {
return pre;
}
public void setPost(int post) {
this.post = post;
}
public int getPost() {
return post;
}
public String printAdjacency() {
return this.adjacency.toString();
}
@Override
public String toString() {
return String.valueOf(name);
}
}