Repository: TheAlgorithms/Java
Pull request #5986 (2024-10-24 07:08:57)

refactor: Enhance docs, add tests in `MinPriorityQueue`
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5986?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `75.00000%` with `5 lines` in your changes missing coverage. Please review.
> Project coverage is 68.27%. Comparing base [(`5246f63`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/5246f635797194f5f0d7ac93af61099a5764fa4f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`6f87d10`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/6f87d10362230721b2b2053fae9d6ddc7ee3aa65?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5986?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...orithms/datastructures/heaps/MinPriorityQueue.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5986?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fdatastructures%2Fheaps%2FMinPriorityQueue.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9kYXRhc3RydWN0dXJlcy9oZWFwcy9NaW5Qcmlvcml0eVF1ZXVlLmphdmE=) | 75.00% | [3 Missing and 2 partials :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5986?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5986      +/-   ##
============================================
+ Coverage     67.98%   68.27%   +0.29%     
- Complexity     4586     4605      +19     
============================================
  Files           620      620              
  Lines         17116    17103      -13     
  Branches       3305     3301       -4     
============================================
+ Hits          11636    11677      +41     
+ Misses         5035     4979      -56     
- Partials        445      447       +2     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5986?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index 2a6bf70c..ee1b9dcd 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -833,6 +833,7 @@
               * [FibonacciHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/FibonacciHeapTest.java)
               * [GenericHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/GenericHeapTest.java)
               * [LeftistHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/LeftistHeapTest.java)
+              * [MinPriorityQueueTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/MinPriorityQueueTest.java)
             * lists
               * [CircleLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CircleLinkedListTest.java)
               * [CountSinglyLinkedListRecursionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CountSinglyLinkedListRecursionTest.java)
diff --git a/src/main/java/com/thealgorithms/datastructures/heaps/MinPriorityQueue.java b/src/main/java/com/thealgorithms/datastructures/heaps/MinPriorityQueue.java
index 9d19e9aa..a1360b14 100644
--- a/src/main/java/com/thealgorithms/datastructures/heaps/MinPriorityQueue.java
+++ b/src/main/java/com/thealgorithms/datastructures/heaps/MinPriorityQueue.java
@@ -1,16 +1,23 @@
 package com.thealgorithms.datastructures.heaps;
 
 /**
- * Minimum Priority Queue It is a part of heap data structure A heap is a
- * specific tree based data structure in which all the nodes of tree are in a
- * specific order. that is the children are arranged in some respect of their
- * parents, can either be greater or less than the parent. This makes it a min
- * priority queue or max priority queue.
+ * A MinPriorityQueue is a specialized data structure that maintains the
+ * min-heap property, where the smallest element has the highest priority.
  *
- * <p>
+ * <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: insert, delete, peek, isEmpty, print, heapSort, sink
+ * <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 {
 
@@ -18,18 +25,25 @@ public class MinPriorityQueue {
     private final int capacity;
     private int size;
 
-    // class the constructor and initializes the capacity
-    MinPriorityQueue(int c) {
+    /**
+     * 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 the key at the end and rearranges it
-    // so that the binary heap is in appropriate order
+    /**
+     * Inserts a new key into the min-priority queue.
+     *
+     * @param key the value to be inserted
+     */
     public void insert(int key) {
         if (this.isFull()) {
-            return;
+            throw new IllegalStateException("MinPriorityQueue is full. Cannot insert new element.");
         }
         this.heap[this.size + 1] = key;
         int k = this.size + 1;
@@ -44,89 +58,98 @@ public class MinPriorityQueue {
         this.size++;
     }
 
-    // returns the highest priority value
+    /**
+     * 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];
     }
 
-    // returns boolean value whether the heap is empty or not
+    /**
+     * Checks whether the queue is empty.
+     *
+     * @return true if the queue is empty, false otherwise
+     */
     public boolean isEmpty() {
-        return 0 == this.size;
+        return size == 0;
     }
 
-    // returns boolean value whether the heap is full or not
+    /**
+     * Checks whether the queue is full.
+     *
+     * @return true if the queue is full, false otherwise
+     */
     public boolean isFull() {
-        return this.size == this.capacity;
+        return size == capacity;
     }
 
-    // prints the heap
+    /**
+     * Prints the elements of the queue.
+     */
     public void print() {
-        for (int i = 1; i <= this.capacity; i++) {
+        for (int i = 1; i <= this.size; i++) {
             System.out.print(this.heap[i] + " ");
         }
         System.out.println();
     }
 
-    // heap sorting can be done by performing
-    // delete function to the number of times of the size of the heap
-    // it returns reverse sort because it is a min priority queue
+    /**
+     * Sorts the elements in the queue using heap sort.
+     */
     public void heapSort() {
-        for (int i = 1; i < this.capacity; i++) {
+        for (int i = 1; i <= this.size; i++) {
             this.delete();
         }
     }
 
-    // this function reorders the heap after every delete function
+    /**
+     * Reorders the heap after a deletion to maintain the heap property.
+     */
     private void sink() {
         int k = 1;
-        while (2 * k <= this.size || 2 * k + 1 <= this.size) {
-            int minIndex;
-            if (this.heap[2 * k] >= this.heap[k]) {
-                if (2 * k + 1 <= this.size && this.heap[2 * k + 1] >= this.heap[k]) {
-                    break;
-                } else if (2 * k + 1 > this.size) {
-                    break;
-                }
+        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) {
-                minIndex = this.heap[2 * k] < this.heap[k] ? 2 * k : k;
-            } else {
-                if (this.heap[k] > this.heap[2 * k] || this.heap[k] > this.heap[2 * k + 1]) {
-                    minIndex = this.heap[2 * k] < this.heap[2 * k + 1] ? 2 * k : 2 * k + 1;
-                } else {
-                    minIndex = k;
-                }
+            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;
+
+            k = minIndex; // Move down to the smallest child
         }
     }
 
-    // deletes the highest priority value from the heap
+    /**
+     * 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];
-        this.heap[this.size] = min;
+        this.heap[1] = this.heap[this.size]; // Move last element to the root
         this.size--;
         this.sink();
         return min;
     }
-
-    public static void main(String[] args) {
-        // testing
-        MinPriorityQueue q = new MinPriorityQueue(8);
-        q.insert(5);
-        q.insert(2);
-        q.insert(4);
-        q.insert(1);
-        q.insert(7);
-        q.insert(6);
-        q.insert(3);
-        q.insert(8);
-        q.print(); // [ 1, 2, 3, 5, 7, 6, 4, 8 ]
-        q.heapSort();
-        q.print(); // [ 8, 7, 6, 5, 4, 3, 2, 1 ]
-    }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/datastructures/heaps/MinPriorityQueueTest.java b/src/test/java/com/thealgorithms/datastructures/heaps/MinPriorityQueueTest.java
new file mode 100644
index 00000000..8f93bd63
--- /dev/null
+++ b/src/test/java/com/thealgorithms/datastructures/heaps/MinPriorityQueueTest.java
@@ -0,0 +1,87 @@
+package com.thealgorithms.datastructures.heaps;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class MinPriorityQueueTest {
+
+    @Test
+    void testInsertAndPeek() {
+        MinPriorityQueue queue = new MinPriorityQueue(5);
+        queue.insert(10);
+        queue.insert(5);
+        queue.insert(15);
+
+        Assertions.assertEquals(5, queue.peek(), "The minimum element should be 5.");
+    }
+
+    @Test
+    void testDelete() {
+        MinPriorityQueue queue = new MinPriorityQueue(5);
+        queue.insert(10);
+        queue.insert(5);
+        queue.insert(15);
+
+        Assertions.assertEquals(5, queue.delete(), "The deleted minimum element should be 5.");
+        Assertions.assertEquals(10, queue.peek(), "After deletion, the new minimum should be 10.");
+    }
+
+    @Test
+    void testIsEmpty() {
+        MinPriorityQueue queue = new MinPriorityQueue(5);
+        Assertions.assertTrue(queue.isEmpty(), "The queue should be empty initially.");
+
+        queue.insert(10);
+        Assertions.assertFalse(queue.isEmpty(), "The queue should not be empty after insertion.");
+    }
+
+    @Test
+    void testIsFull() {
+        MinPriorityQueue queue = new MinPriorityQueue(2);
+        queue.insert(10);
+        queue.insert(5);
+
+        Assertions.assertTrue(queue.isFull(), "The queue should be full after inserting two elements.");
+        queue.delete();
+        Assertions.assertFalse(queue.isFull(), "The queue should not be full after deletion.");
+    }
+
+    @Test
+    void testHeapSort() {
+        MinPriorityQueue queue = new MinPriorityQueue(5);
+        queue.insert(10);
+        queue.insert(5);
+        queue.insert(15);
+        queue.insert(1);
+        queue.insert(3);
+
+        // Delete all elements to sort the queue
+        int[] sortedArray = new int[5];
+        for (int i = 0; i < 5; i++) {
+            sortedArray[i] = queue.delete();
+        }
+
+        Assertions.assertArrayEquals(new int[] {1, 3, 5, 10, 15}, sortedArray, "The array should be sorted in ascending order.");
+    }
+
+    @Test
+    void testPeekEmptyQueue() {
+        MinPriorityQueue queue = new MinPriorityQueue(5);
+        Assertions.assertThrows(IllegalStateException.class, queue::peek, "Should throw an exception when peeking into an empty queue.");
+    }
+
+    @Test
+    void testDeleteEmptyQueue() {
+        MinPriorityQueue queue = new MinPriorityQueue(5);
+        Assertions.assertThrows(IllegalStateException.class, queue::delete, "Should throw an exception when deleting from an empty queue.");
+    }
+
+    @Test
+    void testInsertWhenFull() {
+        MinPriorityQueue queue = new MinPriorityQueue(2);
+        queue.insert(10);
+        queue.insert(5);
+
+        Assertions.assertThrows(IllegalStateException.class, () -> queue.insert(15), "Should throw an exception when inserting into a full queue.");
+    }
+}