Adding exception throws in case of handling empty heap.
This commit is contained in:
parent
4c14273d17
commit
c3dda1f0dd
@ -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()));
|
||||||
|
@ -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()));
|
||||||
|
Loading…
Reference in New Issue
Block a user