Add tests Generic Heap and add check null item (#5801)

This commit is contained in:
Giulio Tantaro
2024-10-25 19:56:54 +02:00
committed by GitHub
parent 3b2ba488bb
commit 131e5381be
2 changed files with 130 additions and 0 deletions

View File

@ -9,6 +9,10 @@ public class GenericHeap<T extends Comparable<T>> {
HashMap<T, Integer> map = new HashMap<>();
public void add(T item) {
if (item == null) {
throw new IllegalArgumentException("Cannot insert null into the heap.");
}
this.data.add(item);
map.put(item, this.data.size() - 1); //
upHeapify(this.data.size() - 1);