mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 09:06:51 +08:00
Add index validation to Min Heap and Max Heap (#3189)
Co-authored-by: Andrii Siriak <siryaka@gmail.com>
This commit is contained in:
@ -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();
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user