00001 00002 // 00003 // Module : MinPriorityQueue.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 MinPriorityQueue_HH 00022 #define MinPriorityQueue_HH 00023 00024 00025 00050 template <class T> 00051 class MinPriorityQueue : private HeapMin<T> { 00052 00053 public: 00058 // Constructor 00059 MinPriorityQueue(T init); 00060 00066 // Constructor 00067 MinPriorityQueue(T init,int); 00068 00069 // Default destructor okay 00070 00077 void insertKeyInQueue(T); 00078 00082 void printQueue(); 00083 00091 bool emptyQueue(); 00092 00099 void setQueueValue(int); 00100 00105 T extractRoot(); 00106 00116 void deleteKey(int key); 00117 00128 void changeKey(int key ,T newValue); 00129 00130 }; 00131 00132 00133 00134 // Constructor which is calling constructor HeapMin<T>::HeapMin(T init) 00135 template <class T> 00136 inline MinPriorityQueue<T>::MinPriorityQueue(T init) 00137 :HeapMin<T>::HeapMin(init) {} 00138 00139 00140 // Constructor which is calls constructor HeapMin<T>::HeapMin(T init,int _value) 00141 template <class T> 00142 inline MinPriorityQueue<T>::MinPriorityQueue(T init,int _value) 00143 :HeapMin<T>::HeapMin(init,_value) {} 00144 00145 00146 // This method extracts the root of the heap 00147 template <class T> 00148 inline T MinPriorityQueue<T>::extractRoot() { 00149 T root; 00150 // Reach the variable m_numberOfNodes of HeapMin<T> 00151 int num = getNumberOfNodes(); 00152 // Assignment of the value of the root to root 00153 root = m_heapAsArray[1]; 00154 // Assignment of the value of the last key to the root 00155 m_heapAsArray[1] = m_heapAsArray[num]; 00156 // Cut the last node 00157 decreaseNumberOfNodes(); 00158 // Reestablish the heap condition by heapify 00159 heapify(1); 00160 return root; 00161 } 00162 00163 00164 // Method to insert a new key by calling void HeapMin<T>::insertKey(T _value) 00165 template <class T> 00166 inline void MinPriorityQueue<T>::insertKeyInQueue(T _value) { 00167 insertKey(_value); 00168 } 00169 00170 00171 // Method to print the queue by calling void HeapMin<T>::printHeap() 00172 template <class T> 00173 inline void MinPriorityQueue<T>::printQueue() { 00174 printHeap(); 00175 } 00176 00177 00178 00179 // Method to check whether the queue is empty or not by calling 00180 // bool HeapMin<T>::emptyHeap() 00181 template <class T> 00182 inline bool MinPriorityQueue<T>::emptyQueue() { 00183 emptyHeap(); 00184 } 00185 00186 00187 // With this method you are able to re-set the variable m_value of HeapMin<T> 00188 // by calling void HeapMin<T>::setValue(int _value) 00189 template <class T> 00190 inline void MinPriorityQueue<T>::setQueueValue(int reset) { 00191 setValue(reset); 00192 } 00193 00194 00195 // Method to delete a member key of the heap 00196 template <class T> 00197 inline void MinPriorityQueue<T>::deleteKey(int key) { 00198 cout << "\nDelete Key Number(" << key << "):" << endl ; 00199 // Area check 00200 if(( key < 1) || (key > getNumberOfNodes())) { 00201 cerr << "\nThe chosen key "<< key <<" is out of the admissable area\n"; 00202 return; 00203 } 00204 // If check is okay: 00205 else { 00206 int parent = key/2; 00207 // Check whether newValue is bigger or smaller than the old value 00208 // If it's bigger 00209 if(m_heapAsArray[getNumberOfNodes()] > m_heapAsArray[key]) { 00210 exchange(&(m_heapAsArray[key]),&(m_heapAsArray[getNumberOfNodes()])); 00211 // Cut the last key(->delete) 00212 decreaseNumberOfNodes(); 00213 // Achive heap condition again 00214 heapify(key); 00215 } 00216 else { 00217 // Otherwise do, while parent!=0, 00218 // (heapify (parent)&& parent=parent/2) 00219 m_heapAsArray[key]=m_heapAsArray[getNumberOfNodes()]; 00220 while(parent != 0) { 00221 heapify(parent); 00222 parent = parent/2; 00223 } 00224 } 00225 } 00226 } 00227 00228 00229 // Method to change the value of a member key to newValue 00230 template <class T> 00231 inline void MinPriorityQueue<T>::changeKey(int key,T newValue) { 00232 cout <<"\nChange Key Number("<< key << ") to Value "<< newValue <<":"<< endl; 00233 // If check is okay: 00234 if(( key < 1) || (key > getNumberOfNodes())) { 00235 cerr << "\nThe chosen key "<< key <<" is out of the admissable area\n"; 00236 return; 00237 } 00238 // If check is okay: 00239 else { 00240 int parent = key/2; 00241 // Check whether newValue is bigger or smaller than the old value 00242 // If it's bigger assign and call heapify(key) 00243 if(newValue > (m_heapAsArray[key])) { 00244 m_heapAsArray[key] = newValue; 00245 heapify(key); 00246 } 00247 // Otherwise assign and do, while parent!=0, 00248 // (heapify (parent)&& parent=parent/2) 00249 else { 00250 m_heapAsArray[key] = newValue; 00251 while(parent != 0) { 00252 heapify(parent); 00253 parent = parent/2; 00254 } 00255 } 00256 } 00257 } 00258 00259 #endif