Class BinaryArrayIntegerValueHeap<V>
- Type Parameters:
V- the type of values maintained by this heap
- All Implemented Interfaces:
Serializable,Heap<Integer>,ValueHeap<Integer,V>
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
insert and deleteMin operations. Operation 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:
- Ingo Wegener. BOTTOM-UP-HEAPSORT, a new variant of HEAPSORT beating, on an average, QUICKSORT (if n is not very small). Theoretical Computer Science, 118(1), 81--98, 1993.
- Peter Sanders. Fast Priority Queues for Cached Memory. Algorithms Engineering and Experiments (ALENEX), 312--327, 1999.
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.
- Author:
- Dimitrios Michail
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault initial capacity of the heap. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new, empty heap, using the natural ordering of its keys.BinaryArrayIntegerValueHeap(int capacity) Constructs a new, empty heap, with a provided initial capacity using the natural ordering of its keys. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clear all the elements of this heap.Comparator<? super Integer> Returns the comparator used to order the keys in this heap, ornullif this heap uses the natural ordering of its keys.Delete and return an element with the minimum key.findMin()Find an element with the minimum key.Find the value of an element with the minimum key.voidInsert a key into the heap.voidInsert an element into the heap.booleanisEmpty()Returnstrueif this heap is empty.longsize()Returns the number of elements in this heap.
-
Field Details
-
DEFAULT_HEAP_CAPACITY
public static final int DEFAULT_HEAP_CAPACITYDefault initial capacity of the heap.- See Also:
-
-
Constructor Details
-
BinaryArrayIntegerValueHeap
public BinaryArrayIntegerValueHeap()Constructs a new, empty heap, using the natural ordering of its keys.The initial capacity of the heap is
DEFAULT_HEAP_CAPACITYand adjusts automatically based on the sequence of insertions and deletions. -
BinaryArrayIntegerValueHeap
public BinaryArrayIntegerValueHeap(int 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.
- Parameters:
capacity- the initial heap capacity
-
-
Method Details
-
isEmpty
public boolean isEmpty()Returnstrueif this heap is empty. -
size
public long size()Returns the number of elements in this heap. -
clear
public void clear()Clear all the elements of this heap. -
comparator
Returns the comparator used to order the keys in this heap, ornullif this heap uses the natural ordering of its keys.- Specified by:
comparatorin interfaceHeap<V>- Returns:
- the comparator used to order the keys in this heap, or
nullif this heap uses the natural ordering of its keys
-
findMin
Find an element with the minimum key. -
findMinValue
Find the value of an element with the minimum key.- Specified by:
findMinValuein interfaceValueHeap<Integer,V> - Returns:
- the value of an element with the minimum key
-
insert
Insert an element into the heap. -
insert
Insert a key into the heap. -
deleteMin
Delete and return an element with the minimum key. If multiple such elements exists, only one of them will be deleted.
-