Min-heap priority queue realized by a data array. More...
#include <ogdf/basic/BinaryHeap2.h>
Inheritance diagram for ogdf::BinaryHeap2< key, HeapObject >:Classes | |
| struct | HeapEntry |
Public Member Functions | |
| BinaryHeap2 (int startSize=128) | |
| Creates a binary heap. | |
| virtual | ~BinaryHeap2 () |
| int | capacity () const |
| Returns the current size. | |
| void | clear () |
| Reinitializes the data structure. | |
| virtual void | decreaseKey (int index, key priority) |
| Decreases priority of an object that is addressed by index. | |
| int | empty () const |
| Returns true iff the heap is empty. | |
| HeapObject | extractMin () |
| Returns minimum priority element and removes it from the heap. | |
| key | getPriority (int index) const |
| void | insert (HeapObject &obj, key &p, int *keyUpdate=0) |
| Inserts a new element obj with priority p and pointer for index update. | |
| virtual void | makeHeap () |
| Obtains heap property, only needed if the elements are not inserted by insert method. | |
| HeapObject | minRet () const |
| Returns minimum priority element. | |
| const BinaryHeap2 & | operator= (const BinaryHeap2< key, HeapObject > &rhs) |
| Assignment operator. | |
| int | size () const |
| Returns the number of stored elements. | |
Public Member Functions inherited from ogdf::HeapBase< key, HeapObject > | |
| HeapBase () | |
| Constructor. | |
| virtual | ~HeapBase () |
| virtual void | decreaseKey () |
| update the data structure by decreasing the key of an object | |
| virtual void | insert (HeapObject, key) |
| insert a new element with priority key | |
| HeapObject | minRet () |
Protected Member Functions | |
| int | arrayBound (int arraySize) |
| bool | hasLeft (int num) |
| Returns true if left child exists. | |
| bool | hasRight (int num) |
| Returns true if right child exists. | |
| int | higherArrayBound (int arraySize) |
| int | higherArraySize (int arraySize) |
| void | init (int initSize) |
| int | leftChildIndex (int num) |
| Array index of left child. | |
| int | lowerArrayBound (int arraySize) |
| int | lowerArraySize (int arraySize) |
| int | parentIndex (int num) |
| Array index of parent node. | |
| int | rightChildIndex (int num) |
| Array index of right child. | |
| void | siftDown (int pos) |
| Establishes heap property by moving element down in heap if necessary. | |
| void | siftUp (int pos) |
| Establishes heap property by moving element up in heap if necessary. | |
Private Attributes | |
| int | m_arraySize |
| HeapEntry * | m_heapArray |
| int | m_startSize |
Additional Inherited Members | |
Protected Attributes inherited from ogdf::HeapBase< key, HeapObject > | |
| int | m_size |
Min-heap priority queue realized by a data array.
Heaps store objects that are weighted with costs; the minimal cost object is accessible over member function minRet() and extracted by extractMin().
The class uses two template parameters:
HeapObjects can be all types with copy constructor, as copies of inserted elements are created; key should be of a type with compare operators. We only allow integer as index/size type; the array index starts with 1.
To allow direct access to the underlying array structure in order to minimize decreaseKey() runningtime, a pointer to an integer storage can be provided as input() parameter that will be kept updated with the index position during heap operations.
The worst case running times of the methods is given by the following table, where n is the current number of elements.
| method | worst-case | amortized |
|---|---|---|
| extractMin() | O(n) | O(lg(n)) |
| siftDown() | O(lg(n)) | |
| siftUp() | O(lg(n)) | |
| minRet() | O(1) | |
| insert() | O(n) | O(lg(n)) |
Definition at line 105 of file BinaryHeap2.h.
| ogdf::BinaryHeap2< key, HeapObject >::BinaryHeap2 | ( | int | startSize = 128 | ) |
Creates a binary heap.
Definition at line 293 of file BinaryHeap2.h.
|
inlinevirtual |
Definition at line 115 of file BinaryHeap2.h.
|
inlineprotected |
Definition at line 220 of file BinaryHeap2.h.
|
inline |
Returns the current size.
Definition at line 158 of file BinaryHeap2.h.
| void ogdf::BinaryHeap2< key, HeapObject >::clear | ( | ) |
Reinitializes the data structure.
Deletes the array and reallocates it with size that was passed at construction time.
Definition at line 314 of file BinaryHeap2.h.
|
virtual |
Decreases priority of an object that is addressed by index.
Definition at line 418 of file BinaryHeap2.h.
|
inline |
Returns true iff the heap is empty.
Reimplemented from ogdf::HeapBase< key, HeapObject >.
Definition at line 164 of file BinaryHeap2.h.
| HeapObject ogdf::BinaryHeap2< key, HeapObject >::extractMin | ( | ) |
Returns minimum priority element and removes it from the heap.
Definition at line 433 of file BinaryHeap2.h.
|
inline |
Definition at line 151 of file BinaryHeap2.h.
|
inlineprotected |
Returns true if left child exists.
Definition at line 205 of file BinaryHeap2.h.
|
inlineprotected |
Returns true if right child exists.
Definition at line 212 of file BinaryHeap2.h.
|
inlineprotected |
Definition at line 221 of file BinaryHeap2.h.
|
inlineprotected |
Definition at line 222 of file BinaryHeap2.h.
|
protected |
Definition at line 301 of file BinaryHeap2.h.
| void ogdf::BinaryHeap2< key, HeapObject >::insert | ( | HeapObject & | obj, |
| key & | p, | ||
| int * | keyUpdate = 0 |
||
| ) |
Inserts a new element obj with priority p and pointer for index update.
Definition at line 469 of file BinaryHeap2.h.
|
inlineprotected |
Array index of left child.
Definition at line 191 of file BinaryHeap2.h.
|
inlineprotected |
Definition at line 223 of file BinaryHeap2.h.
|
inlineprotected |
Definition at line 224 of file BinaryHeap2.h.
|
virtual |
Obtains heap property, only needed if the elements are not inserted by insert method.
Implements ogdf::HeapBase< key, HeapObject >.
Definition at line 408 of file BinaryHeap2.h.
|
inline |
Returns minimum priority element.
Definition at line 149 of file BinaryHeap2.h.
| const BinaryHeap2< key, HeapObject > & ogdf::BinaryHeap2< key, HeapObject >::operator= | ( | const BinaryHeap2< key, HeapObject > & | rhs | ) |
Assignment operator.
Definition at line 495 of file BinaryHeap2.h.
|
inlineprotected |
Array index of parent node.
Definition at line 184 of file BinaryHeap2.h.
|
inlineprotected |
Array index of right child.
Definition at line 198 of file BinaryHeap2.h.
|
protected |
Establishes heap property by moving element down in heap if necessary.
Definition at line 360 of file BinaryHeap2.h.
|
protected |
Establishes heap property by moving element up in heap if necessary.
Definition at line 327 of file BinaryHeap2.h.
|
inline |
Returns the number of stored elements.
Reimplemented from ogdf::HeapBase< key, HeapObject >.
Definition at line 161 of file BinaryHeap2.h.
|
private |
Definition at line 277 of file BinaryHeap2.h.
|
private |
Definition at line 272 of file BinaryHeap2.h.
|
private |
Definition at line 279 of file BinaryHeap2.h.