Open
Graph Drawing
Framework

 v.2007.11
 

ogdf::BinaryHeap< key, HeapObject > Class Template Reference

Min-heap priority queue realized by a data array. More...

#include <BinaryHeap.h>

Inheritance diagram for ogdf::BinaryHeap< key, HeapObject >:

ogdf::HeapBase< key, HeapObject >

List of all members.

Public Member Functions

 BinaryHeap (int startSize=128)
 Creates a binary heap.
virtual ~BinaryHeap ()
const BinaryHeapoperator= (const BinaryHeap< key, HeapObject > &rhs)
 Assignment operator.
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 extractMin ()
 Returns minimum priority element and removes it from the heap.
virtual void decreaseKey (int index, key priority)
 Decreases priority of an object that is addressed by index.
HeapObject minRet () const
 Returns minimum priority element.
key getPriority (int index) const
int capacity () const
 Returns the current size.
int size () const
 Returns the number of stored elements.
int empty () const
 Returns true iff the heap is empty.
void clear ()
 Reinitializes the data structure.

Protected Member Functions

void siftUp (int pos)
 Establishes heap property by moving element up in heap if necessary.
void siftDown (int pos)
 Establishes heap property by moving element down in heap if necessary.
int parentIndex (int num)
 Array index of parent node.
int leftChildIndex (int num)
 Array index of left child.
int rightChildIndex (int num)
 Array index of right child.
bool hasLeft (int num)
 Returns true if left child exists.
bool hasRight (int num)
 Returns true if right child exists.
int arrayBound (int arraySize)
int higherArrayBound (int arraySize)
int higherArraySize (int arraySize)
int lowerArrayBound (int arraySize)
int lowerArraySize (int arraySize)
void init (int initSize)

Private Attributes

HeapEntrym_heapArray
int m_arraySize
int m_startSize

Classes

struct  HeapEntry


Detailed Description

template<class key, class HeapObject>
class ogdf::BinaryHeap< key, HeapObject >

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.

Running Time

The worst case running times of the methods is given by the following table, where n is the current number of elements.

methodworst-caseamortized
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 112 of file BinaryHeap.h.


Constructor & Destructor Documentation

template<class key, class HeapObject>
ogdf::BinaryHeap< key, HeapObject >::BinaryHeap ( int  startSize = 128  )  [inline]

Creates a binary heap.

Definition at line 305 of file BinaryHeap.h.

template<class key, class HeapObject>
virtual ogdf::BinaryHeap< key, HeapObject >::~BinaryHeap (  )  [inline, virtual]

Definition at line 122 of file BinaryHeap.h.


Member Function Documentation

template<class key, class HeapObject>
const BinaryHeap< key, HeapObject > & ogdf::BinaryHeap< key, HeapObject >::operator= ( const BinaryHeap< key, HeapObject > &  rhs  )  [inline]

Assignment operator.

Definition at line 503 of file BinaryHeap.h.

template<class key, class HeapObject>
void ogdf::BinaryHeap< key, HeapObject >::insert ( HeapObject &  obj,
key &  p,
int *  keyUpdate = 0 
) [inline]

Inserts a new element obj with priority p and pointer for index update.

Definition at line 477 of file BinaryHeap.h.

template<class key, class HeapObject>
void ogdf::BinaryHeap< key, HeapObject >::makeHeap (  )  [inline, virtual]

Obtains heap property, only needed if the elements are not inserted by insert method.

Implements ogdf::HeapBase< key, HeapObject >.

Definition at line 418 of file BinaryHeap.h.

template<class key, class HeapObject>
HeapObject ogdf::BinaryHeap< key, HeapObject >::extractMin (  )  [inline]

Returns minimum priority element and removes it from the heap.

Definition at line 441 of file BinaryHeap.h.

template<class key, class HeapObject>
void ogdf::BinaryHeap< key, HeapObject >::decreaseKey ( int  index,
key  priority 
) [inline, virtual]

Decreases priority of an object that is addressed by index.

Definition at line 427 of file BinaryHeap.h.

template<class key, class HeapObject>
HeapObject ogdf::BinaryHeap< key, HeapObject >::minRet (  )  const [inline]

Returns minimum priority element.

Definition at line 156 of file BinaryHeap.h.

template<class key, class HeapObject>
key ogdf::BinaryHeap< key, HeapObject >::getPriority ( int  index  )  const [inline]

Definition at line 158 of file BinaryHeap.h.

template<class key, class HeapObject>
int ogdf::BinaryHeap< key, HeapObject >::capacity (  )  const [inline]

Returns the current size.

Definition at line 165 of file BinaryHeap.h.

template<class key, class HeapObject>
int ogdf::BinaryHeap< key, HeapObject >::size (  )  const [inline]

Returns the number of stored elements.

Reimplemented from ogdf::HeapBase< key, HeapObject >.

Definition at line 168 of file BinaryHeap.h.

template<class key, class HeapObject>
int ogdf::BinaryHeap< key, HeapObject >::empty (  )  const [inline]

Returns true iff the heap is empty.

Reimplemented from ogdf::HeapBase< key, HeapObject >.

Definition at line 171 of file BinaryHeap.h.

template<class key, class HeapObject>
void ogdf::BinaryHeap< key, HeapObject >::clear (  )  [inline]

Reinitializes the data structure.

Deletes the array and reallocates it with size that was passed at construction time.

Definition at line 324 of file BinaryHeap.h.

template<class key, class HeapObject>
void ogdf::BinaryHeap< key, HeapObject >::siftUp ( int  pos  )  [inline, protected]

Establishes heap property by moving element up in heap if necessary.

Definition at line 336 of file BinaryHeap.h.

template<class key, class HeapObject>
void ogdf::BinaryHeap< key, HeapObject >::siftDown ( int  pos  )  [inline, protected]

Establishes heap property by moving element down in heap if necessary.

Definition at line 368 of file BinaryHeap.h.

template<class key, class HeapObject>
int ogdf::BinaryHeap< key, HeapObject >::parentIndex ( int  num  )  [inline, protected]

Array index of parent node.

Definition at line 191 of file BinaryHeap.h.

template<class key, class HeapObject>
int ogdf::BinaryHeap< key, HeapObject >::leftChildIndex ( int  num  )  [inline, protected]

Array index of left child.

Definition at line 198 of file BinaryHeap.h.

template<class key, class HeapObject>
int ogdf::BinaryHeap< key, HeapObject >::rightChildIndex ( int  num  )  [inline, protected]

Array index of right child.

Definition at line 205 of file BinaryHeap.h.

template<class key, class HeapObject>
bool ogdf::BinaryHeap< key, HeapObject >::hasLeft ( int  num  )  [inline, protected]

Returns true if left child exists.

Definition at line 212 of file BinaryHeap.h.

template<class key, class HeapObject>
bool ogdf::BinaryHeap< key, HeapObject >::hasRight ( int  num  )  [inline, protected]

Returns true if right child exists.

Definition at line 219 of file BinaryHeap.h.

template<class key, class HeapObject>
int ogdf::BinaryHeap< key, HeapObject >::arrayBound ( int  arraySize  )  [inline, protected]

Definition at line 227 of file BinaryHeap.h.

template<class key, class HeapObject>
int ogdf::BinaryHeap< key, HeapObject >::higherArrayBound ( int  arraySize  )  [inline, protected]

Definition at line 228 of file BinaryHeap.h.

template<class key, class HeapObject>
int ogdf::BinaryHeap< key, HeapObject >::higherArraySize ( int  arraySize  )  [inline, protected]

Definition at line 229 of file BinaryHeap.h.

template<class key, class HeapObject>
int ogdf::BinaryHeap< key, HeapObject >::lowerArrayBound ( int  arraySize  )  [inline, protected]

Definition at line 230 of file BinaryHeap.h.

template<class key, class HeapObject>
int ogdf::BinaryHeap< key, HeapObject >::lowerArraySize ( int  arraySize  )  [inline, protected]

Definition at line 231 of file BinaryHeap.h.

template<class key, class HeapObject>
void ogdf::BinaryHeap< key, HeapObject >::init ( int  initSize  )  [inline, protected]

Definition at line 312 of file BinaryHeap.h.


Member Data Documentation

template<class key, class HeapObject>
HeapEntry* ogdf::BinaryHeap< key, HeapObject >::m_heapArray [private]

Definition at line 279 of file BinaryHeap.h.

template<class key, class HeapObject>
int ogdf::BinaryHeap< key, HeapObject >::m_arraySize [private]

Definition at line 284 of file BinaryHeap.h.

template<class key, class HeapObject>
int ogdf::BinaryHeap< key, HeapObject >::m_startSize [private]

Definition at line 286 of file BinaryHeap.h.


The documentation for this class was generated from the following file:

© 1999-2007 by oreas GmbH, © 2005-2007 by University Dortmund and University Cologne.

Generated on Thu Nov 22 19:40:07 2007 by doxygen 1.5.4.