-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueue.java
More file actions
156 lines (139 loc) · 3.22 KB
/
PriorityQueue.java
File metadata and controls
156 lines (139 loc) · 3.22 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package Class05_BFS_Graph;
//!!!!!!!!!!!!!!!!!!!!!!!!!!! check corner case!!!!!!!!!!!!!!!!!!!!!!
public class PriorityQueue {
private int[] array;
private int lastIdx;
public PriorityQueue(int[] arr) {
if (arr == null || arr.length == 0) {
// throw exception // how to do this part?
}
array = arr;
// the left-hand of lastIdx (not included) is the last index of array;
lastIdx = array.length;
heapify();
}
public PriorityQueue(int capacity) {
if (capacity <= 0) {
// throw exception // ????
}
array = new int[capacity];
lastIdx = 0;
}
private void heapify() {
for (int i = 0; i < array.length / 2 - 1; i++) {
percolateDown(i);
}
}
public int size() {
return lastIdx;
}
public boolean isEmpty() {
if (size() == 0) {
return true;
}
return false;
}
public boolean isFull() {
if (size() == array.length) {
return true;
}
return false;
}
public int peek() {
return array[0];
}
public int poll() {
if (size() == 0) {
// throw exception;
}
int rsl = array[0];
array[0] = array[array.length - 1];
lastIdx--;
percolateDown(array[0]);
return rsl;
}
public void offer(int val) {
if (size() == array.length) {
int[] newArray = new int[(int) 1.5 * array.length];
copy(newArray, array);
array = newArray;
}
array[size()] = val;
percolateUp(size());
lastIdx++;
}
public void update(int index, int value) {
if (index < 0 || index >= size()) {
// throw exception;
}
if (array[index] == value) {
return;
} else if (array[index] < value) { // new value > old value, percolate
// down
array[index] = value;
percolateDown(index);
} else { // new value < old value, percolate up
array[index] = value;
percolateUp(index);
}
}
private void percolateUp(int index) {
if (index == 0) {
return;
}
int parent = (index - 1) / 2;
while (index >= 0 && array[index] < array[parent]) {
swap(array, index, parent);
percolateUp(parent);
}
}
private void percolateDown(int index) {
int leftC = 2 * index + 1;
int rightC = 2 * index + 1;
if (leftC < size() && rightC < size()) {
// get the index of smallest value
int temp = findSmallest(array, index, leftC, rightC);
if (temp == index) {
return;
} else if (temp == leftC) {
swap(array, leftC, index);
percolateDown(leftC);
} else {
swap(array, rightC, index);
percolateDown(rightC);
}
} else if (leftC < size() && array[leftC] < array[index]) {
swap(array, leftC, index);
}
return;
}
// copy the value from smaller array to larger array
private void copy(int[] target, int[] origin) {
if (target.length <= origin.length) {
return;
}
for (int i : origin) {
target[i] = origin[i];
}
}
// swap two values of an array
private void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
// find the index of smallest value
private int findSmallest(int[] array, int p, int l, int r) {
if (array[p] <= array[l] && array[p] <= array[r]) {
return p;
} else if (array[p] < array[l] && array[p] > array[r]) {
return r;
} else if (array[p] > array[l] && array[p] < array[r]) {
return l;
} else if (array[l] < array[r]) {
return l;
} else {
return r;
}
}
}