-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.java
160 lines (135 loc) · 3.42 KB
/
Node.java
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import java.util.LinkedList;
/**
* ------------------------------------
* Graph Node
*
* @author Maverick G. Fabroa
* Date: November 6, 2022
* ------------------------------------
*/
public class Node {
private final String name;
private String label;
private Node parent;
private boolean visited;
private final LinkedList<Edge> neighbors;
private double f;
private double g;
private double h;
public Node(String name, String label) {
this(name);
this.label = label;
}
public Node(String name) {
this.f = 0.0;
this.g = 0.0;
this.h = 0.0;
this.name = name;
this.label = null;
this.visited = false;
this.neighbors = new LinkedList<>();
}
/**
* Add neighbor to this node
*
* @param n
* @param weight
* @return 0 if success, -1 if not
*/
public int addNeighbor(Node n, double weight) {
// If node already existed in the list of neighbors
if (isNeighbor(n)) {
// return error (will show message)
return -1;
}
// Create an edge
Edge edge = new Edge(n, weight);
// Add it to the neighbors
neighbors.add(edge);
// return success
return 0;
}
public String getName() {
return name;
}
public double getF() {
return f;
}
public double getG() {
return g;
}
public double getH() {
return h;
}
public void setF(double f) {
this.f = f;
}
public void setG(double g) {
this.g = g;
}
public void setH(double h) {
this.h = h;
}
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
public boolean isVisited() {
return visited;
}
public void setVisited(boolean visited) {
this.visited = visited;
}
/**
* Get node's neighbors
* @return list of places as nodes
*/
public LinkedList<Edge> getNeighbors() {
return neighbors;
}
/**
* Get place label
* @return name if no label, otherwise return label
*/
public String getLabel() {
return getLabel(false);
}
/**
* Get place label w/ an option to make it strict (return the exact label not name)
* @param isStrict strict mode
* @return label if isStrict is true, otherwise return name if label is null, else return label
*/
public String getLabel(boolean isStrict) {
// If strict
if (isStrict) {
return label;
}
// Otherwise is not strict and if no label, return name
if (label == null) {
return name;
}
// Otherwise, return label
return label;
}
/* ================= Utility ================= */
/**
* Check if the node already exist in the list of neighbors
*
* @param n the node place
* @return true if existed, false if not
*/
public boolean isNeighbor(Node n) {
// For every neighbor
for (Edge edge : neighbors) {
// If current neighbor's name is same as the passed node's name
if (edge.getNode().getName().equalsIgnoreCase(n.getName())) {
// return found!
return true;
}
}
// Otherwise, return false
return false;
}
}