-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPriorityQueueNode.java
80 lines (69 loc) · 1.55 KB
/
PriorityQueueNode.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
import java.util.ArrayList;
import java.util.Arrays;
/**
* Custom Priority Queue for Nodes
*/
public class PriorityQueueNode {
ArrayList<Node> pl;
public PriorityQueueNode(){
pl = new ArrayList<Node>();
}
/**
* Add a node to the priority queue
* @param n - node
*/
public void add(Node n) {
pl.add(n);
pl.sort((a,b)->(a.f() - b.f()));
}
/**
* Check if the priority queue is empty
* @return true if empty
*/
public boolean isEmpty() {
return pl.isEmpty();
}
/**
* Poll the first node in the priority queue
* @return the first node
*/
public Node poll() {
Node t = pl.get(0);
pl.remove(0);
return t;
}
/**
* Check if the priority queue contains a node
* @param g - node
* @return true if contains
*/
public boolean contains(Node g) {
for(Node n : pl){
if(n.equals(g)) return true;
}
return false;
}
/**
* Swap a node with a lower value
* @param n - node
*/
public void swapForLowerValue(Node n){
for (int i = 0; i < pl.size(); i++) {
Node t = pl.get(i);
if(t.equals(n)){
pl.remove(i);
this.add(n);
break;
}
}
}
public String toString(){
return Arrays.deepToString(pl.toArray());
}
public int size() {
return pl.size();
}
public Object[] toArray() {
return pl.toArray();
}
}