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