// CMSC 341 - Fall 2019 - Project 5 // heap.h // Templated, vector-based heap implementation // To work with Heap, the template class T must provide the following: // (a) Default constructor // (b) priority function (unsigned priority()) // (c) overloaded insertion operator // To work with Heap *and* HashTable, it also needs: // (d) key function (string key()) #ifndef _HEAP_H #define _HEAP_H #include #include #include #include // swap using std::vector; using std::endl; using std::cout; using std::swap; using std::range_error; // To work with Heap and HashTable, class T must provide the // following: // (a) Default constructor // (b) priority function (unsigned priority()) // (c) key function (string key()) // (d) overloaded insertion operator class Grader; template class Heap { friend Grader; public: // Constructor Heap(); // Inline functions: size, empty, used unsigned size() const { return _heap.size() - 1 ; } bool empty() const { return _heap.size() <= 1 ; } bool used() const { return _used ; } // Main heap operations: insert, read, remove void insert(const T& object); T readTop() const; void removeTop(); // Dump the entire heap void dump() const; // Root node always has index 1 static const unsigned ROOT = 1; private: vector _heap; // Vector-based heap bool _used; // Has the heap ever been used? Needed for // linear probing. // ********************************************* // Private helper function declarations go here! // ********************************************* }; // *************************************** // Templated function definitions go here! // *************************************** #endif