-
Notifications
You must be signed in to change notification settings - Fork 0
/
PriorityQueueUsingHeap.java
108 lines (97 loc) · 2.62 KB
/
PriorityQueueUsingHeap.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
/**
* Created with IntelliJ IDEA.
* User: Gong Li
* Date: 5/26/13
* Time: 12:23 PM
* Implement a priority queue using a heap
* The heap is implemented using an array indexed from 1
*/
public class PriorityQueueUsingHeap<T extends Comparable<T>> {
T[] arr;
int N;
public static void main(String[] args) {
PriorityQueueUsingHeap<Integer> pq = new PriorityQueueUsingHeap<Integer>();
pq.insert(3);
System.out.println(pq.toString());
pq.insert(5);
System.out.println(pq.toString());
pq.insert(2);
System.out.println(pq.toString());
pq.insert(-1);
System.out.println(pq.toString());
pq.insert(9);
System.out.println(pq.toString());
pq.insert(4);
System.out.println(pq.toString());
pq.delMax();
System.out.println(pq.toString());
pq.delMax();
System.out.println(pq.toString());
pq.delMax();
System.out.println(pq.toString());
pq.delMax();
System.out.println(pq.toString());
pq.delMax();
System.out.println(pq.toString());
}
public PriorityQueueUsingHeap(){
arr = (T[]) new Comparable[2];
N = 0;
}
public void insert(T t){
if (N == arr.length - 1) resize(2*N + 1);
arr[++N] = t;
swim(N);
}
public T delMax(){
if (isEmpty()) return null;
T t= arr[1];
exch(1,N--);
arr[N+1] = null;
sink(1);
//resize this array
if (N == (arr.length -1)/4) resize((arr.length-1) / 2 + 1);
return t;
}
//helper methods
public String toString(){
StringBuffer sb = new StringBuffer();
for(int i = 1; i <= N; i ++)
sb.append(arr[i].toString()+" ");
return sb.toString();
}
private boolean isEmpty(){
return N == 0;
}
private void resize(int capacity){
T[] copy = (T[]) new Comparable[capacity];
for(int i = 1; i <= N; i ++ )
copy[i] = arr[i];
arr = copy;
}
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 = j + 1;
if(less(j, k)) break;
exch(k,j);
k = j;
}
}
private boolean less(int i, int j){
if (arr[i].compareTo(arr[j]) < 0)
return true;
return false;
}
private void exch(int i, int j){
T temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}