-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxPQTest.java
More file actions
108 lines (83 loc) · 2.31 KB
/
MaxPQTest.java
File metadata and controls
108 lines (83 loc) · 2.31 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
/* *****************************************************************************
* Name: Jaya Mukherjee
* Coursera User ID: XXXXXX
* Last modified: October 23, 2021
**************************************************************************** */
import edu.princeton.cs.algs4.MaxPQ;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class MaxPQTest<Key extends Comparable<Key>> implements Iterable<Key> {
private Key[] pq;
private int n = 0;
public MaxPQTest(int max) {
pq = (Key[]) new Comparable[max + 1];
}
public MaxPQTest(Key[] a) {
}
public boolean isEmpty() {
return n == 0;
}
public int size() {
return n;
}
public void insert(Key v) {
pq[++n] = v;
swim(n);
}
public Key delMax() {
Key max = pq[1];
exch(1, n--);
pq[n + 1] = null;
sink(1);
return max;
}
private void swim(int k) {
while (k > 1 && less(k / 2, k)) {
exch(k / 2, k);
k = k / 2;
}
}
private void sink(int k) {
while (2 * k <= n) {
int j = 2 * k;
if (j < n && less(j, j + 1))
j++;
if (!less(k, j))
break;
exch(k, j);
k = j;
}
}
private boolean less(int i, int j) {
return pq[i].compareTo(pq[j]) < 0;
}
private void exch(int i, int j) {
Key t = pq[i];
pq[i] = pq[j];
pq[j] = t;
}
public Iterator<Key> iterator() {
return new HeapIterator();
}
private class HeapIterator implements Iterator<Key> {
// create a new pq
private MaxPQ<Key> copy;
// add all items to copy of heap
// takes linear time since already in heap order so no keys move
public HeapIterator() {
copy = new MaxPQ<Key>(size());
for (int i = 1; i <= n; i++)
copy.insert(pq[i]);
}
public boolean hasNext() {
return !copy.isEmpty();
}
public void remove() {
throw new UnsupportedOperationException();
}
public Key next() {
if (!hasNext()) throw new NoSuchElementException();
return copy.delMax();
}
}
}