1
1
package com .thealgorithms .datastructures .heaps ;
2
2
3
3
/**
4
- * Minimum Priority Queue It is a part of heap data structure A heap is a
5
- * specific tree based data structure in which all the nodes of tree are in a
6
- * specific order. that is the children are arranged in some respect of their
7
- * parents, can either be greater or less than the parent. This makes it a min
8
- * priority queue or max priority queue.
4
+ * A MinPriorityQueue is a specialized data structure that maintains the
5
+ * min-heap property, where the smallest element has the highest priority.
9
6
*
10
- * <p>
7
+ * <p>In a min-priority queue, every parent node is less than or equal
8
+ * to its child nodes, which ensures that the smallest element can
9
+ * always be efficiently retrieved.</p>
11
10
*
12
- * <p>
13
- * Functions: insert, delete, peek, isEmpty, print, heapSort, sink
11
+ * <p>Functions:</p>
12
+ * <ul>
13
+ * <li><b>insert(int key)</b>: Inserts a new key into the queue.</li>
14
+ * <li><b>delete()</b>: Removes and returns the highest priority value (the minimum).</li>
15
+ * <li><b>peek()</b>: Returns the highest priority value without removing it.</li>
16
+ * <li><b>isEmpty()</b>: Checks if the queue is empty.</li>
17
+ * <li><b>isFull()</b>: Checks if the queue is full.</li>
18
+ * <li><b>heapSort()</b>: Sorts the elements in ascending order.</li>
19
+ * <li><b>print()</b>: Prints the current elements in the queue.</li>
20
+ * </ul>
14
21
*/
15
22
public class MinPriorityQueue {
16
23
17
24
private final int [] heap ;
18
25
private final int capacity ;
19
26
private int size ;
20
27
21
- // class the constructor and initializes the capacity
22
- MinPriorityQueue (int c ) {
28
+ /**
29
+ * Initializes a new MinPriorityQueue with a specified capacity.
30
+ *
31
+ * @param c the maximum number of elements the queue can hold
32
+ */
33
+ public MinPriorityQueue (int c ) {
23
34
this .capacity = c ;
24
35
this .size = 0 ;
25
36
this .heap = new int [c + 1 ];
26
37
}
27
38
28
- // inserts the key at the end and rearranges it
29
- // so that the binary heap is in appropriate order
39
+ /**
40
+ * Inserts a new key into the min-priority queue.
41
+ *
42
+ * @param key the value to be inserted
43
+ */
30
44
public void insert (int key ) {
31
45
if (this .isFull ()) {
32
- return ;
46
+ throw new IllegalStateException ( "MinPriorityQueue is full. Cannot insert new element." ) ;
33
47
}
34
48
this .heap [this .size + 1 ] = key ;
35
49
int k = this .size + 1 ;
@@ -44,89 +58,98 @@ public void insert(int key) {
44
58
this .size ++;
45
59
}
46
60
47
- // returns the highest priority value
61
+ /**
62
+ * Retrieves the highest priority value (the minimum) without removing it.
63
+ *
64
+ * @return the minimum value in the queue
65
+ * @throws IllegalStateException if the queue is empty
66
+ */
48
67
public int peek () {
68
+ if (isEmpty ()) {
69
+ throw new IllegalStateException ("MinPriorityQueue is empty. Cannot peek." );
70
+ }
49
71
return this .heap [1 ];
50
72
}
51
73
52
- // returns boolean value whether the heap is empty or not
74
+ /**
75
+ * Checks whether the queue is empty.
76
+ *
77
+ * @return true if the queue is empty, false otherwise
78
+ */
53
79
public boolean isEmpty () {
54
- return 0 == this . size ;
80
+ return size == 0 ;
55
81
}
56
82
57
- // returns boolean value whether the heap is full or not
83
+ /**
84
+ * Checks whether the queue is full.
85
+ *
86
+ * @return true if the queue is full, false otherwise
87
+ */
58
88
public boolean isFull () {
59
- return this . size == this . capacity ;
89
+ return size == capacity ;
60
90
}
61
91
62
- // prints the heap
92
+ /**
93
+ * Prints the elements of the queue.
94
+ */
63
95
public void print () {
64
- for (int i = 1 ; i <= this .capacity ; i ++) {
96
+ for (int i = 1 ; i <= this .size ; i ++) {
65
97
System .out .print (this .heap [i ] + " " );
66
98
}
67
99
System .out .println ();
68
100
}
69
101
70
- // heap sorting can be done by performing
71
- // delete function to the number of times of the size of the heap
72
- // it returns reverse sort because it is a min priority queue
102
+ /**
103
+ * Sorts the elements in the queue using heap sort.
104
+ */
73
105
public void heapSort () {
74
- for (int i = 1 ; i < this .capacity ; i ++) {
106
+ for (int i = 1 ; i <= this .size ; i ++) {
75
107
this .delete ();
76
108
}
77
109
}
78
110
79
- // this function reorders the heap after every delete function
111
+ /**
112
+ * Reorders the heap after a deletion to maintain the heap property.
113
+ */
80
114
private void sink () {
81
115
int k = 1 ;
82
- while (2 * k <= this .size || 2 * k + 1 <= this .size ) {
83
- int minIndex ;
84
- if (this .heap [2 * k ] >= this .heap [k ]) {
85
- if (2 * k + 1 <= this .size && this .heap [2 * k + 1 ] >= this .heap [k ]) {
86
- break ;
87
- } else if (2 * k + 1 > this .size ) {
88
- break ;
89
- }
116
+ while (2 * k <= this .size ) {
117
+ int minIndex = k ; // Assume current index is the minimum
118
+
119
+ if (2 * k <= this .size && this .heap [2 * k ] < this .heap [minIndex ]) {
120
+ minIndex = 2 * k ; // Left child is smaller
90
121
}
91
- if (2 * k + 1 > this .size ) {
92
- minIndex = this .heap [2 * k ] < this .heap [k ] ? 2 * k : k ;
93
- } else {
94
- if (this .heap [k ] > this .heap [2 * k ] || this .heap [k ] > this .heap [2 * k + 1 ]) {
95
- minIndex = this .heap [2 * k ] < this .heap [2 * k + 1 ] ? 2 * k : 2 * k + 1 ;
96
- } else {
97
- minIndex = k ;
98
- }
122
+ if (2 * k + 1 <= this .size && this .heap [2 * k + 1 ] < this .heap [minIndex ]) {
123
+ minIndex = 2 * k + 1 ; // Right child is smaller
99
124
}
125
+
126
+ if (minIndex == k ) {
127
+ break ; // No swap needed, heap property is satisfied
128
+ }
129
+
130
+ // Swap with the smallest child
100
131
int temp = this .heap [k ];
101
132
this .heap [k ] = this .heap [minIndex ];
102
133
this .heap [minIndex ] = temp ;
103
- k = minIndex ;
134
+
135
+ k = minIndex ; // Move down to the smallest child
104
136
}
105
137
}
106
138
107
- // deletes the highest priority value from the heap
139
+ /**
140
+ * Deletes and returns the highest priority value (the minimum) from the queue.
141
+ *
142
+ * @return the minimum value from the queue
143
+ * @throws IllegalStateException if the queue is empty
144
+ */
108
145
public int delete () {
146
+ if (isEmpty ()) {
147
+ throw new IllegalStateException ("MinPriorityQueue is empty. Cannot delete." );
148
+ }
109
149
int min = this .heap [1 ];
110
- this .heap [1 ] = this .heap [this .size ];
111
- this .heap [this .size ] = min ;
150
+ this .heap [1 ] = this .heap [this .size ]; // Move last element to the root
112
151
this .size --;
113
152
this .sink ();
114
153
return min ;
115
154
}
116
-
117
- public static void main (String [] args ) {
118
- // testing
119
- MinPriorityQueue q = new MinPriorityQueue (8 );
120
- q .insert (5 );
121
- q .insert (2 );
122
- q .insert (4 );
123
- q .insert (1 );
124
- q .insert (7 );
125
- q .insert (6 );
126
- q .insert (3 );
127
- q .insert (8 );
128
- q .print (); // [ 1, 2, 3, 5, 7, 6, 4, 8 ]
129
- q .heapSort ();
130
- q .print (); // [ 8, 7, 6, 5, 4, 3, 2, 1 ]
131
- }
132
155
}
0 commit comments