Package com.github.tommyettinger.ds
Class BinaryHeap<T extends BinaryHeap.Node>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractQueue<T>
com.github.tommyettinger.ds.BinaryHeap<T>
- All Implemented Interfaces:
EnhancedCollection<T>,Iterable<T>,Collection<T>,Queue<T>
public class BinaryHeap<T extends BinaryHeap.Node>
extends AbstractQueue<T>
implements EnhancedCollection<T>
A binary heap that stores nodes which each have a float value and are sorted either lowest first or highest first.
This can expand if its capacity is exceeded. It defaults to acting as a min-heap, sorting lowest-first.
The
This isn't a direct copy from libGDX, but it's very close. It implements
BinaryHeap.Node class can be extended to store additional information.
This isn't a direct copy from libGDX, but it's very close. It implements
Queue and Collection.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBinaryHeap.HeapIterator<T extends BinaryHeap.Node>static classA binary heap node. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected BinaryHeap.HeapIterator<T>protected BinaryHeap.HeapIterator<T>int -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a BinaryHeap with 16 starting capacity, sorting lowest-first (a min-heap).BinaryHeap(boolean isMaxHeap, Collection<? extends T> coll) Constructs a BinaryHeap with the specified sorting order and the contents from the given Collection of nodes.BinaryHeap(boolean isMaxHeap, T[] arr) Constructs a BinaryHeap with the specified sorting order and the contents from the given array of nodes.BinaryHeap(int capacity, boolean isMaxHeap) Constructs a BinaryHeap with the specified capacity and sorting order.BinaryHeap(Collection<? extends T> coll) Constructs a BinaryHeap with the contents from the given Collection of nodes, sorting lowest-first (a min-heap).BinaryHeap(T[] arr) Constructs a BinaryHeap with the contents from the given array of nodes, sorting lowest-first (a min-heap). -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdds the node to the heap using its current value.booleanSets the node's value and adds it to the heap.booleanaddAll(Collection<? extends T> c) booleanbooleanvoidclear()Removes all nodes from this BinaryHeap.booleanReturns true if the heap contains the specified node.booleanReturns true if the heap contains the specified node.booleancontainsAll(Object[] array) Exactly likecontainsAll(Collection), but takes an array instead of a Collection.booleancontainsAll(Object[] array, int offset, int length) booleancontainsAll(Collection<?> c) booleancontainsAny(Object[] values) Returns true if this set contains any of the specified values.booleancontainsAny(Object[] values, int offset, int length) Returns true if this set contains any items from the specified range of values.booleancontainsAnyIterable(Iterable<?> values) Returns true if this set contains any of the specified values.element()Returns the first item in the heap.booleaninthashCode()booleanisEmpty()Returns true if the heap is empty.booleanReturns true if this is a max-heap (that is, it sorts highest-first), or false if this is a min-heap (it sorts lowest-first).iterator()Returns an iterator over the elements contained in this collection.static <T extends BinaryHeap.Node>
BinaryHeap<T>maxHeapWith(T... array) Builds a BinaryHeap with the max-heap property from the given array or varargs of items that extendBinaryHeap.Node.static <T extends BinaryHeap.Node>
BinaryHeap<T>minHeapWith(T... array) Builds a BinaryHeap with the min-heap property from the given array or varargs of items that extendBinaryHeap.Node.booleannotEmpty()Returns true if the heap has one or more items.booleanInserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions.peek()Returns the first item in the heap.poll()Retrieves and removes the head of this queue, or returnsnullif this BinaryHeap is empty.pop()Retrieves and removes the head of this queue, or returnsnullif this BinaryHeap is empty.remove()Retrieves and removes the head of this queue.booleanRemoves the given node and returns it.booleanExactly likeremoveAll(Collection), but takes an array instead of a Collection.booleanbooleanremoveAll(Collection<?> other) Removes each object inotherfrom this heap, removing an item once if it appears once, twice if it appears twice, and so on.booleanremoveEach(Object[] array) Exactly likeremoveEachIterable(Iterable), but takes an array instead of a Collection.booleanremoveEach(Object[] array, int offset, int length) booleanremoveEachIterable(Iterable<?> other) Removes from this ObjectList element-wise occurrences of elements contained in the specified Iterable.voidChanges the value of the node, which should already be in the heap.intsize()toString()static <T extends BinaryHeap.Node>
BinaryHeap<T>with(T... array) Builds a BinaryHeap with the min-heap property from the given array or varargs of items that extendBinaryHeap.Node.Methods inherited from class java.util.AbstractCollection
retainAll, toArray, toArrayMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, retainAll, spliterator, stream, toArray, toArray, toArrayMethods inherited from interface com.github.tommyettinger.ds.EnhancedCollection
add, add, add, addAll, addAllIterable, addLegible, addLegible, addVarargs, appendTo, appendTo, containsAll, containsAllIterable, containsAny, first, removeAll, removeAllIterable, removeEach, toString, toString, toString
-
Field Details
-
size
public int size -
iterator1
-
iterator2
-
-
Constructor Details
-
BinaryHeap
public BinaryHeap()Constructs a BinaryHeap with 16 starting capacity, sorting lowest-first (a min-heap). -
BinaryHeap
public BinaryHeap(int capacity, boolean isMaxHeap) Constructs a BinaryHeap with the specified capacity and sorting order.- Parameters:
capacity- the initial capacityisMaxHeap- if true, this will sort highest-first; if false, it will sort lowest-first
-
BinaryHeap
Constructs a BinaryHeap with the contents from the given Collection of nodes, sorting lowest-first (a min-heap). If a duplicate node is present incoll, all repeats are ignored.- Parameters:
coll- a Collection of T (which must extendBinaryHeap.Node) or objects that subclass T
-
BinaryHeap
Constructs a BinaryHeap with the specified sorting order and the contents from the given Collection of nodes. If a duplicate node is present incoll, all repeats are ignored.- Parameters:
isMaxHeap- if true, this will sort highest-first; if false, it will sort lowest-firstcoll- a Collection of T (which must extendBinaryHeap.Node) or objects that subclass T
-
BinaryHeap
Constructs a BinaryHeap with the contents from the given array of nodes, sorting lowest-first (a min-heap). If a duplicate node is present inarr, all repeats are ignored.- Parameters:
arr- an array of T (which must extendBinaryHeap.Node) or objects that subclass T
-
BinaryHeap
Constructs a BinaryHeap with the specified sorting order and the contents from the given array of nodes. If a duplicate node is present inarr, all repeats are ignored.- Parameters:
isMaxHeap- if true, this will sort highest-first; if false, it will sort lowest-firstarr- an array of T (which must extendBinaryHeap.Node) or objects that subclass T
-
-
Method Details
-
isMaxHeap
public boolean isMaxHeap()Returns true if this is a max-heap (that is, it sorts highest-first), or false if this is a min-heap (it sorts lowest-first). If not specified, this is usually false; it can be set only in the constructor, such asBinaryHeap(int, boolean)orBinaryHeap(boolean, Collection).- Returns:
- true if this sorts highest-first; false if it sorts lowest-first
-
addAll
- Specified by:
addAllin interfaceCollection<T extends BinaryHeap.Node>- Overrides:
addAllin classAbstractQueue<T extends BinaryHeap.Node>
-
addAll
- Specified by:
addAllin interfaceEnhancedCollection<T extends BinaryHeap.Node>
-
addAll
- Specified by:
addAllin interfaceEnhancedCollection<T extends BinaryHeap.Node>
-
add
Adds the node to the heap using its current value. The node should not already be in the heap.- Specified by:
addin interfaceCollection<T extends BinaryHeap.Node>- Specified by:
addin interfaceQueue<T extends BinaryHeap.Node>- Overrides:
addin classAbstractQueue<T extends BinaryHeap.Node>
-
offer
Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. You can also useadd(Node), but if you try to add a duplicate element with that, anIllegalStateExceptionis thrown. Here, if you try to add a duplicate element, no Exception is thrown and this returnsfalse.- Specified by:
offerin interfaceQueue<T extends BinaryHeap.Node>- Parameters:
node- the element to add; must not be null- Returns:
trueif the element was added to this queue, elsefalse(typically when node is already present in this BinaryHeap)- Throws:
ClassCastException- if the class of the specified element prevents it from being added to this queueNullPointerException- if the specified element is null
-
poll
Retrieves and removes the head of this queue, or returnsnullif this BinaryHeap is empty. The head is the item with the lowest value (or highest value if this heap is configured as a max heap).- Specified by:
pollin interfaceQueue<T extends BinaryHeap.Node>- Returns:
- the head of this BinaryHeap, or
nullif this queue is empty - Throws:
ClassCastException- if the class of the specified element prevents it from being added to this queue
-
remove
Retrieves and removes the head of this queue. This method differs frompoll()only in that it throws an exception if this queue is empty, and won't return null.- Specified by:
removein interfaceQueue<T extends BinaryHeap.Node>- Overrides:
removein classAbstractQueue<T extends BinaryHeap.Node>- Returns:
- the head of this queue
- Throws:
NoSuchElementException- if this queue is emptyClassCastException- if the class of the specified element prevents it from being added to this queue
-
add
Sets the node's value and adds it to the heap. The node should not already be in the heap. -
contains
Returns true if the heap contains the specified node. Exactly the same ascontains(Object, boolean)withidentityset to false.- Specified by:
containsin interfaceCollection<T extends BinaryHeap.Node>- Overrides:
containsin classAbstractCollection<T extends BinaryHeap.Node>- Parameters:
node- should be aT, which must extendBinaryHeap.Node; can be some other type, which gives false- "Implementation Requirements"
- This implementation iterates over the elements in the collection,
checking each element in turn for equality with the specified element via
Object.equals(Object).
-
contains
Returns true if the heap contains the specified node.- Parameters:
node- should be aT, which must extendBinaryHeap.Node; can be some other type, which gives falseidentity- If true, == comparison will be used. If false, .equals() comparison will be used.
-
element
Returns the first item in the heap. This is the item with the lowest value (or highest value if this heap is configured as a max heap). If the heap is empty, throws anNoSuchElementException.- Specified by:
elementin interfaceQueue<T extends BinaryHeap.Node>- Overrides:
elementin classAbstractQueue<T extends BinaryHeap.Node>- Returns:
- the first item in the heap
- Throws:
NoSuchElementException- if the heap is empty.ClassCastException- if the class of the specified element prevents it from being added to this queue
-
peek
Returns the first item in the heap. This is the item with the lowest value (or highest value if this heap is configured as a max heap). If the heap is empty, returns null.- Specified by:
peekin interfaceQueue<T extends BinaryHeap.Node>- Returns:
- the first item in the heap, or null if the heap is empty
- Throws:
ClassCastException- if the class of the specified element prevents it from being added to this queue
-
pop
Retrieves and removes the head of this queue, or returnsnullif this BinaryHeap is empty. The head is the item with the lowest value (or highest value if this heap is configured as a max heap).
This method is identical topoll()in this class, but because poll() is defined as part of the Queue interface, whereas this method was defined ad-hoc by libGDX, poll() should be preferred in new code.- Returns:
- the head of this BinaryHeap, or
nullif this queue is empty
-
remove
Removes the given node and returns it. If the node is not present in this BinaryHeap or is invalid, this will probably throw an Exception.- Parameters:
node- aBinaryHeap.Nodethat should be present in this already- Returns:
nodeafter its removal
-
size
public int size()- Specified by:
sizein interfaceCollection<T extends BinaryHeap.Node>- Specified by:
sizein classAbstractCollection<T extends BinaryHeap.Node>
-
notEmpty
public boolean notEmpty()Returns true if the heap has one or more items. -
isEmpty
public boolean isEmpty()Returns true if the heap is empty.- Specified by:
isEmptyin interfaceCollection<T extends BinaryHeap.Node>- Overrides:
isEmptyin classAbstractCollection<T extends BinaryHeap.Node>
-
clear
public void clear()Removes all nodes from this BinaryHeap.- Specified by:
clearin interfaceCollection<T extends BinaryHeap.Node>- Overrides:
clearin classAbstractQueue<T extends BinaryHeap.Node>
-
setValue
Changes the value of the node, which should already be in the heap. -
remove
- Specified by:
removein interfaceCollection<T extends BinaryHeap.Node>- Overrides:
removein classAbstractCollection<T extends BinaryHeap.Node>
-
containsAll
- Specified by:
containsAllin interfaceCollection<T extends BinaryHeap.Node>- Overrides:
containsAllin classAbstractCollection<T extends BinaryHeap.Node>
-
containsAll
Exactly likecontainsAll(Collection), but takes an array instead of a Collection.- Specified by:
containsAllin interfaceEnhancedCollection<T extends BinaryHeap.Node>- Parameters:
array- array to be checked for containment in this set- Returns:
trueif this set contains all the elements in the specified array- See Also:
-
containsAll
- Specified by:
containsAllin interfaceEnhancedCollection<T extends BinaryHeap.Node>- Parameters:
array- array to be checked for containment in this setoffset- the index of the first item in array to checklength- how many items, at most, to check from array- Returns:
trueif this set contains all the elements in the specified range of array- See Also:
-
containsAnyIterable
Returns true if this set contains any of the specified values.- Specified by:
containsAnyIterablein interfaceEnhancedCollection<T extends BinaryHeap.Node>- Parameters:
values- must not contain nulls, and must not be null itself- Returns:
- true if this set contains any of the items in
values, false otherwise
-
containsAny
Returns true if this set contains any of the specified values.- Specified by:
containsAnyin interfaceEnhancedCollection<T extends BinaryHeap.Node>- Parameters:
values- must not contain nulls, and must not be null itself- Returns:
- true if this set contains any of the items in
values, false otherwise
-
containsAny
Returns true if this set contains any items from the specified range of values.- Specified by:
containsAnyin interfaceEnhancedCollection<T extends BinaryHeap.Node>- Parameters:
values- must not contain nulls, and must not be null itselfoffset- the index to start checking in valueslength- how many items to check from values- Returns:
- true if this set contains any of the items in the given range of
values, false otherwise
-
removeAll
Removes each object inotherfrom this heap, removing an item once if it appears once, twice if it appears twice, and so on. In this respect, this acts likeremoveEachIterable(Iterable)rather than Collection's removeAll().- Specified by:
removeAllin interfaceCollection<T extends BinaryHeap.Node>- Overrides:
removeAllin classAbstractCollection<T extends BinaryHeap.Node>- Parameters:
other- collection containing elements to be removed from this collection- Returns:
- true if any elements were removed, or false otherwise
- See Also:
-
removeAll
Exactly likeremoveAll(Collection), but takes an array instead of a Collection. This delegates entirely toremoveEach(Object[]), and does not act like removeAll() does in other collections if there are duplicate nodes present in the heap.- Specified by:
removeAllin interfaceEnhancedCollection<T extends BinaryHeap.Node>- Parameters:
other- array containing elements to be removed from this list- Returns:
trueif this list changed as a result of the call- See Also:
-
removeAll
LikeremoveAll(Object[]), but only uses at mostlengthitems fromarray, starting atoffset. This delegates entirely toremoveEach(Object[], int, int), and does not act like removeAll() does in other collections if there are duplicate nodes present in the heap.- Specified by:
removeAllin interfaceEnhancedCollection<T extends BinaryHeap.Node>- Parameters:
array- the elements to be removed from this listoffset- the index of the first item in array to removelength- how many items, at most, to get from array and remove from this- Returns:
trueif this list changed as a result of the call- See Also:
-
removeEachIterable
Removes from this ObjectList element-wise occurrences of elements contained in the specified Iterable. Note that if a value is present more than once in this ObjectList, only one of those occurrences will be removed for each occurrence of that value inother. Ifotherhas the same contents as this ObjectList or has additional items, then removing each ofotherwill clear this.- Specified by:
removeEachIterablein interfaceEnhancedCollection<T extends BinaryHeap.Node>- Parameters:
other- an Iterable of T items to remove one-by-one, such as another ObjectList or an ObjectSet- Returns:
- true if this list was modified.
-
removeEach
Exactly likeremoveEachIterable(Iterable), but takes an array instead of a Collection.- Specified by:
removeEachin interfaceEnhancedCollection<T extends BinaryHeap.Node>- Parameters:
array- array containing elements to be removed from this list- Returns:
trueif this list changed as a result of the call- See Also:
-
removeEach
- Specified by:
removeEachin interfaceEnhancedCollection<T extends BinaryHeap.Node>- Parameters:
array- the elements to be removed from this listoffset- the index of the first item in array to removelength- how many items, at most, to get from array and remove from this- Returns:
trueif this list changed as a result of the call- See Also:
-
equals
- Specified by:
equalsin interfaceCollection<T extends BinaryHeap.Node>- Overrides:
equalsin classObject
-
hashCode
public int hashCode()- Specified by:
hashCodein interfaceCollection<T extends BinaryHeap.Node>- Overrides:
hashCodein classObject
-
toString
- Overrides:
toStringin classAbstractCollection<T extends BinaryHeap.Node>
-
iterator
Returns an iterator over the elements contained in this collection.- Specified by:
iteratorin interfaceCollection<T extends BinaryHeap.Node>- Specified by:
iteratorin interfaceIterable<T extends BinaryHeap.Node>- Specified by:
iteratorin classAbstractCollection<T extends BinaryHeap.Node>- Returns:
- an iterator over the elements contained in this collection
-
with
Builds a BinaryHeap with the min-heap property from the given array or varargs of items that extendBinaryHeap.Node. This is equivalent tominHeapWith(Node[]).- Type Parameters:
T- must extendBinaryHeap.Node- Parameters:
array- an array or varargs of items that extendBinaryHeap.Node- Returns:
- a new BinaryHeap of T with the min-heap property.
-
minHeapWith
Builds a BinaryHeap with the min-heap property from the given array or varargs of items that extendBinaryHeap.Node. This is equivalent towith(Node[]).- Type Parameters:
T- must extendBinaryHeap.Node- Parameters:
array- an array or varargs of items that extendBinaryHeap.Node- Returns:
- a new BinaryHeap of T with the min-heap property.
-
maxHeapWith
Builds a BinaryHeap with the max-heap property from the given array or varargs of items that extendBinaryHeap.Node.- Type Parameters:
T- must extendBinaryHeap.Node- Parameters:
array- an array or varargs of items that extendBinaryHeap.Node- Returns:
- a new BinaryHeap of T with the max-heap property.
-