diff --git a/data_structures/heaps/MaxHeap.java b/data_structures/heaps/MaxHeap.java index e4e1a89fa..6768d4871 100644 --- a/data_structures/heaps/MaxHeap.java +++ b/data_structures/heaps/MaxHeap.java @@ -84,6 +84,7 @@ public class MaxHeap implements Heap { @Override public void deleteElement(int elementIndex) { + if (isempty(maxHeap)) throw new EmptyHeapException("Attempt to delete an element from an empty heap"); if ((elementIndex > maxHeap.size()) && (elementIndex <= 0)) throw new IndexOutOfBoundsException("Index out of heap range"); // The last element in heap replaces the one to be deleted maxHeap.set(elementIndex - 1, getElement(maxHeap.size())); diff --git a/data_structures/heaps/MinHeap.java b/data_structures/heaps/MinHeap.java index 5714c74cf..cd484d79f 100644 --- a/data_structures/heaps/MinHeap.java +++ b/data_structures/heaps/MinHeap.java @@ -87,6 +87,7 @@ public class MinHeap implements Heap { @Override public void deleteElement(int elementIndex) { + if (isempty(maxHeap)) throw new EmptyHeapException("Attempt to delete an element from an empty heap"); if ((elementIndex > minHeap.size()) && (elementIndex <= 0)) throw new IndexOutOfBoundsException("Index out of heap range"); // The last element in heap replaces the one to be deleted minHeap.set(elementIndex - 1, getElement(minHeap.size()));