//====================================================================== // File: priorque.h // Author: Timothy A. Budd // Description: This file contains the interface of the priorityQueue // classes. // // Copyright (c) 1992 by Timothy A. Budd. All Rights Reserved. // may be reproduced for any non-commercial purpose //====================================================================== #ifndef PRIORQUE_H #define PRIORQUE_H #include #include "list.h" #include "vector.h" #include "tree.h" //---------------------------------------------------------------------- // class priorityQueue // ordered collection of values permitting // rapid removal of smallest value //---------------------------------------------------------------------- template class priorityQueue { public: // add a value to the collection virtual void add(T value) = 0; // return and remove the smallest element from the collection T deleteMin(); // indicate whether there are elements in the collection virtual int isEmpty() = 0; // return the smallest element in the collection virtual T min() = 0; // remove the smallest element from the collection virtual void removeMin() = 0; }; //---------------------------------------------------------------------- // class priorityQueueList // implement priority queue protocol // using a linked list structure //---------------------------------------------------------------------- template class priorityQueueList : public priorityQueue { public: // constructors priorityQueueList(); priorityQueueList(const priorityQueueList & v); // priority queue protocol virtual void add(T value); virtual int isEmpty(); virtual T min(); virtual void removeMin(); private: // data area orderedList lst; }; //---------------------------------------------------------------------- // class heap // a priority queue managed as a vector heap //---------------------------------------------------------------------- template class heap : public priorityQueue { public: // constructors heap(unsigned int maxsize); heap(const heap &); // priority queue protocol virtual void add(T value); virtual int isEmpty(); virtual T min(); virtual void removeMin(); // delete all values void deleteAllValues(); // return the size virtual int size() { return heapsize; }; protected: // data areas vector data; unsigned int heapmax; unsigned int heapsize; }; //---------------------------------------------------------------------- // class skewHeap // heap priority implemented using skew heap merge // operations //---------------------------------------------------------------------- template class skewHeap : public priorityQueue { public: // constructors and destructor skewHeap(); skewHeap(const skewHeap & v); virtual ~skewHeap(); // priority queue protocol virtual void add(T value); virtual int isEmpty(); virtual T min(); virtual void removeMin(); // delete all values void deleteAllValues(); // merge two heaps together void add(skewHeap & secondHeap); // root of heap node * root; // private method to merge two heaps node * merge(node *, node *); }; //---------------------------------------------------------------------- // class prioityQueue implementation //---------------------------------------------------------------------- template T priorityQueue::deleteMin() { // return and remove the smallest element T result = min(); removeMin(); return result; } //---------------------------------------------------------------------- // class priorityQueueList implementation //---------------------------------------------------------------------- template priorityQueueList::priorityQueueList() { // no initialization required } template priorityQueueList::priorityQueueList( const priorityQueueList & source) : lst(source.lst) { # if 0 // remove all values from the priority queue lst.deleteAllValues(); // duplicate elements from source list listIterator itr(source.lst); for (itr.init(); !itr; ++itr) add(itr()); # endif } template void priorityQueueList::add(T value) { // add item to queue by adding to list lst.add(value); } template int priorityQueueList::isEmpty() { // queue is empty if list is empty return lst.isEmpty(); } template T priorityQueueList::min() { // smallest item is first element in list return lst.firstElement(); } template void priorityQueueList::removeMin() { // smallest item is first element in list lst.removeFirst(); } //---------------------------------------------------------------------- // class heap implementation //---------------------------------------------------------------------- template heap::heap(unsigned int maxsize) : data(maxsize), heapmax(maxsize), heapsize(0) { // no further initialization } template heap::heap(const heap & source) : data(source.data), heapmax(source.heapmax), heapsize(source.heapsize) { //data = source.data; //heapmax = source.heapmax; //heapsize = source.heapsize; } template void heap::add(T newele) { // add a new element to the heap // make sure there is room for new element if (heapsize + 1 >= heapmax) { data.setSize(data.length() + 5); heapmax += 5; } // value starts out in last position unsigned int position = heapsize++; // inv: position <= 0 and < heapmax // now percolate up while (position > 0 && newele < data[(position-1)/2]) { data[position] = data[(position-1)/2]; // inv: tree rooted at "position" is a heap position = (position - 1) / 2; } // found location, add new element data[position] = newele; // inv: tree rooted at position is a heap // inv: data holds a heap // for (int ii = 0; ii < heapsize; ii++) // cout << data[ii] << " & "; // cout << "\\\\\n"; } template int heap::isEmpty() { // heap is empty if heapsize is zero return heapsize == 0; } template T heap::min() { // smallest item is first element in vector return data[0]; } // g++ and CFRONT handle template functions differently # ifndef __GNUG__ template void buildHeap( vector & data, int heapsize, int position); # endif template void heap::removeMin() { // remove the smallest element from a heap // move the last element into the first position data[0] = data[--heapsize]; // then move into position buildHeap(data, (int) heapsize, 0); } template void heap::deleteAllValues() { // heap is empty if heapsize is zero heapsize = 0; } //---------------------------------------------------------------------- // class skewHeap implementation //---------------------------------------------------------------------- template skewHeap::skewHeap() { root = 0; } template skewHeap::skewHeap(const skewHeap & source) { // duplicate tree from source root = source.root->copy(); } template skewHeap::~skewHeap() { deleteAllValues(); } template void skewHeap::add(T val) { // to add a new value, simply merge with an empty tree // current tree node * newnode = new node(val); assert(newnode != 0); root = merge(root, newnode); } template int skewHeap::isEmpty() { // heap is empty if root is zero return root == 0; } template T skewHeap::min() { // smallest item is first at the root assert(! isEmpty()); return root->value; } template void skewHeap::removeMin() { // remove the smallest element from a skew heap assert(!isEmpty()); node * top = root; root = merge(root->right(), root->left()); delete top; } template void skewHeap::deleteAllValues() { // releasing the tree at the root deletes all the values if (root) { root->release(); root = 0; } } template void skewHeap::add(skewHeap & secondHeap) { // merge elements from a second heap into current heap root = merge(root, secondHeap.root); // empty values from second heap secondHeap.root = 0; } template node * skewHeap::merge( node * h1, node * h2) { // merge two skew heaps to form a new heap // if either tree is empty, return the other if (!h1) return h2; if (!h2) return h1; // assume smallest is root h1 if (h2->value < h1->value) return merge(h2, h1); // reverse children and recur node * lchild = h1->left(); if (lchild) { h1->left(merge(h1->right(), h2)); h1->right(lchild); } else // no left child h1->left(h2); return h1; } //---------------------------------------------------------------------- // function: heapSort //---------------------------------------------------------------------- # ifdef __GNUG__ # include "priorque.C" # endif # ifndef __GNUG__ template void heapSort(vector & data); # endif #endif