Add index validation to Min Heap and Max Heap (#3189)

Co-authored-by: Andrii Siriak <siryaka@gmail.com>
This commit is contained in:
Jainam Kothari 2022-07-13 23:10:21 +05:30 committed by GitHub
parent b2f6827c36
commit 1a9937c7cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 0 deletions

View File

@ -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();
}

View File

@ -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();
}