-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
138 lines (127 loc) · 3.26 KB
/
Node.java
File metadata and controls
138 lines (127 loc) · 3.26 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
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
// import com.sun.glass.ui.SystemClipboard;
/**
* *
* * assign1.java – Assignment1
* * @author: Jeremiah Smith, Juyong Kim
* * @student Number: c3238179 c3244203
* * @version: 016/10/2018
* * Description: node used in the minHeap
* */
public class Node implements Comparable<Node>
{
//variables
private boolean visited;
private String path;
private Station vertex;
private int time;
private int changes;
private String criterion;
//constructors
public Node() //default constructor
{
this.vertex = new Station();
this.time = Integer.MAX_VALUE;
this.changes = Integer.MAX_VALUE;
criterion = "";
}
public Node(Station vertex, int time, String criterion) //node instantiated with the vertex, time
{ //and criterion(time/changes)
this.vertex = vertex;
this.time = Integer.MAX_VALUE;
this.changes = Integer.MAX_VALUE;
this.criterion = criterion;
path = ""; //saves the path from source to this current Node
visited = false; //checks if node has been visited
}
//functions
public void updatePath(String prePath, StationEdge edge, Node node, String finalDestination)
{
//updates paths, concatenates the neccesary print out to path which is saved at the node
//so that the code tester can visually see the results better
path = prePath;
//adds first line
if (prePath.equals(""))
{
path += "From " + edge.getSource().getName() + " take line " + edge.getLine() + " to station ";
}
//adds changes lines
if (edge.getSource().getName().equals(edge.getDestination().getName()))
{
path += edge.getSource().getName() + ";\nthen change to line " + edge.getDestination().getLine() + ", and continue to station ";
}
//adds last station
if (edge.getDestination().getName().equals(finalDestination))
{
path += vertex.getName() + ";\n";
}
}
//controls sorting of nodes in the heap by comparing which path is better based on criteria
public int compareTo(Node node)
{
if (getComparator() > node.getComparator())
{
return 1;
}
else if (getComparator() == node.getComparator())
{
if (getSecondComparator() > node.getSecondComparator())
{
return 1;
}
else if (getSecondComparator() < node.getSecondComparator())
{
return -1;
}
return 0;
}
else if (getComparator() < node.getComparator())
{
return -1;
}
return 0;
}
//setters
public void setVertex(Station vertex)
{
this.vertex = vertex;
}
public void setTime(int time)
{
this.time = time;
}
public void setChanges(int changes)
{
this.changes = changes;
}
//getters
public Station getVertex()
{
return vertex;
}
public String getPath()
{
return path;
}
public int getComparator() //returns main comparision variable used for dijkstras Algorithm
{ //used to calculate the minimum time or changes path
if (criterion.equals("time"))
return time;
else
return changes;
}
public int getSecondComparator() //returns 2nd comparision variable used for dijkstras Algorithm
{ //used to calculate prefered path, if main comparator value
if (criterion.equals("changes")) //between verticies is the same
return time;
else
return changes;
}
public int getTime()
{
return time;
}
public int getChanges()
{
return changes;
}
}