// Computer Science 367, Section 3, Fall 1997 // Instructor: Michael Siff (siff@cs.wisc.edu) // // // FILE: pq.h // // // TEMPLATE CLASS PROVIDED: PQueue - a container template // class for a priority queue of Items with associated Keys. // // The template parameter Item is the data type of the items in // the priority queue. It may be any of the C++ built-in types (int, char, // etc.), or a class with a copy constructor, assignment operator, and // destructor. The template parameter Key is the data type of the priority // key associated with each item. It may be any C++ built-in type that // has a comparison operator (<), or a class with a copy constructor, // assignment operator, destructor, equality(==), inequality(!=), and // comparison(<). // // // CONSTRUCTORS for the PQueue template class: // PQueue() // Postcondition: The priority queue has been initialized as an empty // priority queue. // // PQueue(const PQueue& source) // Postcondition: The priority queue has been initialized as a complete copy of // source. (This is the copy constructor.) // // // CONSTANT MEMBER FUNCTIONS for the PQueue template class: // size_t size() const // Postcondition: The return value is the total number of items on the // priority queue. // // bool isEmpty() const // Postcondition: True if the priority queue is empty, false otherwise. // // Item maximum() const // Precondition: The queue is not empty. // Postcondition: A copy of the item of highest priority is returned. // // // MODIFICATION MEMBER FUNCTIONS for the PQueue template class: // void insert(const Item& it, const Key& priority) // Precondition: The priority queue is not full. // Postcondition: The item, along with its priority, has been placed on // the priority queue. // // Item extractMax() // Precondition: The queue is not empty. // Postcondition: The item (along with its key) of highest priority has // been removed from the priority queue. A copy of the item is returned. // // void operator=(const PQueue& source) // Postcondition: The queue that activated this function is now a // complete copy of source. #ifndef PQUEUE_BTHEAP_TEMPLATE_H #define PQUEUE_BTHEAP_TEMPLATE_H #include // Provides size_t template class PQueue { public: // Constructors: PQueue(); PQueue(const PQueue& source); // Destructor: ~PQueue(); // Constant member functions: size_t size() const; bool isEmpty() const; bool isFull() const; Item maximum() const; // Modification member functions: void insert(const Item& it, const Key& priority); Item extractMax(); void operator=(const PQueue& source); private: struct IKP { // Item/Key pair Item val; Key k; }; static const size_t INITIAL_CAPACITY = 2; IKP *heap; // a pointer to the root of the heap - a dynamic array size_t count; // how many item-key pairs are on the heap size_t capacity; // the size of the current dynamic array // Helper functions: size_t parent(size_t index) const; size_t left(size_t index) const; size_t right(size_t index) const; bool hasLeft(size_t index) const; bool hasRight(size_t index) const; void heapifyDown(size_t index); void heapifyUp(size_t index); }; #include "pq.C" #endif