Skip to content

Commit c3dda1f

Browse files
committed
Adding exception throws in case of handling empty heap.
1 parent 4c14273 commit c3dda1f

2 files changed

Lines changed: 2 additions & 0 deletions

File tree

data_structures/heaps/MaxHeap.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public void insertElement(HeapElement element) {
8484

8585
@Override
8686
public void deleteElement(int elementIndex) {
87+
if (isempty(maxHeap)) throw new EmptyHeapException("Attempt to delete an element from an empty heap");
8788
if ((elementIndex > maxHeap.size()) && (elementIndex <= 0)) throw new IndexOutOfBoundsException("Index out of heap range");
8889
// The last element in heap replaces the one to be deleted
8990
maxHeap.set(elementIndex - 1, getElement(maxHeap.size()));

data_structures/heaps/MinHeap.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public void insertElement(HeapElement element) {
8787

8888
@Override
8989
public void deleteElement(int elementIndex) {
90+
if (isempty(maxHeap)) throw new EmptyHeapException("Attempt to delete an element from an empty heap");
9091
if ((elementIndex > minHeap.size()) && (elementIndex <= 0)) throw new IndexOutOfBoundsException("Index out of heap range");
9192
// The last element in heap replaces the one to be deleted
9293
minHeap.set(elementIndex - 1, getElement(minHeap.size()));

0 commit comments

Comments
 (0)