forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeapPriorityQueue.java
165 lines (149 loc) · 4.4 KB
/
HeapPriorityQueue.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import java.util.NoSuchElementException;
public class HeapPriorityQueue {
private String[] heap;
private int size; // Number of elements in the heap
/**
* Initializes an empty priority queue with the given initial capacity
*/
public HeapPriorityQueue(int initialCapacity) {
heap = new String[initialCapacity + 1];
size = 0;
}
/**
* Initializes an empty priority queue
*/
public HeapPriorityQueue() {
this(1);
}
/**
* Initializes a priority queue from the array of values
*
* @param vals the array of values
*/
public HeapPriorityQueue(String [] vals) {
size = vals.length;
heap = new String[size + 1];
// we do not use the 0 index with the heap
System.arraycopy(vals, 0, heap, 1, size);
for(int x = size/2; x > 0; x--) {
percolateDown(x);
}
}
/**
* Returns true if the priority queue is empty
*
* @return true if this priority queue is empty; false otherwise
*/
public boolean isEmpty() {
return size == 0;
}
/**
* Returns the number of elements in the priority queue
*
* @return the number of elements in the priority queue
*/
public int size() {
return size;
}
/**
* Returns the smallest element in the priority queue
*
* @return the smallest element in the priority queue
* @throws NoSuchElementException if the priority queue is empty
*/
public String min() {
if(isEmpty()) {
throw new NoSuchElementException("Priority Queue is empty!");
}
return heap[1];
}
/**
* Add a new element to the priority queue
*
* @param element the element to insert
*/
public void insert(String element) {
if(size == heap.length -1) {
doubleSize();
}
int pos = ++size;
// percolate up
for(; pos > 1 && element.compareTo(heap[pos/2]) < 0; pos = pos/2) {
heap[pos] = heap[pos/2];
}
heap[pos] = element;
}
/**
* Removes and returns the smallest element in the priority queue
*
* @return the smallest element in the priority queue
* @throws NoSuchElementException if the priority queue is empty
*/
public String removeMin() throws NoSuchElementException {
if(size == 0) {
throw new NoSuchElementException("Priority Queue is empty!");
}
String min = heap[1];
heap[1] = heap[size--];
percolateDown(1);
return min;
}
private void doubleSize() {
String [] old = heap;
heap = (String[]) new String[heap.length * 2];
System.arraycopy(old, 1, heap, 1, size);
}
private void percolateDown(int k) {
String tmp = heap[k];
int left;
// Traverse down the left side of the tree
for(; 2*k <= size; k = left) {
left = 2*k;
int right = left + 1;
if(left != size && heap[left].compareTo(heap[right]) > 0) {
left = right;
}
if(tmp.compareTo(heap[left]) > 0) {
heap[k] = heap[left];
} else {
break;
}
}
heap[k] = tmp;
}
@Override
public String toString() {
StringBuilder out = new StringBuilder();
for(int x = 1; x <= size; ++x) {
out.append(heap[x]).append(" ");
}
// remove trailing space
out.setLength(out.length() - 1);
return out.toString();
}
/*
* Output is provided with the inline comments
*/
public static void main(String [] args) {
String [] keys = new String[]{"p", "r", "i", "o"};
HeapPriorityQueue h = new HeapPriorityQueue(keys);
System.out.println(h.toString()); // i o p r
h.insert("q");
h.insert("z");
h.insert("b");
System.out.println(h.toString()); // b o i r q z p
h.removeMin();
System.out.println(h.toString()); // i o p r q z
h.removeMin();
System.out.println(h.toString()); // o q p r z
System.out.println(h.min()); // o
h.removeMin();
System.out.println(h.min()); p
h.removeMin();
h.removeMin();
assert(h.size() == 2);
h.removeMin();
h.removeMin();
System.out.println(h.isEmpty()); // true
}
}