-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractHeap.java
41 lines (34 loc) · 978 Bytes
/
AbstractHeap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.ds_algo.l_heap;
import java.util.Comparator;
@SuppressWarnings({"unchecked","unused"})
public abstract class AbstractHeap<E> implements Heap<E>{
protected int size;
protected Comparator<E> comparator;
public AbstractHeap(Comparator<E> comparator) {
this.comparator = comparator;
}
public AbstractHeap(){
this(null);
}
@Override
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return size == 0;
}
protected void emptyCheck(){
if (size == 0){
throw new IndexOutOfBoundsException("Heap is empty");
}
}
protected void elementNotNullCheck(E element){
if (element == null){
throw new IllegalArgumentException("element must not be null");
}
}
protected int compare(E e1, E e2) {
return comparator != null ? comparator.compare(e1,e2) : ((Comparable<E>)e1).compareTo(e2);
}
}