Main Page Class Hierarchy Compound List File List Compound Members
HeapMax< T > Class Template Reference
#include <HeapMax.hh>
Inheritance diagram for HeapMax< T >:
List of all members.
Detailed Description
template<class T>
class HeapMax< T >
This template realises a heap which has the heap condition: The value of fathernode is bigger than each value of son's nodes. To realize it, I chose a field of the datatype T with the size m_numberOfArrays. This size you can influence by setting the parameter m_value.
The interface of this template:
There are two kinds of Constructors. Destructor. The methods sortIn(int) and heapify(int) to achive the heap condition by using the method exchange(T*,T*) which exchanges the values of two keys. The method insertKey(T) to enter a new member T to the heap. PrintHeap() is dedicated to get a print of the whole heap. To find out whether the heap is empty you can use emptyHeap(). By setValue(int) you change the parameter m_value. The method increasNumberOfNodes() increases m_numberOfNodes about one step; equivalent decreases decreasNumberOfNodes() m_numberOfNodes about one step. Last getNumberOfNodes() returns m_numberOfNodes.
The private part of this template:
- Parameters:
-
m_numberOfNodes |
Number of the keys still being involved into the heap. |
m_numberOfArrays |
Size of the current usable field m_heapAsArray. |
m_start |
Saves the number of the first node (always = 1). |
m_value |
Saves the number of the amount of resizing m_heapAsArray. |
The protected part of this template: - Parameters:
-
T |
*m_heapAsArray Is a pointer to allocated field m_heapAsArray from datatype T. |
Constructor & Destructor Documentation
template<class T> |
HeapMax< T >::HeapMax |
( |
T |
init |
) |
[inline] |
|
|
First there are no keys in the heap, therefore: Set m_numberOfNodes = 0. The number of the first node is always 1: set m_start = 1. The default value of field size is 10 : set m_value = 10. The size of the field is given by m_numberOfArrays. Try {...} tries whether there is enough memory to allocate a field of datatype T with the size m_numberOfArrays. If it's okay, the field will be allocated and values will be initialized by init. Otherwise there will be an exception from type bad_alloc and there is no construction. - Parameters:
-
init |
It's the input value to initialize the array. |
|
template<class T> |
HeapMax< T >::HeapMax |
( |
T |
init, |
|
|
int |
|
|
) |
[inline] |
|
|
The same features like HeapMax(T init) but further you can set the parameter m_value with your input _value.If _value is chosen smaller than 1,there would be no construction with the comment "Invalid input for m_value!" - Parameters:
-
_value |
It's the input to set the variable m_value. |
init |
It's the input value to initialize the array. |
|
template<class T> |
HeapMax< T >::~HeapMax |
( |
|
) |
[inline] |
|
|
The destructor destroies just the allocated field, so that the reserved memory is given free. |
Member Function Documentation
template<class T> |
void HeapMax< T >::decreaseNumberOfNodes |
( |
|
) |
[inline] |
|
|
This method decreases m_numberOfNodes about one step. |
template<class T> |
bool HeapMax< T >::emptyHeap |
( |
|
) |
[inline] |
|
|
Here you are able to check whether the heap is empty or not. - Returns :
-
bool You will get true if the heap is empty and false if the heap isn't empty.
|
template<class T> |
void HeapMax< T >::exchange |
( |
T * |
, |
|
|
T * |
|
|
) |
[inline] |
|
|
This method exchanges the value of two given nodes (call-by-reference). - Parameters:
-
T |
*node1 It's the first given node. |
T |
*node2 It's the second given node. |
|
template<class T> |
int HeapMax< T >::getNumberOfNodes |
( |
|
) |
[inline] |
|
|
This method returns the current m_numberOfNodes. - Returns :
-
int It's the current m_numberOfNodes.
|
template<class T> |
void HeapMax< T >::heapify |
( |
int |
|
) |
[inline] |
|
|
This method you can call the heart of the whole priority queue: It keeps or achives the heap condition again (e.g. after extracting the root). This works by comparing the values of father and his sons. If the heap condition is violated, it would be repaired by exchanging the values. After that there is a recurrent call of heapify(selectedSon) until the heap condition is reestablished down to leaves of the heap. - Parameters:
-
node |
It's the number of the node at which may the heap condition is violated. |
|
template<class T> |
void HeapMax< T >::increaseNumberOfNodes |
( |
|
) |
[inline] |
|
|
This method increases m_numberOfNodes about one step. |
template<class T> |
void HeapMax< T >::insertKey |
( |
T |
|
) |
[inline] |
|
|
This method adds a new node together with the value _value to the current heap. First it increases m_numberOfNodes with the amount of 1. After that there runs a check whether the field runs to small through this insertion. If this is the case there will be built up a new field with the new size: old size + m_value. If this allocation is not possible, the variable m_value will be halved and the insertion will be tried again and again until it will work or in bad case (if m_value<50) the program will aborted by exit with the comment "Not enough memory!". If the new allocation was succesfull the new key will be entered at m_heapAsArray[m_numberOfNodes] (at the last node of the heap). Last, the call of sortIn(m_numberOfNodes) will place it a the right position in the tree. - Parameters:
-
_value |
It's the new value that should be insert. |
|
template<class T> |
void HeapMax< T >::printHeap |
( |
|
) |
[inline] |
|
|
This method prints the values of the heap to the screen. |
template<class T> |
void HeapMax< T >::setValue |
( |
int |
|
) |
[inline] |
|
|
It's an additional function to reset the parameter m_value after construction.If _value is chosen smaller than 1, it wouldn't work with the comment "Invalid input for m_value!" - Parameters:
-
_value |
It's the input to re-set the variable m_value. |
|
template<class T> |
void HeapMax< T >::sortIn |
( |
int |
|
) |
[inline] |
|
|
This method sorts the given node to the right position in the heap. - Parameters:
-
node |
It's the number of the node that should be insert. |
|
The documentation for this class was generated from the following file:
Generated on Sun Mar 9 11:43:45 2003 by
1.2.17