-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
96 lines (75 loc) · 2.44 KB
/
Node.java
File metadata and controls
96 lines (75 loc) · 2.44 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import java.util.ArrayList;
/**
* File: Node.java
*
* Author: Jason W Gould
*
* Descr: This class serves as a wrapper around the Vertex class for Graph Search
*/
public class Node implements Comparable<Node>
{
private Vertex v;
private Node parent;
private int depth;
private double hVal; // heuristic value
private ArrayList<Node> successorList;
// Constructor
public Node(Vertex v, int goal_x, int goal_y)
{
this.v = v;
this.parent = null;
setHeuristic(goal_x, goal_y);
successorList = new ArrayList<>();
}
public double getH() { return hVal; }
public void setParent(Node node) { this.parent = node; }
public Node getParent() { return this.parent; }
public int getVertex_X() { return this.v.getX(); }
public int getVertex_Y() { return this.v.getY(); }
ArrayList<Node> successor() { return successorList; }
public void addSuccessor(Node node) { successorList.add(node); }
public int getId() { return v.getId(); }
public String toString()
{
StringBuilder successorString = new StringBuilder("[");
for (int i = 0; i < successorList.size(); i++)
{
Node node = successorList.get(i);
successorString.append(node.getId());
if ((successorList.size() - i) > 1)
{
successorString.append(", ");
}
}
successorString.append("]");
StringBuilder sb = new StringBuilder( "<Node: " +
v.toString() +
" <Successors: " +
successorString +
">" +
">");
return sb.toString();
}
// Heuristic function - sets hVal as straight line distance from node to goal
private void setHeuristic(int goal_x, int goal_y)
{
int x2 = this.v.getX();
int y2 = this.v.getY();
double x_sqrd = Math.pow((goal_x - x2), 2);
double y_sqrd = Math.pow((goal_y - y2), 2);
hVal = Math.sqrt(x_sqrd + y_sqrd);
}
@Override
public int compareTo(Node node)
{
if (this.getH() < node.getH())
{
return -1;
}
else if (node.getH() > node.getH())
{
return 1;
}
return 0;
}
}