Class BinaryHeap<T>
java.lang.Object
com.abstractkamen.datastructures.impl.heaps.BinaryHeap<T>
- Type Parameters:
T
- The type of elements stored in the binary heap.
- All Implemented Interfaces:
Heap<T>
,MergeableHeap<T>
- Direct Known Subclasses:
AdjustableBinaryHeap
The elements of the binary heap are ordered according to their natural ordering,
or by a Comparator provided at construction time, depending on which
constructor is used. This implementation does not permit null elements.
-
Field Summary
-
Constructor Summary
ConstructorDescriptionBinaryHeap
(Comparator<T> comparator) Create anBinaryHeap<T>
with a custom comparator.BinaryHeap
(Comparator<T> comparator, int capacity) Create anBinaryHeap<T>
with a custom comparator and capacity. -
Method Summary
Modifier and TypeMethodDescriptionGet the comparator used to order items in this heap.static <T extends Comparable<T>>
BinaryHeap<T>Create anBinaryHeap<T>
with natural order comparator in a type safe way.static <T extends Comparable<T>>
BinaryHeap<T>createComparable
(int capacity) Create anBinaryHeap<T>
with natural order comparator in a type safe way.protected Object[]
getItems()
protected static <T> void
heapifyDown
(Object[] items, Comparator<T> comparator, int i, int size) protected static <T> void
heapifyUp
(Object[] items, Comparator<T> comparator, int i) boolean
isEmpty()
Checks if the heap is empty.Merges this heap with the other usingthis.
Heap.comparator()
.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.void
Restores the minimum heap property of the binary heap after modifications to its elements.int
size()
Gets the number of elements currently stored in the heap.toString()
-
Field Details
-
DEFAULT_CAPACITY
protected static final int DEFAULT_CAPACITY- See Also:
-
-
Constructor Details
-
BinaryHeap
Create anBinaryHeap<T>
with a custom comparator and capacity.- Parameters:
comparator
- custom comparatorcapacity
- initial capacity
-
BinaryHeap
Create anBinaryHeap<T>
with a custom comparator.- Parameters:
comparator
- custom comparator
-
-
Method Details
-
createComparable
Create anBinaryHeap<T>
with natural order comparator in a type safe way.- Type Parameters:
T
- comparable type
-
createComparable
Create anBinaryHeap<T>
with natural order comparator in a type safe way.- Type Parameters:
T
- comparable type- Parameters:
capacity
- initial capacity
-
size
public int size()Description copied from interface:Heap
Gets the number of elements currently stored in the heap. -
isEmpty
public boolean isEmpty()Description copied from interface:Heap
Checks if the heap is empty. -
push
Description copied from interface:Heap
Inserts an element into the heap and ensures that the minimum heap property is maintained. -
peek
Description copied from interface:Heap
Retrieves the best element of the heap without removing it. -
pop
Description copied from interface:Heap
Removes and retrieves the best element of the heap, and ensures that the heap property is maintained after removal. -
toString
-
comparator
Description copied from interface:Heap
Get the comparator used to order items in this heap.- Specified by:
comparator
in interfaceHeap<T>
- Returns:
- The comparator used to order items in this heap. Never null.
-
mergeWith
Description copied from interface:MergeableHeap
Merges this heap with the other usingthis.
Heap.comparator()
. The heaps will be merged regardless ofother
's order. Depending on the implementation this may have an impact on performance.Example:
MergeableHeap<Heap<Integer>> a = new SomeMergeableHeapImpl(Integer::compare);
// can be merged withMergeableHeap<Heap<Integer>> b = new SomeMergeableHeapImpl(Comparator.reverseOrder(Integer::compare));
// different implementations will have different time complexity a.mergeWith(b);- Specified by:
mergeWith
in interfaceMergeableHeap<T>
- Parameters:
other
- heap- Returns:
this
merged with other
-
restoreHeapOrder
public void restoreHeapOrder()Restores the minimum heap property of the binary heap after modifications to its elements. This method should be called when some elements' keys are mutated to ensure that the minimum heap property is maintained throughout the binary heap.The method traverses the binary heap from the last parent node to the root, applying the heapifyDown operation to each node. This process rearranges the elements to satisfy the minimum heap property, considering the updated keys.
Note: This method assumes that the comparator used to create the binary heap is consistent with the keys of the elements, and the elements are unique based on their keys.
-
getItems
-
heapifyDown
-
heapifyUp
-