-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxPQHeap.java
144 lines (132 loc) · 3.78 KB
/
MaxPQHeap.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
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
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
public class MaxPQHeap<T extends Comparable<? super T>> implements MaxPriorityQueue {
private static final int DEFAULT_SIZE = 10;
private T[] data;
private int size;
private Comparator<? super T> comp;
/**
* Creates a heap with a backing array of default initial size.
*/
public MaxPQHeap() {
this(DEFAULT_SIZE);
}
/**
* Creates an empty heap with a backing array of the given initial capacity.
*
* @param initialCapacity
* the initial size of the backing array
*/
public MaxPQHeap(int initialCapacity) {
// Unchecked warning unavoidable. Note we have to create
// an array of Comparable, not Object, in order
// to cast to E[]
data = (T[]) new Comparable[initialCapacity];
size = 0;
// create and cache the comparator to reuse in
// the static percolateUp/Down methods
Comparator<T> comp = new Comparator<T>() {
@Override
public int compare(T lhs, T rhs) {
return rhs.compareTo(lhs);
}
};
this.comp = comp;
}
@Override
public int size() {
return this.size;
}
@Override
public boolean isEmpty() {
return this.size == 0;
}
@Override
public void clear() {
data = (T[]) new Comparable[DEFAULT_SIZE];
this.size = 0;
}
@Override
public Comparable getMax() throws QueueEmptyException {
if (size == 0) throw new QueueEmptyException();
return data[0];
}
@Override
public Comparable removeMax() throws QueueEmptyException {
if (size == 0) throw new QueueEmptyException();
T ret = data[0];
data[0] = data[size - 1];
data[size - 1] = null;
--size;
percolateDown(data, comp, size, 0);
return ret;
}
private void checkCapacity()
{
if (size == data.length)
{
// create a copy of the data array with double the capacity
data = Arrays.copyOf(data, data.length * 2);
}
}
@Override
public void insert(Comparable val) {
checkCapacity();
data[size] = (T) val;
percolateUp(data, comp, size);
++size;
}
private static <T> void percolateUp(T[] data, Comparator<? super T> comp, int current)
{
int parent = (current - 1) / 2;
while (current > 0 && comp.compare(data[current], data[parent]) < 0)
{
swap(data, current, parent);
current = parent;
parent = (current - 1) / 2;
}
}
private static <T> void percolateDown(T[] data, Comparator<? super T> comp, int size, int current)
{
int child = 2 * current + 1; // left child, if any
while (child < size)
{
if (child + 1 < size)
{
// there's a right child too, pick the smallest child
if (comp.compare(data[child], data[child + 1]) > 0)
{
child = child + 1;
}
}
if (comp.compare(data[current], data[child]) > 0)
{
swap(data, current, child);
}
current = child;
child = 2 * current + 1;
}
}
private static void swap(Object[] data, int i, int j)
{
Object temp = data[i];
data[i] = data[j];
data[j] = temp;
}
@Override
public void init(Collection values) {
Iterator iter = values.iterator();
while (iter.hasNext()) {
this.insert((Comparable) iter.next());
}
}
public String toString() {
String output = "";
for (int i = 0; i < this.size; i++) {
output += data[i] + " ";
}
return output;
}
}