-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxPQ.java
More file actions
72 lines (62 loc) · 1.64 KB
/
MaxPQ.java
File metadata and controls
72 lines (62 loc) · 1.64 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
//Java Implementation of Maximum Priority Queues(Binary Heaps).
public class MaxPQ<Key extends Comparable<Key>> {
private Key[] pq;
private int N;
@SuppressWarnings("unchecked")
public MaxPQ(int capacity) {
pq = (Key[]) new Comparable[capacity+1]; //Fixed Capacity for Simplicity.
//In Binary heaps, index starts from 1, so the the capacity is increased with a unit.
}
//Checking if the array is empty or not
public boolean isEmpty() {
return N==0;
}
//Insertion operation in a heap.(Add the node at the end and swim it up)
public void insert(Key key) {
pq[++N] = key;
swim(N);
}
//Operation to delete maximum in a heap.(Exchange the root node with end node and then sink it down)
public Key delMax() {
Key max = pq[1];
exch(1,N--);
sink(1);
pq[N+1] = null;
return max;
}
//Swim Operation in a heap
private void swim(int k) {
while(k>1 && less(k/2,k)) {
exch(k,k/2);
k=k/2;
}
}
//Sink Operation in a heap
private void sink(int k) {
//root node = k;
//left node = 2*k;
//right node = 2*k+1;
while(2*k<=N) {
int j = 2*k;
//When the left node is less than the right node
if(k<N && less(j,j+1)) {
j++;
}
//When the child nodes is not greater than root node
if(!less(k,j)) {
break;
}
//Exchanging the root node(greater) and child node
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;
}
}