T
- type of the items on the heappublic class Heap<T>
extends java.lang.Object
While this provides similar functionality to PriorityQueue
data structure, one key difference is that external entities can control
when to restore the heap property, which is done through invocation of the
heapify()
method.
This class is not thread-safe and care must be taken to prevent concurrent modifications.
Constructor and Description |
---|
Heap(java.util.List<T> data,
java.util.Comparator<T> comparator)
Creates a new heap backed by the specified list.
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(java.lang.Object obj) |
T |
extractExtreme()
Extracts and returns the most extreme item from the heap.
|
T |
extreme()
Returns the most extreme item in the heap.
|
int |
hashCode() |
void |
heapify()
Restores the heap property by re-arranging the elements in the backing
array as necessary following any heap modifications.
|
Heap<T> |
insert(T item)
Inserts the specified item into the heap and returns the modified heap.
|
boolean |
isEmpty()
Returns true if there are no items in the heap.
|
java.util.Iterator<T> |
iterator()
Returns iterator to traverse the heap level-by-level.
|
int |
size()
Returns the current size of the heap.
|
java.lang.String |
toString() |
public Heap(java.util.List<T> data, java.util.Comparator<T> comparator)
data
- backing data listcomparator
- comparator for ordering the heap itemspublic void heapify()
public int size()
public boolean isEmpty()
public T extreme()
public T extractExtreme()
public Heap<T> insert(T item)
item
- item to be insertedjava.lang.IllegalArgumentException
- if the heap is already fullpublic java.util.Iterator<T> iterator()
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object