-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryHeap.java
64 lines (54 loc) · 1.68 KB
/
BinaryHeap.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
import java.util.EmptyStackException;
public class BinaryHeap {
int[] data = new int[10];
int size = 0;
public void add(int item) {
if(size == data.length - 1) {
growArray();
}
data[size++] = item;
int current = size-1;
int parent = (current - 1) / 2;
while(data[current] < data[parent] && current != 0) {
swap(data, current, parent);
current = parent;
parent = (parent - 1) / 2;
}
}
public int remove() {
if (size == 0) { throw new EmptyStackException(); }// throw exception if empty
swap(data, 0, size - 1); // swap the root value with the final-1 value in array (sort the end of the array)
size--;
if(size > 0) {
shiftdown(0); // work way down tree recursively, swapping values to restore the order property
}
return data[size];
}
public void shiftdown(int position) {
int smallest = position;
int left = (2 * position) + 1;
int right = (2 * position) + 2;
if (left < size && data[left] < data[smallest]) {
smallest = left;
}
if (right < size && data[right] < data[smallest]) {
smallest = right;
}
if (smallest != position) {
swap(data, smallest, position);
shiftdown(smallest);
}
}
private void swap(int[]a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
protected void growArray() {
int[] temp = new int[data.length * 2];
for(int i = 0; i < size; i++) {
temp[i] = data[i];
}
data = temp;
}
}