forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueues.java
More file actions
179 lines (162 loc) · 5.2 KB
/
PriorityQueues.java
File metadata and controls
179 lines (162 loc) · 5.2 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package com.thealgorithms.datastructures.queues;
/**
* This class implements a PriorityQueue.
*
* <p>
* A priority queue adds elements into positions based on their priority. So the
* most important elements are placed at the front/on the top. In this example I
* give numbers that are bigger, a higher priority. Queues in theory have no
* fixed size but when using an array implementation it does.
* <p>
* Additional contibutions made by: PuneetTri(https://github.com/PuneetTri)
*/
class PriorityQueue {
/**
* The max size of the queue
*/
private int maxSize;
/**
* The array for the queue
*/
private int[] queueArray;
/**
* How many items are in the queue
*/
private int nItems;
/**
* Default Constructor
*/
PriorityQueue() {
/* If capacity is not defined, default size of 11 would be used
* capacity=max+1 because we cant access 0th element of PQ, and to
* accomodate (max)th elements we need capacity to be max+1.
* Parent is at position k, child at position (k*2,k*2+1), if we
* use position 0 in our queue, its child would be at:
* (0*2, 0*2+1) -> (0,0). This is why we start at position 1
*/
int size = 11; // Default value of 11
maxSize = size + 1;
queueArray = new int[maxSize];
nItems = 0;
}
/**
* Parameterized Constructor
*
* @param size Size of the queue
*/
PriorityQueue(int size) {
maxSize = size + 1;
queueArray = new int[maxSize];
nItems = 0;
}
/**
* Helper function for the max-heap implementation of PQ
* Function would help demote parent node to their correct
* position
*
* @param pos Position of newly added element at bottom
*/
private void swim(int pos) {
// Check if parent is smaller than child node
while (pos > 1 && (queueArray[pos / 2] < queueArray[pos])) {
// In such case swap value of child with parent
int temp = queueArray[pos];
queueArray[pos] = queueArray[pos / 2];
queueArray[pos / 2] = temp;
pos = pos / 2; // Jump to position of parent node
}
// Promotion of child node will go on until it becomes smaller than the parent
}
/**
* Helper function for the max-heap implementation of PQ
* Function would help demote parent node to their correct
* position
*
* @param pos Position of element at top
*/
private void sink(int pos) {
// Check if node's position is that of parent node
while (2 * pos <= nItems) {
int current = 2 * pos; // Jump to the positon of child node
// Compare both the children for the greater one
if (current < nItems && queueArray[current] < queueArray[current + 1]) {
current++;
}
// If the parent node is greater, sink operation is complete. Break the loop
if (queueArray[pos] >= queueArray[current]) {
break;
}
// If not exchange the value of parent with child
int temp = queueArray[pos];
queueArray[pos] = queueArray[current];
queueArray[current] = temp;
pos = current; // Exchange parent position to child position in the array
}
}
/**
* Inserts an element in it's appropriate place
*
* @param value Value to be inserted
*/
public void insert(int value) {
// Print overflow message if the capacity is full
if (isFull()) {
throw new RuntimeException("Queue is full");
} else {
queueArray[++nItems] = value;
swim(nItems); // Swim up the element to its correct position
}
}
/**
* Dequeue the element with the max priority from PQ
*
* @return The element removed
*/
public int remove() {
if (isEmpty()) {
throw new RuntimeException("Queue is Empty");
} else {
int max = queueArray[1]; // By defintion of our max-heap, value at queueArray[1] pos is
// the greatest
// Swap max and last element
int temp = queueArray[1];
queueArray[1] = queueArray[nItems];
queueArray[nItems] = temp;
queueArray[nItems--] = 0; // Nullify the last element from the priority queue
sink(1); // Sink the element in order
return max;
}
}
/**
* Checks what's at the front of the queue
*
* @return element at the front of the queue
*/
public int peek() {
return queueArray[1];
}
/**
* Returns true if the queue is empty
*
* @return true if the queue is empty
*/
public boolean isEmpty() {
return (nItems == 0);
}
/**
* Returns true if the queue is full
*
* @return true if the queue is full
*/
public boolean isFull() {
return (nItems == maxSize - 1);
}
/**
* Returns the number of elements in the queue
*
* @return number of elements in the queue
*/
public int getSize() {
return nItems;
}
}