Interface Heap<T>
- Type Parameters:
T
- The type of elements stored in the heap.
- All Known Implementing Classes:
AdjustableBinaryHeap
,BinaryHeap
public interface Heap<T>
A heap is a data structure which satisfies the heap property. Depending on a compare operation by using either
Comparator
or Comparable
the best(minimum) element will always be at the root of the heap
and available to be queried in O(1) constant time.-
Method Summary
Modifier and TypeMethodDescriptionGet the comparator used to order items in this heap.boolean
isEmpty()
Checks if the heap is empty.peek()
Retrieves the best element of the heap without removing it.pop()
Removes and retrieves the best element of the heap, and ensures that the heap property is maintained after removal.int
Inserts an element into the heap and ensures that the minimum heap property is maintained.int
size()
Gets the number of elements currently stored in the heap.
-
Method Details
-
push
Inserts an element into the heap and ensures that the minimum heap property is maintained.- Parameters:
item
- The element to be inserted.- Returns:
- The new size of the heap after insertion.
-
peek
T peek()Retrieves the best element of the heap without removing it.- Returns:
- The best element of the heap, or
null
if the heap is empty.
-
pop
T pop()Removes and retrieves the best element of the heap, and ensures that the heap property is maintained after removal.- Returns:
- The best element of the heap.
- Throws:
NoSuchElementException
- if the heap is empty.
-
size
int size()Gets the number of elements currently stored in the heap.- Returns:
- The size of the heap.
-
isEmpty
boolean isEmpty()Checks if the heap is empty.- Returns:
true
if the heap is empty,false
otherwise.
-
comparator
Comparator<T> comparator()Get the comparator used to order items in this heap.- Returns:
- The comparator used to order items in this heap. Never null.
-