Add index validation to Min Heap and Max Heap (#3189)
Co-authored-by: Andrii Siriak <siryaka@gmail.com>
This commit is contained in:
parent
b2f6827c36
commit
1a9937c7cb
@ -43,6 +43,10 @@ public class MaxHeap implements Heap {
|
|||||||
|
|
||||||
// Get the key of the element at a given index
|
// Get the key of the element at a given index
|
||||||
private double getElementKey(int elementIndex) {
|
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();
|
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
|
// Get the key of the element at a given index
|
||||||
private double getElementKey(int elementIndex) {
|
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();
|
return minHeap.get(elementIndex - 1).getKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user