diff --git a/src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap b/src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java similarity index 100% rename from src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap rename to src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java diff --git a/src/main/java/com/thealgorithms/datastructures/heaps/MaxHeap.java b/src/main/java/com/thealgorithms/datastructures/heaps/MaxHeap.java index 13eb7a20c..e19d4c718 100644 --- a/src/main/java/com/thealgorithms/datastructures/heaps/MaxHeap.java +++ b/src/main/java/com/thealgorithms/datastructures/heaps/MaxHeap.java @@ -43,6 +43,10 @@ public class MaxHeap implements Heap { // Get the key of the element at a given index private double getElementKey(int elementIndex) { + if ((elementIndex <= 0) || (elementIndex > maxHeap.size())) { + throw new IndexOutOfBoundsException("Index out of heap range"); + } + return maxHeap.get(elementIndex - 1).getKey(); } diff --git a/src/main/java/com/thealgorithms/datastructures/heaps/MinHeap.java b/src/main/java/com/thealgorithms/datastructures/heaps/MinHeap.java index b25796154..6d5d6870c 100644 --- a/src/main/java/com/thealgorithms/datastructures/heaps/MinHeap.java +++ b/src/main/java/com/thealgorithms/datastructures/heaps/MinHeap.java @@ -37,6 +37,10 @@ public class MinHeap implements Heap { // Get the key of the element at a given index private double getElementKey(int elementIndex) { + if ((elementIndex <= 0) || (elementIndex > minHeap.size())) { + throw new IndexOutOfBoundsException("Index out of heap range"); + } + return minHeap.get(elementIndex - 1).getKey(); }