Please send questions to
st10@humboldt.edu .
/************************************************************/
/* ADAPTED FROM: */
/* http://www.cs.colorado.edu/~main/chapter10/bt_class.h */
/* */
/* Main and Savitch, "Data Structures and Other Objects */
/* using C++", 2nd edition, Addison-Wesley, Ch.10, Ch.11 */
/************************************************************/
//-----------------------------------------------------------
// File: heap.h
// Name: Michael Main, Walter Savitch
// (adapted by Sharon M. Tuttle)
// last modified: 4-12-05
//
// Template Class: heap<Item> (a heap
// where each node contains an Item
// and the maximum value is in the root.
//
// VALUE SEMANTICS for the heap<Item> template class:
// Assignments and the copy constructor may be used
// with heap<Item> objects.
//
// DYNAMIC MEMORY USAGE by the heap<Item> template class:
// If there is insufficient dynamic memory, then the
// following functions throw bad_alloc:
// add, the copy constructor,
// and the assignment operator.
//-----------------------------------------------------------
#ifndef HEAP_H
#define HEAP_H
#include <cstdlib> // Provides NULL and size_t
#include "complete_tree.h"
using namespace std;
template <typename Item>
class heap
{
public:
/******************************************************/
/* TYPEDEFS and MEMBER CONSTANTS for the heap class */
/******************************************************/
// The template parameter, Item, is the data type of the
// items in the nodes of the heap, also
// defined as heap<Item>::value_type.
// It may be any of the C++ built-in types (int, char,
// etc.), or a class with a default constructor, a
// copy constructor, and an assignment operator.
//
typedef Item value_type;
/****************************************************/
/* CONSTRUCTORS and DESTRUCTOR */
/****************************************************/
// postcondition: creates an empty heap instance
// (with no nodes).
//
heap( );
// copy constructor
//
heap(const heap<Item>& source);
// destructor
//
// (hint: complete_tree has a clear_tree method...)
//
~heap();
/*******************************************************/
/* ACCESSORS and other constant member functions */
/* (observers) */
/*******************************************************/
// postcondition: returns the number of nodes in the
// heap.
//
size_t get_size( ) const;
// postcondition: returns true if the heap is
// empty, returns false otherwise.
//
bool is_empty( ) const;
// precondition: size( ) > 0
// postcondition: returns (non-destructively) the maximum
// value from the heap, BUT the heap is
// unchanged.
//
Item get_max( );
/****************************************************/
/* MODIFIERS and other modifying member functions */
/****************************************************/
// postcondition: entry has been added to the heap.
//
void add(const Item& entry);
// precondition: size( ) > 0
// postcondition: returns (destructively) the maximum
// value from the heap (but the heap is still a heap).
//
Item remove_max( );
// preconditions: if !empty( ), depth is the depth of the
// calling complete_tree instance.
// postconditions: if !empty, then the contents of the
// root and all of its descendants have been written to
// cout with the << operator using a backward in-order
// traversal. Each node is indented four times its depth.
//
void print_heap(size_t depth);
/****************************************************/
/* OVERLOADED OPERATORS */
/****************************************************/
// postcondition: the heap that activated this
// will be made to have the same items as source
//
void operator =(const heap<Item>& source);
private:
complete_tree<Item> compTree;
size_t used; // current number of items in the
// heap
// helper function:
// percolate_down
//
// precondition: the current heap meets the heap order
// property except for the value at the current
// node
// postcondition: the value in the current node is
// "percolated down" until it IS again a heap
//
void percolate_down( );
// helper function:
// percolate_up
//
// precondition: the current heap meets the heap order
// property EXCEPT for the value at the current node
// postcondition: value in current node has been
// "percolated down" until it IS again a heap
//
void percolate_up( );
};
#include "heap.template" // Include the implementation
#endif