#ifndef BINARY_HEAP_H_ #define BINARY_HEAP_H_ #include // Leftist heap class // // CONSTRUCTION: with no parameters // // ******************PUBLIC OPERATIONS********************* // void insert( x ) --> Insert x // deleteMin( minItem ) --> Remove (and optionally return) smallest item // Comparable findMin( ) --> Return smallest item // bool isEmpty( ) --> Return true if empty; else false // bool isFull( ) --> Return true if full; else false // void makeEmpty( ) --> Remove all items // void merge( rhs ) --> Absorb rhs into this heap // ******************ERRORS******************************** // Throws Underflow and Overflow as warranted // Node and forward declaration because g++ does // not understand nested classes. template class LeftistHeap; template class LeftistNode { Comparable element; LeftistNode *left; LeftistNode *right; int npl; LeftistNode( const Comparable & theElement, LeftistNode *lt = NULL, LeftistNode *rt = NULL, int np = 0 ) : element( theElement ), left( lt ), right( rt ), npl( np ) { } friend class LeftistHeap; }; template class LeftistHeap { public: LeftistHeap( ); LeftistHeap( const LeftistHeap & rhs ); ~LeftistHeap( ); bool isEmpty( ) const; bool isFull( ) const; const Comparable & findMin( ) const; void insert( const Comparable & x ); void deleteMin( ); void deleteMin( Comparable & minItem ); void makeEmpty( ); void merge( LeftistHeap & rhs ); const LeftistHeap & operator=( const LeftistHeap & rhs ); private: LeftistNode *root; LeftistNode * merge( LeftistNode *h1, LeftistNode *h2 ) const; LeftistNode * merge1( LeftistNode *h1, LeftistNode *h2 ) const; void swapChildren( LeftistNode * t ) const; void reclaimMemory( LeftistNode * t ) const; LeftistNode * clone( LeftistNode *t ) const; }; #include "LeftistHeap.cpp" #endif