-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
89 lines (75 loc) · 1.69 KB
/
Copy pathNode.java
File metadata and controls
89 lines (75 loc) · 1.69 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
package main;
import java.util.ArrayList;
import java.util.HashSet;
public class Node implements Comparable<Node>{
private static int numberOfNodes = 0;
private int id;
private HashSet<Node> adjacentNodes;
public int distance; //used for dijkstras
public Node(){
id = numberOfNodes;
numberOfNodes++;
adjacentNodes = new HashSet<Node>();
}
/**
* Add the supplied Node to this Node's adjacent nodes and vice versa
*/
public void connect(Node other){
adjacentNodes.add(other);
other.getAdjacentNodes().add(this);
}
/**
* @return All Node's adjacent to this node in the form of an ArrayList
*/
public ArrayList<Node> getAdjacentNodeList(){
ArrayList<Node> list = new ArrayList<Node>();
for( Node n: adjacentNodes){
list.add(n);
}
return list;
}
/**
* Compare Node's distance to other Node's distance, used in Dijkstras
* @return 1 if distance is greater, -1 if is is smaller, 0 if they have the same distance
*/
public int compareDistances(Node other) {
if(other == null){
throw new IllegalArgumentException("Cannot compare to null node");
}
if(distance < other.distance ){
return -1;
}
if(distance > other.distance){
return 1;
}
return 0;
}
public boolean isAdjacent(Node other){
if(this == other){
return true;
}
return adjacentNodes.contains(other);
}
public HashSet<Node> getAdjacentNodes(){
return adjacentNodes;
}
public int id(){
return id;
}
public int getDegree(){
return adjacentNodes.size();
}
public String toString(){
return "(" + id + ")";
}
@Override
public int compareTo(Node other) {
if(id < ((Node) other).id()){
return -1;
}
if(id > ((Node) other).id()){
return 1;
}
return 0;
}
}