Main Page   Class Hierarchy   Compound List   File List   Compound Members  

HeapMin.hh

00001 
00002 //
00003 //  Module           : HeapMin.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 HeapMin_HH
00022 #define HeapMin_HH
00023 
00024 
00025 
00055 template <class T>
00056 class HeapMin {
00057 
00058 private:
00059     // Variables:
00060     int m_numberOfArrays;
00061     int m_start;
00062     int m_numberOfNodes;
00063     int m_value;        
00064 
00065 protected:    
00066     // Pointer to m_heapAsArray 
00067     T* m_heapAsArray;           
00068 
00069 public:
00083     // Constructor  
00084     HeapMin(T init);
00085 
00093     // Constructor 
00094     HeapMin(T init,int);
00095 
00100     // Destructor
00101     ~HeapMin();
00102 
00107     void sortIn(int);
00108 
00119     void heapify(int);
00120 
00126     void exchange(T *,T *);
00127 
00128 
00129 
00144     void insertKey(T);
00145 
00149     void printHeap();
00150 
00156     bool emptyHeap();
00157     
00164     void setValue(int);
00165 
00169     void increaseNumberOfNodes();
00170 
00174     void decreaseNumberOfNodes();
00175 
00180     int getNumberOfNodes();
00181 };
00182 
00183 
00184 // Constructor
00185 template <class T>
00186 inline HeapMin<T>::HeapMin(T init)  {
00187     // Initializing of the variables
00188     m_numberOfNodes = 0;
00189     m_start = 1;
00190     m_value = 10;
00191     m_numberOfArrays = m_value;
00192     // Try to allocate
00193     try {
00194         m_heapAsArray = new T[m_numberOfArrays]; 
00195     }
00196     // Not enough memory -> abortion
00197     catch (std::bad_alloc) {
00198         cerr << "Not enough memory!" << endl;
00199         return;
00200     }
00201     // Initializing the allocated field
00202     for(int i=0;i<=m_numberOfArrays;i=i+1) { 
00203         m_heapAsArray[i] = init;
00204     }
00205 }
00206 
00207 
00208 // Constructor with the possibility to set m_value by _value
00209 template <class T>
00210 inline HeapMin<T>::HeapMin(T init,int _value)  {
00211     // Check for invalid input
00212     if(_value<1) {
00213         cout << "\nInvalid input for m_value\n" ;
00214         return;
00215     }
00216     // After a positive check: Construction 
00217     else {
00218         // Initializing of the variables
00219         m_numberOfNodes = 0;
00220         m_start = 1;
00221         m_value = _value;
00222         m_numberOfArrays = m_value;
00223         // Try to allocate
00224         try {
00225             m_heapAsArray = new T[m_numberOfArrays]; 
00226         }
00227         // Not enough memory -> abortion
00228         catch (std::bad_alloc) {
00229             cerr << "Not enough memory!" << endl;
00230             return;
00231         }
00232         // Initializing the allocated field
00233         for(int i=0;i<=m_numberOfArrays;i=i+1) { 
00234             m_heapAsArray[i] = init;
00235         }
00236     }
00237 }
00238 
00239 
00240 // Destructor
00241 template <class T>
00242 inline HeapMin<T>::~HeapMin() {
00243     delete [] m_heapAsArray;
00244 }
00245 
00246 
00247 // Method to change m_value again after construction
00248 template <class T>
00249 inline void HeapMin<T>::setValue(int _value) {
00250     // Check for invalid input
00251     if(_value<1) {
00252         cout << "\nInvalid input for m_value!\n" ;
00253         return;
00254     }
00255     // After positiv check: 
00256     else {
00257         m_value = _value;
00258     }
00259 }
00260 
00261 
00262 // Method to increase m_numberOfNodes 
00263 template <class T>
00264 inline void HeapMin<T>::increaseNumberOfNodes() {
00265     m_numberOfNodes = m_numberOfNodes+1;
00266 }
00267 
00268 
00269 // Method to decrease m_numberOfNodes
00270 template <class T>
00271 inline void HeapMin<T>::decreaseNumberOfNodes() {
00272     m_numberOfNodes = m_numberOfNodes-1;
00273 }
00274 
00275 
00276 // Method to get m_numberOfNodes
00277 template <class T>
00278 inline int HeapMin<T>:: getNumberOfNodes() {
00279     return m_numberOfNodes;
00280 }
00281 
00282 
00283 // Method to find out whether the heap is empty or not
00284 template <class T>
00285 inline bool HeapMin<T>::emptyHeap() {
00286     if (m_numberOfNodes == 0) {
00287         return true;
00288     }
00289     else {
00290         return false;
00291     }
00292 }
00293 
00294 
00295 // Prints the complete heap
00296 template <class T>
00297 inline void HeapMin<T>::printHeap() {
00298     for(int i=m_start; i <= m_numberOfNodes; i = i+1) {
00299         cout << m_heapAsArray[i] << " "  ;
00300     }
00301     cout << endl ;
00302 }
00303 
00304 
00305 // Method to add the key _value to the heap
00306 template <class T>
00307 inline void HeapMin<T>::insertKey(T _value) {
00308         
00309     // Increase number of nodes
00310     m_numberOfNodes =m_numberOfNodes+1;
00311     
00312     // If the current field m_heapAsArray is to small:
00313     // Create an new field which is bigger about the size of m_value
00314     if (m_numberOfNodes >= m_numberOfArrays) {
00315         m_numberOfArrays = m_numberOfArrays+m_value;  
00316         T* heapAsArray2;
00317         
00318         // Try to allocate a new field
00319         try {
00320                  heapAsArray2 = new T[m_numberOfArrays];
00321         }
00322         // Exception handling:
00323         catch (std::bad_alloc) {
00324             // If m_value < 50 exit 
00325             if(m_value<50) {
00326                 cerr << "Not enough memory!" << endl;
00327                 exit;
00328             }
00329             // Else halve m_value and try again
00330             else {
00331                 m_value = m_value/2;
00332                 insertKey(_value);
00333             }
00334         }
00335         
00336         // Copying from m_heapAsArray to the temporary heapAsArray2
00337         for (int i=m_start; i<m_numberOfNodes; i=i+1) {  
00338             heapAsArray2[i] = m_heapAsArray[i];
00339         }
00340         heapAsArray2[m_numberOfNodes] =_value;
00341         delete [] m_heapAsArray;
00342         m_heapAsArray = heapAsArray2;
00343     }
00344     // Write the new key 
00345     m_heapAsArray[m_numberOfNodes] = _value;
00346         
00347     // Sort in the new insert key by calling sortIn
00348     sortIn(m_numberOfNodes);
00349 }
00350 
00351 
00352 // Sort in the given node to the right position of the heap
00353 template <class T>
00354 inline void HeapMin<T>::sortIn(int node) {
00355     if (node>1) {
00356         // Determine the father node 
00357         int parentNumber = (node)/2;
00358         
00359         // If the heap condition is violated exchange 
00360         // the values of father and son by calling the method exchange
00361         if (m_heapAsArray[node] < m_heapAsArray[parentNumber]) {
00362             exchange(&m_heapAsArray[node], &m_heapAsArray[parentNumber]);
00363             
00364             // Recurent call of sortIn until node>1 
00365             sortIn(parentNumber);
00366         }  
00367     }
00368 }
00369 
00370 
00371 // Method to exchange the values of the keys node1 and node2 
00372 template <class T>
00373 inline void HeapMin<T>::exchange(T *node1, T *node2) {
00374     T temp = *node1;
00375     *node1 = *node2;
00376     *node2 = temp;
00377 }
00378 
00379 
00380 // Method to keep or rather to achive the heap condition
00381 template <class T>
00382 inline void HeapMin<T>::heapify(int node) {
00383     int leftSon = 2*node;
00384     int rightSon = 2*node+1 ;
00385     int selectedSon;
00386     
00387     // No existing right son
00388     if ((leftSon <= m_numberOfNodes) && (rightSon > m_numberOfNodes)) {
00389         // If heap condition is violated exchange the value of 
00390         // father and left son
00391         if (m_heapAsArray[leftSon] < m_heapAsArray[node]) {
00392             exchange(&m_heapAsArray[node],&m_heapAsArray[leftSon]);
00393         }
00394     }
00395     else {
00396         // Existing right son
00397         if (rightSon <= m_numberOfNodes) {
00398             // Compare of left and right son and choose the smaller once 
00399             if (m_heapAsArray[leftSon] < m_heapAsArray[rightSon]) {
00400                 selectedSon = leftSon;
00401             }
00402             else {
00403                 selectedSon = rightSon;
00404             }
00405             // If heap condition is violated exchange the value of 
00406             // father and selected son
00407             if (m_heapAsArray[selectedSon] < m_heapAsArray[node]) {
00408                 exchange(&m_heapAsArray[node],&m_heapAsArray[selectedSon]);
00409                 heapify(selectedSon);
00410             }
00411         }
00412     }
00413 }
00414        
00415 #endif
00416 
00417 
00418 

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