Main Page   Class Hierarchy   Compound List   File List   Compound Members  

HeapMax.hh

00001 
00002 //
00003 //  Module           : HeapMax.hh
00004 //  Description      :
00005 //  Author           : Christoph Laux
00006 //  Email            : claux@ix.urz.uni-heidelberg.de
00007 //  Copyright        : University of Heidelberg
00008 //  Created on       : Thu Febr 27 09:21:54 2003
00009 //  Last modified by : laux
00010 //  Last modified on : Thu Febr 27 09:21:54 2003
00011 //  Update count     : 0
00012 //
00014 //
00015 //  Date        Name            Changes/Extensions
00016 //  ----        ----            ------------------
00017 //
00019 
00020 
00021 #ifndef HeapMax_HH
00022 #define HeapMax_HH
00023 
00024 
00025 
00056 template <class T>
00057 class HeapMax {
00058 
00059 private:
00060     // Variables:
00061     int m_numberOfArrays;
00062     int m_start;
00063     int m_numberOfNodes;
00064     int m_value;        
00065 
00066 protected:
00067     // Pointer to m_heapAsArray 
00068     T* m_heapAsArray;           
00069 
00070 public:
00071     
00085     // Constructor
00086     HeapMax(T init);
00087 
00095     // Constructor
00096     HeapMax(T init,int);
00097 
00102     // Destructor
00103     ~HeapMax();
00104 
00109     void sortIn(int);
00110 
00121     void heapify(int);
00122 
00128     void exchange(T *,T *);
00129 
00130  
00131 
00147     void insertKey(T);
00148 
00152     void printHeap();
00153 
00159     bool emptyHeap();
00160 
00167     void setValue(int);
00168 
00172     void increaseNumberOfNodes();
00173 
00177     void decreaseNumberOfNodes();
00178 
00183     int getNumberOfNodes();
00184        
00185 };
00186 
00187 
00188 
00189 
00190 // Constructor
00191 template <class T>
00192 inline HeapMax<T>::HeapMax(T init)  {
00193     // Initializing of the variables
00194     m_numberOfNodes = 0;
00195     m_start = 1;
00196     m_value = 10;
00197     m_numberOfArrays = m_value;
00198     // Try to allocate
00199     try {
00200         m_heapAsArray = new T[m_numberOfArrays]; 
00201     }
00202     // Not enough memory -> abortion
00203     catch (std::bad_alloc) {
00204         cerr << "Not enough memory!" << endl;
00205         return;
00206     }
00207     // Initializing the allocated field
00208     for(int i=0;i<=m_numberOfArrays;i=i+1) { 
00209         m_heapAsArray[i] = init;
00210     }
00211 }
00212 
00213 
00214 // Constructor with the possibility to set m_value by _value
00215 template <class T>
00216 inline HeapMax<T>::HeapMax(T init,int _value)  {
00217     // Check for invalid input
00218     if(_value<1) {
00219         cout << "\nInvalid input for m_value\n" ;
00220         return;
00221     }
00222     // After a positive check: Construction 
00223     else {
00224         // Initializing of the variables
00225         m_numberOfNodes=0;
00226         m_start = 1;
00227         m_value = _value;
00228         m_numberOfArrays=m_value;
00229         // Try to allocate
00230         try {
00231             m_heapAsArray = new T[m_numberOfArrays]; 
00232         }
00233         // Not enough memory -> abortion
00234         catch (std::bad_alloc) {
00235             cerr << "Not enough memory!" << endl;
00236             return;
00237         }
00238         // Initializing the allocated field
00239         for(int i=0;i<=m_numberOfArrays;i=i+1) { 
00240             m_heapAsArray[i] = init;
00241         }
00242     }
00243 }
00244 
00245 
00246 // Destructor
00247 template <class T>
00248 inline HeapMax<T>::~HeapMax() {
00249     delete [] m_heapAsArray;
00250 }
00251 
00252 
00253 // Method to change m_value again after construction
00254 template <class T>
00255 inline void HeapMax<T>::setValue(int _value) {
00256     // Check for invalid input
00257     if(_value<1) {
00258         cout << "\nInvalid input for m_yvalue!\n" ;
00259         return;
00260     }
00261     // After positiv check: 
00262     else {
00263         m_value = _value;
00264     }
00265 }
00266 
00267 
00268 // Method to increase m_numberOfNodes 
00269 template <class T>
00270 inline void HeapMax<T>::increaseNumberOfNodes() {
00271     m_numberOfNodes = m_numberOfNodes+1;
00272 }
00273 
00274 
00275 // Method to decrease m_numberOfNodes
00276 template <class T>
00277 inline void HeapMax<T>::decreaseNumberOfNodes() {
00278     m_numberOfNodes = m_numberOfNodes-1;
00279 }
00280 
00281 
00282 // Method to get m_numberOfNodes
00283 template <class T>
00284 inline int HeapMax<T>:: getNumberOfNodes() {
00285     return m_numberOfNodes;
00286 }
00287 
00288 
00289 // Method to find out whether the heap is empty or not
00290 template <class T>
00291 inline bool HeapMax<T>::emptyHeap() {
00292     if (m_numberOfNodes == 0) {
00293         return true;
00294     }
00295     else {
00296         return false;
00297     }
00298 }
00299 
00300 
00301 // Prints the complete heap
00302 template <class T>
00303 inline void HeapMax<T>::printHeap() {
00304     for(int i=m_start; i <= m_numberOfNodes; i = i+1) {
00305         cout << m_heapAsArray[i] << " "  ;
00306     }
00307     cout << endl ;
00308 }
00309 
00310 
00311 // Method to add the key _value to the heap
00312 template <class T>
00313 inline void HeapMax<T>::insertKey(T _value) {
00314         
00315     // Increase number of nodes
00316     m_numberOfNodes =m_numberOfNodes+1;
00317     
00318     // If the current field m_heapAsArray is to small:
00319     // Create an new field which is bigger about the size of m_value
00320     if (m_numberOfNodes >= m_numberOfArrays) {
00321         m_numberOfArrays = m_numberOfArrays+m_value;  
00322         T* heapAsArray2;
00323         
00324         // Try to allocate a new field
00325         try {
00326                  heapAsArray2 = new T[m_numberOfArrays];
00327         }
00328         // Exception handling:
00329         catch (std::bad_alloc) {
00330             // If m_value < 50 exit 
00331             if(m_value<50) {
00332                 cerr << "Not enough memory!" << endl;
00333                 exit;
00334             }
00335             // Else halve m_value and try again
00336             else {
00337                 m_value = m_value/2;
00338                 insertKey(_value);
00339             }
00340         }
00341         
00342         // Copying from m_heapAsArray to the temporary heapAsArray2
00343         for (int i=m_start; i<m_numberOfNodes; i=i+1) {  
00344             heapAsArray2[i] = m_heapAsArray[i];
00345         }
00346         heapAsArray2[m_numberOfNodes] =_value;
00347         delete [] m_heapAsArray;
00348         m_heapAsArray = heapAsArray2;
00349     }
00350     // Write the new key 
00351     m_heapAsArray[m_numberOfNodes] = _value;
00352         
00353     // Sort in the new insert key by calling sortIn
00354     sortIn(m_numberOfNodes);
00355 }
00356 
00357 
00358 // Sort in the given node to the right position of the heap
00359 template <class T>
00360 inline void HeapMax<T>::sortIn(int node) {
00361     if (node>1) {
00362         // Determine the father node 
00363         int parentNumber = (node)/2;
00364         
00365         // If the heap condition is violated exchange 
00366         // the values of father and son by calling the method exchange
00367         if (m_heapAsArray[node] > m_heapAsArray[parentNumber]) {
00368             exchange(&m_heapAsArray[node], &m_heapAsArray[parentNumber]);
00369             
00370             // Recurent call of sortIn until node>1 
00371             sortIn(parentNumber);
00372         }  
00373     }
00374 }
00375 
00376 
00377 // Method to exchange the values of the keys node1 and node2 
00378 template <class T>
00379 inline void HeapMax<T>::exchange(T *node1, T *node2) {
00380     T temp = *node1;
00381     *node1 = *node2;
00382     *node2 = temp;
00383 }
00384 
00385 
00386 // Method to keep or rather to achive the heap condition
00387 template <class T>
00388 inline void HeapMax<T>::heapify(int node) {
00389     int leftSon = 2*node;
00390     int rightSon = 2*node+1 ;
00391     int selectedSon;
00392     
00393     // No existing right son
00394     if ((leftSon <= m_numberOfNodes) && (rightSon > m_numberOfNodes)) {
00395         // If heap condition is violated exchange the value of 
00396         // father and left son
00397         if (m_heapAsArray[leftSon] > m_heapAsArray[node]) {
00398             exchange(&m_heapAsArray[node],&m_heapAsArray[leftSon]);
00399         }
00400     }
00401     else {
00402         // Existing right son
00403         if (rightSon <= m_numberOfNodes) {
00404             // Compare of left and right son and choose the bigger once 
00405             if (m_heapAsArray[leftSon] > m_heapAsArray[rightSon]) {
00406                 selectedSon = leftSon;
00407             }
00408             else {
00409                 selectedSon = rightSon;
00410             }
00411             // If heap condition is violated exchange the value of 
00412             // father and selected son
00413             if (m_heapAsArray[selectedSon] > m_heapAsArray[node]) {
00414                 exchange(&m_heapAsArray[node],&m_heapAsArray[selectedSon]);
00415                 heapify(selectedSon);
00416             }
00417         }
00418     }
00419 }
00420 
00421 
00422 #endif
00423 

Generated on Sun Mar 9 11:43:45 2003 by doxygen1.2.17