Adding exception throws in case of handling empty heap.

This commit is contained in:
Nicolas Renard 2016-12-13 15:52:17 +01:00
parent 4c14273d17
commit c3dda1f0dd
2 changed files with 2 additions and 0 deletions

View File

@ -84,6 +84,7 @@ public class MaxHeap implements Heap {
@Override @Override
public void deleteElement(int elementIndex) { 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"); 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 // The last element in heap replaces the one to be deleted
maxHeap.set(elementIndex - 1, getElement(maxHeap.size())); maxHeap.set(elementIndex - 1, getElement(maxHeap.size()));

View File

@ -87,6 +87,7 @@ public class MinHeap implements Heap {
@Override @Override
public void deleteElement(int elementIndex) { 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"); 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 // The last element in heap replaces the one to be deleted
minHeap.set(elementIndex - 1, getElement(minHeap.size())); minHeap.set(elementIndex - 1, getElement(minHeap.size()));