* The second heap becomes empty and unusable after the meld operation, meaning * than further insertions are not possible and will throw an * {@link IllegalStateException}. * *
* A {@link ClassCastException} will be thrown if the two heaps are not of the * same type. Moreover, the two heaps need to use the same comparators. If only * one of them uses a custom comparator or both use custom comparators but are * not the same by equals, an {@code IllegalArgumentException} is * thrown. * *
* Note that all running time bounds on mergeable heaps are valid assuming that * the user does not perform cascading melds on heaps such as: * *
* d.meld(e); * c.meld(d); * b.meld(c); * a.meld(b); ** * The above scenario, although efficiently supported by using union-find with * path compression, invalidates the claimed bounds. * * @param
* The second heap becomes empty and unusable after the meld operation, meaning * than further insertions are not possible and will throw an * {@link IllegalStateException}. * *
* A {@link ClassCastException} will be thrown if the two heaps are not of the * same type. Moreover, the two heaps need to use the same comparators. If only * one of them uses a custom comparator or both use custom comparators but are * not the same by equals, an {@code IllegalArgumentException} is * thrown. * *
* Note that all running time bounds on mergeable heaps are valid assuming that * the user does not perform cascading melds on heaps such as: * *
* d.meld(e); * c.meld(d); * b.meld(c); * a.meld(b); ** * The above scenario, although efficiently supported by using union-find with * path compression, invalidates the claimed bounds. * * @param
* The second heap becomes empty and unusable after the meld operation, meaning * than further insertions are not possible and will throw an * {@link IllegalStateException}. * *
* A {@link ClassCastException} will be thrown if the two heaps are not of the * same type. Moreover, the two heaps need to use the same comparators. If only * one of them uses a custom comparator or both use custom comparators but are * not the same by equals, an {@code IllegalArgumentException} is * thrown. * *
* Note that all running time bounds on mergeable heaps are valid assuming that * the user does not perform cascading melds on heaps such as: * *
* d.meld(e); * c.meld(d); * b.meld(c); * a.meld(b); ** * The above scenario, although efficiently supported by using union-find with * path compression, invalidates the claimed bounds. * * @param
* The implementation uses an array in order to store the elements and * automatically maintains the size of the array much like a * {@link java.util.Vector} does, providing amortized O(log(n)) time cost for * the {@code insert} and {@code deleteMin} operations. Operation * {@code findMin}, is a worst-case O(1) operation. Operations {@code delete} * and {@code decreaseKey} take worst-case O(log(n)) time. The bounds are * worst-case if the user initializes the heap with a capacity larger or equal * to the total number of elements that are going to be inserted into the heap. * *
* Constructing such a heap from an array of elements can be performed using the * method {@link #heapify(Object[], Object[])} or * {@link #heapify(Object[], Object[], Comparator)} in linear time. * *
* Note that the ordering maintained by a binary heap, like any heap, and * whether or not an explicit comparator is provided, must be consistent * with {@code equals} if this heap is to correctly implement the * {@code Heap} interface. (See {@code Comparable} or {@code Comparator} for a * precise definition of consistent with equals.) This is so because * the {@code Heap} interface is defined in terms of the {@code equals} * operation, but a binary heap performs all key comparisons using its * {@code compareTo} (or {@code compare}) method, so two keys that are deemed * equal by this method are, from the standpoint of the binary heap, equal. The * behavior of a heap is well-defined even if its ordering is * inconsistent with {@code equals}; it just fails to obey the general contract * of the {@code Heap} interface. * *
* Note that this implementation is not synchronized. If
* multiple threads access a heap concurrently, and at least one of the threads
* modifies the heap structurally, it must be synchronized externally.
* (A structural modification is any operation that adds or deletes one or more
* elements or changing the key of some element.) This is typically accomplished
* by synchronizing on some object that naturally encapsulates the heap.
*
* @param
* All keys inserted into the heap must implement the {@link Comparable}
* interface. Furthermore, all such keys must be mutually
* comparable: {@code k1.compareTo(k2)} must not throw a
* {@code ClassCastException} for any keys {@code k1} and {@code k2} in the
* heap. If the user attempts to put a key into the heap that violates this
* constraint (for example, the user attempts to put a string key into a
* heap whose keys are integers), the {@code insert(Object key)} call will
* throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is {@link #DEFAULT_HEAP_CAPACITY} and
* adjusts automatically based on the sequence of insertions and deletions.
*/
public BinaryArrayAddressableHeap() {
this(null, DEFAULT_HEAP_CAPACITY);
}
/**
* Constructs a new, empty heap, with a provided initial capacity using the
* natural ordering of its keys.
*
*
* All keys inserted into the heap must implement the {@link Comparable}
* interface. Furthermore, all such keys must be mutually
* comparable: {@code k1.compareTo(k2)} must not throw a
* {@code ClassCastException} for any keys {@code k1} and {@code k2} in the
* heap. If the user attempts to put a key into the heap that violates this
* constraint (for example, the user attempts to put a string key into a
* heap whose keys are integers), the {@code insert(Object key)} call will
* throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is provided by the user and is adjusted
* automatically based on the sequence of insertions and deletions. The
* capacity will never become smaller than the initial requested capacity.
*
* @param capacity
* the initial heap capacity
*/
public BinaryArrayAddressableHeap(int capacity) {
this(null, capacity);
}
/**
* Constructs a new, empty heap, ordered according to the given comparator.
*
*
* All keys inserted into the heap must be mutually comparable by
* the given comparator: {@code comparator.compare(k1,
* k2)} must not throw a {@code ClassCastException} for any keys {@code k1}
* and {@code k2} in the heap. If the user attempts to put a key into the
* heap that violates this constraint, the {@code insert(Object key)} call
* will throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is {@link #DEFAULT_HEAP_CAPACITY} and
* adjusts automatically based on the sequence of insertions and deletions.
*
* @param comparator
* the comparator that will be used to order this heap. If
* {@code null}, the {@linkplain Comparable natural ordering} of
* the keys will be used.
*/
public BinaryArrayAddressableHeap(Comparator super K> comparator) {
this(comparator, DEFAULT_HEAP_CAPACITY);
}
/**
* Constructs a new, empty heap, with a provided initial capacity ordered
* according to the given comparator.
*
*
* All keys inserted into the heap must be mutually comparable by
* the given comparator: {@code comparator.compare(k1,
* k2)} must not throw a {@code ClassCastException} for any keys {@code k1}
* and {@code k2} in the heap. If the user attempts to put a key into the
* heap that violates this constraint, the {@code insert(Object key)} call
* will throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is provided by the user and is adjusted
* automatically based on the sequence of insertions and deletions. The
* capacity will never become smaller than the initial requested capacity.
*
* @param comparator
* the comparator that will be used to order this heap. If
* {@code null}, the {@linkplain Comparable natural ordering} of
* the keys will be used.
* @param capacity
* the initial heap capacity
*/
public BinaryArrayAddressableHeap(Comparator super K> comparator, int capacity) {
super(comparator, capacity);
}
/**
* Create a heap from an array of elements. The elements of the array are
* not destroyed. The method has linear time complexity.
*
* @param
* The implementation uses an array in order to store the elements and
* automatically maintains the size of the array much like a
* {@link java.util.Vector} does, providing amortized O(1) time cost for the
* {@code insert} and amortized O(log(n)) for the {@code deleteMin} operation.
* Operation {@code findMin}, is a worst-case O(1) operation.
*
*
* Constructing such a heap from an array of elements can be performed using the
* method {@link #heapify(Object[])} or {@link #heapify(Object[], Comparator)}
* in linear time.
*
*
* Note that the ordering maintained by a binary heap, like any heap, and
* whether or not an explicit comparator is provided, must be consistent
* with {@code equals} if this heap is to correctly implement the
* {@code Heap} interface. (See {@code Comparable} or {@code Comparator} for a
* precise definition of consistent with equals.) This is so because
* the {@code Heap} interface is defined in terms of the {@code equals}
* operation, but a binary heap performs all key comparisons using its
* {@code compareTo} (or {@code compare}) method, so two keys that are deemed
* equal by this method are, from the standpoint of the binary heap, equal. The
* behavior of a heap is well-defined even if its ordering is
* inconsistent with {@code equals}; it just fails to obey the general contract
* of the {@code Heap} interface.
*
*
* Note that this implementation is not synchronized. If
* multiple threads access a heap concurrently, and at least one of the threads
* modifies the heap structurally, it must be synchronized externally.
* (A structural modification is any operation that adds or deletes one or more
* elements or changing the key of some element.) This is typically accomplished
* by synchronizing on some object that naturally encapsulates the heap.
*
* @param
* All keys inserted into the heap must implement the {@link Comparable}
* interface. Furthermore, all such keys must be mutually
* comparable: {@code k1.compareTo(k2)} must not throw a
* {@code ClassCastException} for any keys {@code k1} and {@code k2} in the
* heap. If the user attempts to put a key into the heap that violates this
* constraint (for example, the user attempts to put a string key into a
* heap whose keys are integers), the {@code insert(Object key)} call will
* throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is
* {@link BinaryArrayBulkInsertWeakHeap#DEFAULT_HEAP_CAPACITY} and adjusts
* automatically based on the sequence of insertions and deletions.
*/
public BinaryArrayBulkInsertWeakHeap() {
this(null, DEFAULT_HEAP_CAPACITY);
}
/**
* Constructs a new, empty heap, with a provided initial capacity using the
* natural ordering of its keys.
*
*
* All keys inserted into the heap must implement the {@link Comparable}
* interface. Furthermore, all such keys must be mutually
* comparable: {@code k1.compareTo(k2)} must not throw a
* {@code ClassCastException} for any keys {@code k1} and {@code k2} in the
* heap. If the user attempts to put a key into the heap that violates this
* constraint (for example, the user attempts to put a string key into a
* heap whose keys are integers), the {@code insert(Object key)} call will
* throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is provided by the user and is adjusted
* automatically based on the sequence of insertions and deletions.
*
* @param capacity
* the initial heap capacity
*/
public BinaryArrayBulkInsertWeakHeap(int capacity) {
this(null, capacity);
}
/**
* Constructs a new, empty heap, ordered according to the given comparator.
*
*
* All keys inserted into the heap must be mutually comparable by
* the given comparator: {@code comparator.compare(k1,
* k2)} must not throw a {@code ClassCastException} for any keys {@code k1}
* and {@code k2} in the heap. If the user attempts to put a key into the
* heap that violates this constraint, the {@code insert(Object key)} call
* will throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is
* {@link BinaryArrayBulkInsertWeakHeap#DEFAULT_HEAP_CAPACITY} and adjusts
* automatically based on the sequence of insertions and deletions.
*
* @param comparator
* the comparator that will be used to order this heap. If
* {@code null}, the {@linkplain Comparable natural ordering} of
* the keys will be used.
*/
public BinaryArrayBulkInsertWeakHeap(Comparator super K> comparator) {
this(comparator, DEFAULT_HEAP_CAPACITY);
}
/**
* Constructs a new, empty heap, with a provided initial capacity ordered
* according to the given comparator.
*
*
* All keys inserted into the heap must be mutually comparable by
* the given comparator: {@code comparator.compare(k1,
* k2)} must not throw a {@code ClassCastException} for any keys {@code k1}
* and {@code k2} in the heap. If the user attempts to put a key into the
* heap that violates this constraint, the {@code insert(Object key)} call
* will throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is provided by the user and is adjusted
* automatically based on the sequence of insertions and deletions.
*
* @param comparator
* the comparator that will be used to order this heap. If
* {@code null}, the {@linkplain Comparable natural ordering} of
* the keys will be used.
* @param capacity
* the initial heap capacity
*/
@SuppressWarnings("unchecked")
public BinaryArrayBulkInsertWeakHeap(Comparator super K> comparator, int capacity) {
super(comparator, capacity);
this.insertionBuffer = (K[]) new Object[INSERTION_BUFFER_CAPACITY];
this.insertionBufferSize = 0;
this.insertionBufferMinPos = 0;
}
/**
* {@inheritDoc}
*/
@Override
@ConstantTime
public boolean isEmpty() {
return size + insertionBufferSize == 0;
}
/**
* {@inheritDoc}
*/
@Override
@ConstantTime
public long size() {
return (long)size + insertionBufferSize;
}
/**
* {@inheritDoc}
*/
@Override
@ConstantTime
public void clear() {
size = 0;
insertionBufferSize = 0;
insertionBufferMinPos = 0;
}
/**
* {@inheritDoc}
*/
@Override
@ConstantTime
@SuppressWarnings("unchecked")
public K findMin() {
if (size + insertionBufferSize == 0) {
throw new NoSuchElementException();
}
if (insertionBufferSize == 0) {
return array[0];
} else if (size == 0) {
return insertionBuffer[insertionBufferMinPos];
} else {
K insertionBufferMin = insertionBuffer[insertionBufferMinPos];
if (comparator == null) {
if (((Comparable super K>) array[0]).compareTo(insertionBufferMin) <= 0) {
return array[0];
} else {
return insertionBufferMin;
}
} else {
if (comparator.compare(array[0], insertionBufferMin) <= 0) {
return array[0];
} else {
return insertionBufferMin;
}
}
}
}
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked")
@ConstantTime(amortized = true)
public void insert(K key) {
if (key == null) {
throw new NullPointerException("Null keys not permitted");
}
// add in buffer
insertionBuffer[insertionBufferSize++] = key;
if (isBulkInsertionBufferFull()) {
if (size + insertionBufferSize > array.length) {
// first try to double size
if (array.length == 0) {
ensureCapacity(1);
} else {
ensureCapacity(2 * array.length);
}
// if not enough, set to requested size
ensureCapacity(size + insertionBufferSize);
}
if (comparator == null) {
bulkInsert();
} else {
bulkInsertWithComparator();
}
} else if (insertionBufferSize > 1) {
// update minimum
K insertionBufferMin = insertionBuffer[insertionBufferMinPos];
if (comparator == null) {
if (((Comparable super K>) key).compareTo(insertionBufferMin) < 0) {
insertionBufferMinPos = insertionBufferSize - 1;
}
} else {
if (comparator.compare(key, insertionBufferMin) < 0) {
insertionBufferMinPos = insertionBufferSize - 1;
}
}
}
}
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked")
@LogarithmicTime(amortized = true)
public K deleteMin() {
if (size + insertionBufferSize == 0) {
throw new NoSuchElementException();
}
// where is the minimum
boolean deleteFromInsertionBuffer = false;
if (size == 0) {
deleteFromInsertionBuffer = true;
} else if (insertionBufferSize > 0) {
K arrayMin = array[0];
K insertionBufferMin = insertionBuffer[insertionBufferMinPos];
if (comparator == null) {
if (((Comparable super K>) insertionBufferMin).compareTo(arrayMin) < 0) {
deleteFromInsertionBuffer = true;
}
} else {
if (comparator.compare(insertionBufferMin, arrayMin) < 0) {
deleteFromInsertionBuffer = true;
}
}
}
K result;
if (deleteFromInsertionBuffer) {
result = insertionBuffer[insertionBufferMinPos];
insertionBuffer[insertionBufferMinPos] = insertionBuffer[insertionBufferSize - 1];
insertionBuffer[insertionBufferSize - 1] = null;
insertionBufferSize--;
insertionBufferMinPos = 0;
if (comparator == null) {
for (int i = 1; i < insertionBufferSize; i++) {
if (((Comparable super K>) insertionBuffer[i])
.compareTo(insertionBuffer[insertionBufferMinPos]) < 0) {
insertionBufferMinPos = i;
}
}
} else {
for (int i = 1; i < insertionBufferSize; i++) {
if (comparator.compare(insertionBuffer[i], insertionBuffer[insertionBufferMinPos]) < 0) {
insertionBufferMinPos = i;
}
}
}
} else {
result = array[0];
size--;
array[0] = array[size];
array[size] = null;
if (size > 1) {
if (comparator == null) {
fixdown(0);
} else {
fixdownWithComparator(0);
}
}
if (minCapacity <= array.length && 4 * size < array.length) {
ensureCapacity(array.length / 2);
}
}
return result;
}
/**
* Create a heap from an array of elements. The elements of the array are
* not destroyed. The method has linear time complexity.
*
* @param
* The implementation uses an array in order to store the elements and
* automatically maintains the size of the array much like a
* {@link java.util.Vector} does, providing amortized O(log(n)) time cost for
* the {@code insert} and {@code deleteMin} operations. Operation
* {@code findMin}, is a worst-case O(1) operation. The bounds are worst-case if
* the user initializes the heap with a capacity larger or equal to the total
* number of elements that are going to be inserted into the heap.
*
*
* Constructing such a heap from an array of elements can be performed using the
* method {@link #heapify(Object[])} or {@link #heapify(Object[], Comparator)}
* in linear time.
*
*
* Note that the ordering maintained by a binary heap, like any heap, and
* whether or not an explicit comparator is provided, must be consistent
* with {@code equals} if this heap is to correctly implement the
* {@code Heap} interface. (See {@code Comparable} or {@code Comparator} for a
* precise definition of consistent with equals.) This is so because
* the {@code Heap} interface is defined in terms of the {@code equals}
* operation, but a binary heap performs all key comparisons using its
* {@code compareTo} (or {@code compare}) method, so two keys that are deemed
* equal by this method are, from the standpoint of the binary heap, equal. The
* behavior of a heap is well-defined even if its ordering is
* inconsistent with {@code equals}; it just fails to obey the general contract
* of the {@code Heap} interface.
*
*
* Note that this implementation is not synchronized. If
* multiple threads access a heap concurrently, and at least one of the threads
* modifies the heap structurally, it must be synchronized externally.
* (A structural modification is any operation that adds or deletes one or more
* elements or changing the key of some element.) This is typically accomplished
* by synchronizing on some object that naturally encapsulates the heap.
*
* @param
* All keys inserted into the heap must implement the {@link Comparable}
* interface. Furthermore, all such keys must be mutually
* comparable: {@code k1.compareTo(k2)} must not throw a
* {@code ClassCastException} for any keys {@code k1} and {@code k2} in the
* heap. If the user attempts to put a key into the heap that violates this
* constraint (for example, the user attempts to put a string key into a
* heap whose keys are integers), the {@code insert(Object key)} call will
* throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is {@link #DEFAULT_HEAP_CAPACITY} and
* adjusts automatically based on the sequence of insertions and deletions.
*/
public BinaryArrayHeap() {
super(null, DEFAULT_HEAP_CAPACITY);
}
/**
* Constructs a new, empty heap, with a provided initial capacity using the
* natural ordering of its keys.
*
*
* All keys inserted into the heap must implement the {@link Comparable}
* interface. Furthermore, all such keys must be mutually
* comparable: {@code k1.compareTo(k2)} must not throw a
* {@code ClassCastException} for any keys {@code k1} and {@code k2} in the
* heap. If the user attempts to put a key into the heap that violates this
* constraint (for example, the user attempts to put a string key into a
* heap whose keys are integers), the {@code insert(Object key)} call will
* throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is provided by the user and is adjusted
* automatically based on the sequence of insertions and deletions. The
* capacity will never become smaller than the initial requested capacity.
*
* @param capacity
* the initial heap capacity
*/
public BinaryArrayHeap(int capacity) {
super(null, capacity);
}
/**
* Constructs a new, empty heap, ordered according to the given comparator.
*
*
* All keys inserted into the heap must be mutually comparable by
* the given comparator: {@code comparator.compare(k1,
* k2)} must not throw a {@code ClassCastException} for any keys {@code k1}
* and {@code k2} in the heap. If the user attempts to put a key into the
* heap that violates this constraint, the {@code insert(Object key)} call
* will throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is
* {@link BinaryArrayHeap#DEFAULT_HEAP_CAPACITY} and adjusts automatically
* based on the sequence of insertions and deletions.
*
* @param comparator
* the comparator that will be used to order this heap. If
* {@code null}, the {@linkplain Comparable natural ordering} of
* the keys will be used.
*/
public BinaryArrayHeap(Comparator super K> comparator) {
super(comparator, DEFAULT_HEAP_CAPACITY);
}
/**
* Constructs a new, empty heap, with a provided initial capacity ordered
* according to the given comparator.
*
*
* All keys inserted into the heap must be mutually comparable by
* the given comparator: {@code comparator.compare(k1,
* k2)} must not throw a {@code ClassCastException} for any keys {@code k1}
* and {@code k2} in the heap. If the user attempts to put a key into the
* heap that violates this constraint, the {@code insert(Object key)} call
* will throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is provided by the user and is adjusted
* automatically based on the sequence of insertions and deletions. The
* capacity will never become smaller than the initial requested capacity.
*
* @param comparator
* the comparator that will be used to order this heap. If
* {@code null}, the {@linkplain Comparable natural ordering} of
* the keys will be used.
* @param capacity
* the initial heap capacity
*/
public BinaryArrayHeap(Comparator super K> comparator, int capacity) {
super(comparator, capacity);
}
/**
* Create a heap from an array of elements. The elements of the array are
* not destroyed. The method has linear time complexity.
*
* @param
* This is a highly optimized implementation which uses (a) the Wegener
* bottom-up heuristic and (b) sentinel values. The implementation uses an array
* in order to store the elements, providing amortized O(log(n)) time for the
* {@code insert} and {@code deleteMin} operations. Operation {@code findMin},
* is a worst-case O(1) operation. All bounds are worst-case if the user
* initializes the heap with a capacity larger or equal to the total number of
* elements that are going to be inserted into the heap.
*
*
* See the following papers for details about the optimizations:
*
* Note that this implementation is not synchronized. If
* multiple threads access a heap concurrently, and at least one of the threads
* modifies the heap structurally, it must be synchronized externally.
* (A structural modification is any operation that adds or deletes one or more
* elements or changing the key of some element.) This is typically accomplished
* by synchronizing on some object that naturally encapsulates the heap.
*
* @param
* The initial capacity of the heap is {@link #DEFAULT_HEAP_CAPACITY} and
* adjusts automatically based on the sequence of insertions and deletions.
*/
public BinaryArrayIntegerValueHeap() {
this(DEFAULT_HEAP_CAPACITY);
}
/**
* Constructs a new, empty heap, with a provided initial capacity using the
* natural ordering of its keys.
*
*
* The initial capacity of the heap is provided by the user and is adjusted
* automatically based on the sequence of insertions and deletions. The
* capacity will never become smaller than the initial requested capacity.
*
* @param capacity
* the initial heap capacity
*/
@SuppressWarnings("unchecked")
public BinaryArrayIntegerValueHeap(int capacity) {
checkCapacity(capacity);
this.minCapacity = Math.max(capacity, DEFAULT_HEAP_CAPACITY);
this.array = (Elem
* The implementation uses an array in order to store the elements and
* automatically maintains the size of the array much like a
* {@link java.util.Vector} does, providing amortized O(log(n)) time cost for
* the {@code insert} and {@code deleteMin} operations. Operation
* {@code findMin}, is a worst-case O(1) operation. The bounds are worst-case if
* the user initializes the heap with a capacity larger or equal to the total
* number of elements that are going to be inserted into the heap.
*
*
* Constructing such a heap from an array of elements can be performed using the
* method {@link #heapify(Object[])} or {@link #heapify(Object[], Comparator)}
* in linear time.
*
*
* Note that the ordering maintained by a binary heap, like any heap, and
* whether or not an explicit comparator is provided, must be consistent
* with {@code equals} if this heap is to correctly implement the
* {@code Heap} interface. (See {@code Comparable} or {@code Comparator} for a
* precise definition of consistent with equals.) This is so because
* the {@code Heap} interface is defined in terms of the {@code equals}
* operation, but a binary heap performs all key comparisons using its
* {@code compareTo} (or {@code compare}) method, so two keys that are deemed
* equal by this method are, from the standpoint of the binary heap, equal. The
* behavior of a heap is well-defined even if its ordering is
* inconsistent with {@code equals}; it just fails to obey the general contract
* of the {@code Heap} interface.
*
*
* Note that this implementation is not synchronized. If
* multiple threads access a heap concurrently, and at least one of the threads
* modifies the heap structurally, it must be synchronized externally.
* (A structural modification is any operation that adds or deletes one or more
* elements or changing the key of some element.) This is typically accomplished
* by synchronizing on some object that naturally encapsulates the heap.
*
* @param
* All keys inserted into the heap must implement the {@link Comparable}
* interface. Furthermore, all such keys must be mutually
* comparable: {@code k1.compareTo(k2)} must not throw a
* {@code ClassCastException} for any keys {@code k1} and {@code k2} in the
* heap. If the user attempts to put a key into the heap that violates this
* constraint (for example, the user attempts to put a string key into a
* heap whose keys are integers), the {@code insert(Object key)} call will
* throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is
* {@link BinaryArrayWeakHeap#DEFAULT_HEAP_CAPACITY} and adjusts
* automatically based on the sequence of insertions and deletions.
*/
public BinaryArrayWeakHeap() {
super(null, DEFAULT_HEAP_CAPACITY);
}
/**
* Constructs a new, empty heap, with a provided initial capacity using the
* natural ordering of its keys.
*
*
* All keys inserted into the heap must implement the {@link Comparable}
* interface. Furthermore, all such keys must be mutually
* comparable: {@code k1.compareTo(k2)} must not throw a
* {@code ClassCastException} for any keys {@code k1} and {@code k2} in the
* heap. If the user attempts to put a key into the heap that violates this
* constraint (for example, the user attempts to put a string key into a
* heap whose keys are integers), the {@code insert(Object key)} call will
* throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is provided by the user and is adjusted
* automatically based on the sequence of insertions and deletions. The
* capacity will never become smaller than the initial requested capacity.
*
* @param capacity
* the initial heap capacity
*/
public BinaryArrayWeakHeap(int capacity) {
super(null, capacity);
}
/**
* Constructs a new, empty heap, ordered according to the given comparator.
*
*
* All keys inserted into the heap must be mutually comparable by
* the given comparator: {@code comparator.compare(k1,
* k2)} must not throw a {@code ClassCastException} for any keys {@code k1}
* and {@code k2} in the heap. If the user attempts to put a key into the
* heap that violates this constraint, the {@code insert(Object key)} call
* will throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is
* {@link BinaryArrayWeakHeap#DEFAULT_HEAP_CAPACITY} and adjusts
* automatically based on the sequence of insertions and deletions.
*
* @param comparator
* the comparator that will be used to order this heap. If
* {@code null}, the {@linkplain Comparable natural ordering} of
* the keys will be used.
*/
public BinaryArrayWeakHeap(Comparator super K> comparator) {
super(comparator, DEFAULT_HEAP_CAPACITY);
}
/**
* Constructs a new, empty heap, with a provided initial capacity ordered
* according to the given comparator.
*
*
* All keys inserted into the heap must be mutually comparable by
* the given comparator: {@code comparator.compare(k1,
* k2)} must not throw a {@code ClassCastException} for any keys {@code k1}
* and {@code k2} in the heap. If the user attempts to put a key into the
* heap that violates this constraint, the {@code insert(Object key)} call
* will throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is provided by the user and is adjusted
* automatically based on the sequence of insertions and deletions. The
* capacity will never become smaller than the initial requested capacity.
*
* @param comparator
* the comparator that will be used to order this heap. If
* {@code null}, the {@linkplain Comparable natural ordering} of
* the keys will be used.
* @param capacity
* the initial heap capacity
*/
public BinaryArrayWeakHeap(Comparator super K> comparator, int capacity) {
super(comparator, capacity);
}
/**
* Create a heap from an array of elements. The elements of the array are
* not destroyed. The method has linear time complexity.
*
* @param
* The implementation uses an array in order to store the elements. and
* automatically maintains the size of the array much like a
* {@link java.util.Vector} does, providing amortized O(log_d(n)) time cost for
* the {@code insert} and amortized O(d log_d(n)) for the {@code deleteMin}
* operation. Operation {@code findMin}, is a worst-case O(1) operation.
* Operations {@code delete} and {@code decreaseKey} take worst-case O(log(n))
* time. The bounds are worst-case if the user initializes the heap with a
* capacity larger or equal to the total number of elements that are going to be
* inserted into the heap.
*
*
* Constructing such a heap from an array of elements can be performed using the
* method {@link #heapify(int, Object[], Object[])} or
* {@link #heapify(int, Object[], Object[], Comparator)} in linear time.
*
*
* Note that the ordering maintained by a d-ary heap, like any heap, and whether
* or not an explicit comparator is provided, must be consistent with
* {@code equals} if this heap is to correctly implement the {@code Heap}
* interface. (See {@code Comparable} or {@code Comparator} for a precise
* definition of consistent with equals.) This is so because the
* {@code Heap} interface is defined in terms of the {@code equals} operation,
* but a binary heap performs all key comparisons using its {@code compareTo}
* (or {@code compare}) method, so two keys that are deemed equal by this method
* are, from the standpoint of the d-ary heap, equal. The behavior of a heap
* is well-defined even if its ordering is inconsistent with
* {@code equals}; it just fails to obey the general contract of the
* {@code Heap} interface.
*
*
* Note that this implementation is not synchronized. If
* multiple threads access a heap concurrently, and at least one of the threads
* modifies the heap structurally, it must be synchronized externally.
* (A structural modification is any operation that adds or deletes one or more
* elements or changing the key of some element.) This is typically accomplished
* by synchronizing on some object that naturally encapsulates the heap.
*
* @param
* All keys inserted into the heap must implement the {@link Comparable}
* interface. Furthermore, all such keys must be mutually
* comparable: {@code k1.compareTo(k2)} must not throw a
* {@code ClassCastException} for any keys {@code k1} and {@code k2} in the
* heap. If the user attempts to put a key into the heap that violates this
* constraint (for example, the user attempts to put a string key into a
* heap whose keys are integers), the {@code insert(Object key)} call will
* throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is {@link #DEFAULT_HEAP_CAPACITY} and
* adjusts automatically based on the sequence of insertions and deletions.
*
* @param d
* the number of children of each node in the d-ary heap
* @throws IllegalArgumentException
* in case the number of children per node are less than 2
*/
public DaryArrayAddressableHeap(int d) {
this(d, null, DEFAULT_HEAP_CAPACITY);
}
/**
* Constructs a new, empty heap, with a provided initial capacity using the
* natural ordering of its keys.
*
*
* All keys inserted into the heap must implement the {@link Comparable}
* interface. Furthermore, all such keys must be mutually
* comparable: {@code k1.compareTo(k2)} must not throw a
* {@code ClassCastException} for any keys {@code k1} and {@code k2} in the
* heap. If the user attempts to put a key into the heap that violates this
* constraint (for example, the user attempts to put a string key into a
* heap whose keys are integers), the {@code insert(Object key)} call will
* throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is provided by the user and is adjusted
* automatically based on the sequence of insertions and deletions. The
* capacity will never become smaller than the initial requested capacity.
*
* @param d
* the number of children of each node in the d-ary heap
* @param capacity
* the initial heap capacity
* @throws IllegalArgumentException
* in case the number of children per node are less than 2
*/
public DaryArrayAddressableHeap(int d, int capacity) {
this(d, null, capacity);
}
/**
* Constructs a new, empty heap, ordered according to the given comparator.
*
*
* All keys inserted into the heap must be mutually comparable by
* the given comparator: {@code comparator.compare(k1,
* k2)} must not throw a {@code ClassCastException} for any keys {@code k1}
* and {@code k2} in the heap. If the user attempts to put a key into the
* heap that violates this constraint, the {@code insert(Object key)} call
* will throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is {@link #DEFAULT_HEAP_CAPACITY} and
* adjusts automatically based on the sequence of insertions and deletions.
*
* @param d
* the number of children of each node in the d-ary heap
* @param comparator
* the comparator that will be used to order this heap. If
* {@code null}, the {@linkplain Comparable natural ordering} of
* the keys will be used.
* @throws IllegalArgumentException
* in case the number of children per node are less than 2 *
*/
public DaryArrayAddressableHeap(int d, Comparator super K> comparator) {
this(d, comparator, DEFAULT_HEAP_CAPACITY);
}
/**
* Constructs a new, empty heap, with a provided initial capacity ordered
* according to the given comparator.
*
*
* All keys inserted into the heap must be mutually comparable by
* the given comparator: {@code comparator.compare(k1,
* k2)} must not throw a {@code ClassCastException} for any keys {@code k1}
* and {@code k2} in the heap. If the user attempts to put a key into the
* heap that violates this constraint, the {@code insert(Object key)} call
* will throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is provided by the user and is adjusted
* automatically based on the sequence of insertions and deletions. The
* capacity will never become smaller than the initial requested capacity.
*
* @param d
* the number of children of each node in the d-ary heap
* @param comparator
* the comparator that will be used to order this heap. If
* {@code null}, the {@linkplain Comparable natural ordering} of
* the keys will be used.
* @param capacity
* the initial heap capacity
* @throws IllegalArgumentException
* in case the number of children per node are less than 2
*/
public DaryArrayAddressableHeap(int d, Comparator super K> comparator, int capacity) {
super(comparator, capacity);
if (d < 2) {
throw new IllegalArgumentException(D_ARY_HEAPS_MUST_HAVE_AT_LEAST_2_CHILDREN_PER_NODE);
}
this.d = d;
}
/**
* Create a heap from an array of elements. The elements of the array are
* not destroyed. The method has linear time complexity.
*
* @param
* The implementation uses an array in order to store the elements and
* automatically maintains the size of the array much like a
* {@link java.util.Vector} does, providing amortized O(log_d(n)) time cost for
* the {@code insert} and amortized O(d log_d(n)) for the {@code deleteMin}
* operation. Operation {@code findMin}, is a worst-case O(1) operation. The
* bounds are worst-case if the user initializes the heap with a capacity larger
* or equal to the total number of elements that are going to be inserted into
* the heap.
*
*
* Constructing such a heap from an array of elements can be performed using the
* method {@link #heapify(int, Object[])} or
* {@link #heapify(int, Object[], Comparator)} in linear time.
*
*
* Note that the ordering maintained by a d-ary heap, like any heap, and whether
* or not an explicit comparator is provided, must be consistent with
* {@code equals} if this heap is to correctly implement the {@code Heap}
* interface. (See {@code Comparable} or {@code Comparator} for a precise
* definition of consistent with equals.) This is so because the
* {@code Heap} interface is defined in terms of the {@code equals} operation,
* but a d-ary heap performs all key comparisons using its {@code compareTo} (or
* {@code compare}) method, so two keys that are deemed equal by this method
* are, from the standpoint of the d-ary heap, equal. The behavior of a heap
* is well-defined even if its ordering is inconsistent with
* {@code equals}; it just fails to obey the general contract of the
* {@code Heap} interface.
*
*
* Note that this implementation is not synchronized. If
* multiple threads access a heap concurrently, and at least one of the threads
* modifies the heap structurally, it must be synchronized externally.
* (A structural modification is any operation that adds or deletes one or more
* elements or changing the key of some element.) This is typically accomplished
* by synchronizing on some object that naturally encapsulates the heap.
*
* @param
* All keys inserted into the heap must implement the {@link Comparable}
* interface. Furthermore, all such keys must be mutually
* comparable: {@code k1.compareTo(k2)} must not throw a
* {@code ClassCastException} for any keys {@code k1} and {@code k2} in the
* heap. If the user attempts to put a key into the heap that violates this
* constraint (for example, the user attempts to put a string key into a
* heap whose keys are integers), the {@code insert(Object key)} call will
* throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is
* {@link DaryArrayHeap#DEFAULT_HEAP_CAPACITY} and adjusts automatically
* based on the sequence of insertions and deletions.
*
* @param d
* the number of children of each node in the d-ary heap
* @throws IllegalArgumentException
* in case the number of children per node are less than 2
*/
public DaryArrayHeap(int d) {
this(d, null, DEFAULT_HEAP_CAPACITY);
}
/**
* Constructs a new, empty heap, with a provided initial capacity using the
* natural ordering of its keys.
*
*
* All keys inserted into the heap must implement the {@link Comparable}
* interface. Furthermore, all such keys must be mutually
* comparable: {@code k1.compareTo(k2)} must not throw a
* {@code ClassCastException} for any keys {@code k1} and {@code k2} in the
* heap. If the user attempts to put a key into the heap that violates this
* constraint (for example, the user attempts to put a string key into a
* heap whose keys are integers), the {@code insert(Object key)} call will
* throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is provided by the user and is adjusted
* automatically based on the sequence of insertions and deletions. The
* capacity will never become smaller than the initial requested capacity.
*
* @param d
* the number of children of each node in the d-ary heap
* @param capacity
* the initial heap capacity
* @throws IllegalArgumentException
* in case the number of children per node are less than 2
*/
public DaryArrayHeap(int d, int capacity) {
this(d, null, capacity);
}
/**
* Constructs a new, empty heap, ordered according to the given comparator.
*
*
* All keys inserted into the heap must be mutually comparable by
* the given comparator: {@code comparator.compare(k1,
* k2)} must not throw a {@code ClassCastException} for any keys {@code k1}
* and {@code k2} in the heap. If the user attempts to put a key into the
* heap that violates this constraint, the {@code insert(Object key)} call
* will throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is
* {@link DaryArrayHeap#DEFAULT_HEAP_CAPACITY} and adjusts automatically
* based on the sequence of insertions and deletions.
*
* @param d
* the number of children of each node in the d-ary heap
* @param comparator
* the comparator that will be used to order this heap. If
* {@code null}, the {@linkplain Comparable natural ordering} of
* the keys will be used.
* @throws IllegalArgumentException
* in case the number of children per node are less than 2
*/
public DaryArrayHeap(int d, Comparator super K> comparator) {
this(d, comparator, DEFAULT_HEAP_CAPACITY);
}
/**
* Constructs a new, empty heap, with a provided initial capacity ordered
* according to the given comparator.
*
*
* All keys inserted into the heap must be mutually comparable by
* the given comparator: {@code comparator.compare(k1,
* k2)} must not throw a {@code ClassCastException} for any keys {@code k1}
* and {@code k2} in the heap. If the user attempts to put a key into the
* heap that violates this constraint, the {@code insert(Object key)} call
* will throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is provided by the user and is adjusted
* automatically based on the sequence of insertions and deletions. The
* capacity will never become smaller than the initial requested capacity.
*
* @param d
* the number of children of each node in the d-ary heap
* @param comparator
* the comparator that will be used to order this heap. If
* {@code null}, the {@linkplain Comparable natural ordering} of
* the keys will be used.
* @param capacity
* the initial heap capacity
* @throws IllegalArgumentException
* in case the number of children per node are less than 2
*/
public DaryArrayHeap(int d, Comparator super K> comparator, int capacity) {
super(comparator, capacity);
if (d < 2) {
throw new IllegalArgumentException("D-ary heaps must have at least 2 children per node");
}
this.d = d;
}
/**
* Create a heap from an array of elements. The elements of the array are
* not destroyed. The method has linear time complexity.
*
* @param
* For details about the implementation see the following
* paper:
*
* The implementation uses an array in order to store the elements and
* automatically maintains the size of the array much like a
* {@link java.util.Vector} does, providing amortized O(log(n)) time cost for
* the {@code insert}, {@code deleteMin}, and {@code deleteMax} operations.
* Operations {@code findMin} and {@code findMax} are worst-case O(1). The
* bounds are worst-case if the user initializes the heap with a capacity larger
* or equal to the total number of elements that are going to be inserted into
* the heap.
*
*
* Constructing such a heap from an array of elements can be performed using the
* method {@link #heapify(Object[])} or {@link #heapify(Object[], Comparator)}
* in linear time.
*
*
* Note that the ordering maintained by this heap, like any heap, and whether or
* not an explicit comparator is provided, must be consistent with
* {@code equals} if this heap is to correctly implement the {@code Heap}
* interface. (See {@code Comparable} or {@code Comparator} for a precise
* definition of consistent with equals.) This is so because the
* {@code Heap} interface is defined in terms of the {@code equals} operation,
* but this heap performs all key comparisons using its {@code compareTo} (or
* {@code compare}) method, so two keys that are deemed equal by this method
* are, from the standpoint of this heap, equal. The behavior of a heap
* is well-defined even if its ordering is inconsistent with
* {@code equals}; it just fails to obey the general contract of the
* {@code Heap} interface.
*
*
* Note that this implementation is not synchronized. If
* multiple threads access a heap concurrently, and at least one of the threads
* modifies the heap structurally, it must be synchronized externally.
* (A structural modification is any operation that adds or deletes one or more
* elements or changing the key of some element.) This is typically accomplished
* by synchronizing on some object that naturally encapsulates the heap.
*
* @param
* All keys inserted into the heap must implement the {@link Comparable}
* interface. Furthermore, all such keys must be mutually
* comparable: {@code k1.compareTo(k2)} must not throw a
* {@code ClassCastException} for any keys {@code k1} and {@code k2} in the
* heap. If the user attempts to put a key into the heap that violates this
* constraint (for example, the user attempts to put a string key into a
* heap whose keys are integers), the {@code insert(Object key)} call will
* throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is {@link #DEFAULT_HEAP_CAPACITY} and
* adjusts automatically based on the sequence of insertions and deletions.
*/
public MinMaxBinaryArrayDoubleEndedHeap() {
super(null, DEFAULT_HEAP_CAPACITY);
}
/**
* Constructs a new, empty heap, with a provided initial capacity using the
* natural ordering of its keys.
*
*
* All keys inserted into the heap must implement the {@link Comparable}
* interface. Furthermore, all such keys must be mutually
* comparable: {@code k1.compareTo(k2)} must not throw a
* {@code ClassCastException} for any keys {@code k1} and {@code k2} in the
* heap. If the user attempts to put a key into the heap that violates this
* constraint (for example, the user attempts to put a string key into a
* heap whose keys are integers), the {@code insert(Object key)} call will
* throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is provided by the user and is adjusted
* automatically based on the sequence of insertions and deletions. The
* capacity will never become smaller than the initial requested capacity.
*
* @param capacity
* the initial heap capacity
*/
public MinMaxBinaryArrayDoubleEndedHeap(int capacity) {
super(null, capacity);
}
/**
* Constructs a new, empty heap, ordered according to the given comparator.
*
*
* All keys inserted into the heap must be mutually comparable by
* the given comparator: {@code comparator.compare(k1,
* k2)} must not throw a {@code ClassCastException} for any keys {@code k1}
* and {@code k2} in the heap. If the user attempts to put a key into the
* heap that violates this constraint, the {@code insert(Object key)} call
* will throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is {@link #DEFAULT_HEAP_CAPACITY} and
* adjusts automatically based on the sequence of insertions and deletions.
*
* @param comparator
* the comparator that will be used to order this heap. If
* {@code null}, the {@linkplain Comparable natural ordering} of
* the keys will be used.
*/
public MinMaxBinaryArrayDoubleEndedHeap(Comparator super K> comparator) {
super(comparator, DEFAULT_HEAP_CAPACITY);
}
/**
* Constructs a new, empty heap, with a provided initial capacity ordered
* according to the given comparator.
*
*
* All keys inserted into the heap must be mutually comparable by
* the given comparator: {@code comparator.compare(k1,
* k2)} must not throw a {@code ClassCastException} for any keys {@code k1}
* and {@code k2} in the heap. If the user attempts to put a key into the
* heap that violates this constraint, the {@code insert(Object key)} call
* will throw a {@code ClassCastException}.
*
*
* The initial capacity of the heap is provided by the user and is adjusted
* automatically based on the sequence of insertions and deletions.The
* capacity will never become smaller than the initial requested capacity.
*
* @param comparator
* the comparator that will be used to order this heap. If
* {@code null}, the {@linkplain Comparable natural ordering} of
* the keys will be used.
* @param capacity
* the initial heap capacity
*/
public MinMaxBinaryArrayDoubleEndedHeap(Comparator super K> comparator, int capacity) {
super(comparator, capacity);
}
/**
* Create a heap from an array of elements. The elements of the array are
* not destroyed. The method has linear time complexity.
*
* @param
* This is the hollow heap described in detail in the following
* paper:
*
* This implementation provides amortized O(1) time for operations that do not
* involve deleting an element such as {@code insert}, and {@code decreaseKey}.
* Operations {@code deleteMin} and {@code delete} are amortized O(log(n)). The
* operation {@code meld} is also amortized O(1).
*
*
* All the above bounds, however, assume that the user does not perform
* cascading melds on heaps such as:
*
*
* Note that the ordering maintained by this heap, like any heap, and whether or
* not an explicit comparator is provided, must be consistent with
* {@code equals} if this heap is to correctly implement the
* {@code AddressableHeap} interface. (See {@code Comparable} or
* {@code Comparator} for a precise definition of consistent with
* equals.) This is so because the {@code AddressableHeap} interface is
* defined in terms of the {@code equals} operation, but this heap performs all
* key comparisons using its {@code compareTo} (or {@code compare}) method, so
* two keys that are deemed equal by this method are, from the standpoint of
* this heap, equal. The behavior of a heap is well-defined even if its
* ordering is inconsistent with {@code equals}; it just fails to obey the
* general contract of the {@code AddressableHeap} interface.
*
*
* Note that this implementation is not synchronized. If
* multiple threads access a heap concurrently, and at least one of the threads
* modifies the heap structurally, it must be synchronized externally.
* (A structural modification is any operation that adds or deletes one or more
* elements or changing the key of some element.) This is typically accomplished
* by synchronizing on some object that naturally encapsulates the heap.
*
* @param
*
*
*
*
*
*
*
*
*
* d.meld(e);
* c.meld(d);
* b.meld(c);
* a.meld(b);
*
*
* The above scenario, although efficiently supported by using union-find with
* path compression, invalidates the claimed bounds.
*
*