Class BinaryMinHeap<E,K>
java.lang.Object
rgu.transport.algorithms.collections.BinaryMinHeap<E,K>
- Type Parameters:
E- the type of elements, must be a hashable typeK- the type of key (also called priority)
- All Implemented Interfaces:
MinHeap<E,K>
A minimum heap implemented as a binary heap. All mutations are O(log n).
Not safe for concurrent use.
- Author:
- Lee A. Christie
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddElement(E element, K key) Adds the specified element with the specified key.voiddecreaseKey(E element, K key) Replaces the key of the specified element with a specified smaller value.Removes and returns the element with the minimum key.booleanisEmpty()Returnstrueif this heap contains no elements.toString()Returns a string representation of this heap.
-
Constructor Details
-
BinaryMinHeap
Constructs a newBinaryMinHeap.- Parameters:
lessThan- a pure predicate which tests whether a < b, not null
-
-
Method Details
-
isEmpty
-
addElement
Adds the specified element with the specified key.- Specified by:
addElementin interfaceMinHeap<E,K> - Parameters:
element- the element, not nullkey- the key, not null- Throws:
IllegalStateException- if the element already exists
-
decreaseKey
Replaces the key of the specified element with a specified smaller value.- Specified by:
decreaseKeyin interfaceMinHeap<E,K> - Parameters:
element- the element, not nullkey- the new, smaller key value, not null- Throws:
IllegalStateException- if the new key is not smaller than the current keyNoSuchElementException- if the element does not exist in the heap
-
deleteMinimum
Removes and returns the element with the minimum key.- Specified by:
deleteMinimumin interfaceMinHeap<E,K> - Returns:
- the element with the minimum value
- Throws:
NoSuchElementException- if the heap is empty
-
toString
-