forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinPriorityQueue.java
More file actions
155 lines (140 loc) · 4.56 KB
/
MinPriorityQueue.java
File metadata and controls
155 lines (140 loc) · 4.56 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
package com.thealgorithms.datastructures.heaps;
/**
* A MinPriorityQueue is a specialized data structure that maintains the
* min-heap property, where the smallest element has the highest priority.
*
* <p>In a min-priority queue, every parent node is less than or equal
* to its child nodes, which ensures that the smallest element can
* always be efficiently retrieved.</p>
*
* <p>Functions:</p>
* <ul>
* <li><b>insert(int key)</b>: Inserts a new key into the queue.</li>
* <li><b>delete()</b>: Removes and returns the highest priority value (the minimum).</li>
* <li><b>peek()</b>: Returns the highest priority value without removing it.</li>
* <li><b>isEmpty()</b>: Checks if the queue is empty.</li>
* <li><b>isFull()</b>: Checks if the queue is full.</li>
* <li><b>heapSort()</b>: Sorts the elements in ascending order.</li>
* <li><b>print()</b>: Prints the current elements in the queue.</li>
* </ul>
*/
public class MinPriorityQueue {
private final int[] heap;
private final int capacity;
private int size;
/**
* Initializes a new MinPriorityQueue with a specified capacity.
*
* @param c the maximum number of elements the queue can hold
*/
public MinPriorityQueue(int c) {
this.capacity = c;
this.size = 0;
this.heap = new int[c + 1];
}
/**
* Inserts a new key into the min-priority queue.
*
* @param key the value to be inserted
*/
public void insert(int key) {
if (this.isFull()) {
throw new IllegalStateException("MinPriorityQueue is full. Cannot insert new element.");
}
this.heap[this.size + 1] = key;
int k = this.size + 1;
while (k > 1) {
if (this.heap[k] < this.heap[k / 2]) {
int temp = this.heap[k];
this.heap[k] = this.heap[k / 2];
this.heap[k / 2] = temp;
}
k = k / 2;
}
this.size++;
}
/**
* Retrieves the highest priority value (the minimum) without removing it.
*
* @return the minimum value in the queue
* @throws IllegalStateException if the queue is empty
*/
public int peek() {
if (isEmpty()) {
throw new IllegalStateException("MinPriorityQueue is empty. Cannot peek.");
}
return this.heap[1];
}
/**
* Checks whether the queue is empty.
*
* @return true if the queue is empty, false otherwise
*/
public boolean isEmpty() {
return size == 0;
}
/**
* Checks whether the queue is full.
*
* @return true if the queue is full, false otherwise
*/
public boolean isFull() {
return size == capacity;
}
/**
* Prints the elements of the queue.
*/
public void print() {
for (int i = 1; i <= this.size; i++) {
System.out.print(this.heap[i] + " ");
}
System.out.println();
}
/**
* Sorts the elements in the queue using heap sort.
*/
public void heapSort() {
for (int i = 1; i <= this.size; i++) {
this.delete();
}
}
/**
* Reorders the heap after a deletion to maintain the heap property.
*/
private void sink() {
int k = 1;
while (2 * k <= this.size) {
int minIndex = k; // Assume current index is the minimum
if (2 * k <= this.size && this.heap[2 * k] < this.heap[minIndex]) {
minIndex = 2 * k; // Left child is smaller
}
if (2 * k + 1 <= this.size && this.heap[2 * k + 1] < this.heap[minIndex]) {
minIndex = 2 * k + 1; // Right child is smaller
}
if (minIndex == k) {
break; // No swap needed, heap property is satisfied
}
// Swap with the smallest child
int temp = this.heap[k];
this.heap[k] = this.heap[minIndex];
this.heap[minIndex] = temp;
k = minIndex; // Move down to the smallest child
}
}
/**
* Deletes and returns the highest priority value (the minimum) from the queue.
*
* @return the minimum value from the queue
* @throws IllegalStateException if the queue is empty
*/
public int delete() {
if (isEmpty()) {
throw new IllegalStateException("MinPriorityQueue is empty. Cannot delete.");
}
int min = this.heap[1];
this.heap[1] = this.heap[this.size]; // Move last element to the root
this.size--;
this.sink();
return min;
}
}