-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxPQ.java
More file actions
75 lines (55 loc) · 2.01 KB
/
MaxPQ.java
File metadata and controls
75 lines (55 loc) · 2.01 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
/*
* Heap Ordering = A binary tree is heap ordered if the key in each node is larger
* than or equal to the keys in that node children (if any).
* We represent heap of size N in private array pq[] of length N+1, with
* pq[0] unused and the heap in pq[1] through pq[N]. The parent of node in position k is
* in position floor (k/2) and conversely, its two children in positions 2k and 2k+1.
* It is assumed that the keys are immutable and not changed by any client since that
* break the heap ordering. Also automatic array resizing is not addes yet.
*Author Sohof Jan 22, 2017
*/
public class MaxPQ<Key extends Comparable<Key>> {
private Key[] pq;
private int N = 0;
public MaxPQ(int maxN) {pq = (Key[]) new Comparable[maxN + 1]; }
public boolean isEmpty() {return N == 0; }
public int size() {return N;}
public void insert(Key v){
assert N < (pq.length+1);
pq[++N] = v;
swim(N);
}
public Key delMax(){
assert (N>0);
Key max = pq[1]; // Get max from ti
exch(1, N--); //exchange with last item, then decrement nr of items
pq[N+1] = null; // Avoid loitering
sink(1);
return max;
}
// Bottom-up reheapify
private void swim(int k) {
// k/2 is the parent of k
while(k > 1 && less(k/2, k)) {
exch(k/2, k); // parent was less than k, so exchange
k = k/2; // set k to parent
}
}
// Top-down reahipfy
private void sink(int k) {
while(2*k <= N) {
int j = 2*k;
if (j < N && less(j,j+1)) j++;
//make sure in bounds and compare children of k. Adjust j so it is the bigger child
if (!less(k,j)) break; // if parent no longer less than child j, then break.
exch(k, j); // parent k was less than j, so exchange
k = j; // recheck condition downwards for new k
}
}
// helper function to compare keys/nodes
private boolean less(int i, int j) {
return pq[i].compareTo(pq[j]) < 0 ; }
// Helper function to exchange to keys/nodes
private void exch(int i, int j) {
Key t = pq[i]; pq[i] = pq[j]; pq[j] = t; }
}