Source of Listing17-1.h


  1: //  Created by Frank M. Carrano and Timothy M. Henry.
  2: //  Copyright (c) 2017 Pearson Education, Hoboken, New Jersey.

  4: // Listing 17-1.

  6: /** Interface for the ADT heap.
  7:  @file HeapInterface.h */

  9: #ifndef HEAP_INTERFACE_
 10: #define HEAP_INTERFACE_

 12: template<class ItemType>
 13: class HeapInterface
 14: {
 15: public:
 16:    /** Sees whether this heap is empty.
 17:     @return  True if the heap is empty, or false if not. */
 18:    virtual bool isEmpty() const = 0;
 19:    
 20:    /** Gets the number of nodes in this heap.
 21:     @return  The number of nodes in the heap. */
 22:    virtual int getNumberOfNodes() const = 0;
 23:    
 24:    /** Gets the height of this heap.
 25:     @return  The height of the heap. */
 26:    virtual int getHeight() const = 0;
 27:    
 28:    /** Gets the data that is in the root (top) of this heap.
 29:        For a maxheap, the data is the largest value in the heap;
 30:        for a minheap, the data is the smallest value in the heap.
 31:     @pre  The heap is not empty.
 32:     @post  The root’s data has been returned, and the heap is unchanged.
 33:     @return  The data in the root of the heap. */
 34:    virtual ItemType peekTop() const = 0;
 35:    
 36:    /** Adds a new data item to this heap.
 37:     @param newData  The data for the new node.
 38:     @post  The heap has a new node that contains newData.
 39:     @return  True if the addition is successful, or false if not. */
 40:    virtual bool add(const ItemType& newData) = 0;
 41:    
 42:    /** Removes the data that is in the root (top) of this heap.
 43:     @return  True if the removal is successful, or false if not. */
 44:    virtual bool remove() = 0;
 45:    
 46:    /** Removes all data from this heap. */
 47:    virtual void clear() = 0;
 48:    
 49:    /** Destroys this heap and frees its assigned memory. */
 50:    virtual ~HeapInterface() { }
 51: }; // end HeapInterface
 52: #endif