Class BinaryArrayIntegerValueHeap<V>
- java.lang.Object
-
- org.jheaps.array.BinaryArrayIntegerValueHeap<V>
-
- Type Parameters:
V
- the type of values maintained by this heap
- All Implemented Interfaces:
java.io.Serializable
,Heap<java.lang.Integer>
,ValueHeap<java.lang.Integer,V>
public class BinaryArrayIntegerValueHeap<V> extends java.lang.Object implements ValueHeap<java.lang.Integer,V>, java.io.Serializable
An optimized array-based binary heap with integer keys.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
anddeleteMin
operations. OperationfindMin
, 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.
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
BinaryArrayIntegerValueHeap.Elem<V>
-
Field Summary
Fields Modifier and Type Field Description private BinaryArrayIntegerValueHeap.Elem<V>[]
array
The array used for representing the heap.static int
DEFAULT_HEAP_CAPACITY
Default initial capacity of the heap.private static int
INF_KEY
Infimumprivate static int
MAX_HEAP_CAPACITY
The maximum heap capacity.private static int
MIN_HEAP_CAPACITY
The minimum heap capacity.private int
minCapacity
Minimum capacity due to initially requested capacity.private static long
serialVersionUID
private int
size
Number of elements in the heap.private static int
SUP_KEY
Supremum
-
Constructor Summary
Constructors Constructor Description BinaryArrayIntegerValueHeap()
Constructs 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
checkCapacity(int capacity)
Check that a capacity is valid.void
clear()
Clear all the elements of this heap.java.util.Comparator<? super java.lang.Integer>
comparator()
Returns the comparator used to order the keys in this heap, ornull
if this heap uses the natural ordering of its keys.java.lang.Integer
deleteMin()
Delete and return an element with the minimum key.private void
ensureCapacity(int capacity)
Ensure that the array representation has the necessary capacity.java.lang.Integer
findMin()
Find an element with the minimum key.V
findMinValue()
Find the value of an element with the minimum key.void
insert(java.lang.Integer key)
Insert a key into the heap.void
insert(java.lang.Integer key, V value)
Insert an element into the heap.boolean
isEmpty()
Returnstrue
if this heap is empty.long
size()
Returns the number of elements in this heap.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
DEFAULT_HEAP_CAPACITY
public static final int DEFAULT_HEAP_CAPACITY
Default initial capacity of the heap.- See Also:
- Constant Field Values
-
SUP_KEY
private static final int SUP_KEY
Supremum- See Also:
- Constant Field Values
-
INF_KEY
private static final int INF_KEY
Infimum- See Also:
- Constant Field Values
-
MAX_HEAP_CAPACITY
private static final int MAX_HEAP_CAPACITY
The maximum heap capacity.- See Also:
- Constant Field Values
-
MIN_HEAP_CAPACITY
private static final int MIN_HEAP_CAPACITY
The minimum heap capacity.- See Also:
- Constant Field Values
-
array
private BinaryArrayIntegerValueHeap.Elem<V>[] array
The array used for representing the heap.
-
size
private int size
Number of elements in the heap.
-
minCapacity
private int minCapacity
Minimum capacity due to initially requested capacity.
-
-
Constructor Detail
-
BinaryArrayIntegerValueHeap
public BinaryArrayIntegerValueHeap()
Constructs a new, empty heap, using the natural ordering of its keys.The initial capacity of the heap is
DEFAULT_HEAP_CAPACITY
and 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 Detail
-
isEmpty
public boolean isEmpty()
Returnstrue
if 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
public java.util.Comparator<? super java.lang.Integer> comparator()
Returns the comparator used to order the keys in this heap, ornull
if this heap uses the natural ordering of its keys.- Specified by:
comparator
in interfaceHeap<V>
- Returns:
- the comparator used to order the keys in this heap, or
null
if this heap uses the natural ordering of its keys
-
findMin
public java.lang.Integer findMin()
Find an element with the minimum key.
-
findMinValue
public V findMinValue()
Find the value of an element with the minimum key.- Specified by:
findMinValue
in interfaceValueHeap<java.lang.Integer,V>
- Returns:
- the value of an element with the minimum key
-
insert
public void insert(java.lang.Integer key, V value)
Insert an element into the heap.
-
insert
public void insert(java.lang.Integer key)
Insert a key into the heap.
-
deleteMin
public java.lang.Integer deleteMin()
Delete and return an element with the minimum key. If multiple such elements exists, only one of them will be deleted.
-
ensureCapacity
private void ensureCapacity(int capacity)
Ensure that the array representation has the necessary capacity.- Parameters:
capacity
- the requested capacity
-
checkCapacity
private void checkCapacity(int capacity)
Check that a capacity is valid.- Parameters:
capacity
- the capacity- Throws:
java.lang.IllegalArgumentException
- if the capacity is negative or more than the maximum array size
-
-