#ifndef __Heap__ #define __Heap__ /*****************************************************************************/ /* AUTHOR: Michael J. Radwin */ /* DATE: 2/14/94 */ /* DESCRIPTION: This file contains the code for a generic Heap class. */ /* You will need to fill in all of the methods of the CHeap */ /* methods for any other classes you need. */ /* MODIFIED: 12/14/94 */ /*****************************************************************************/ template class CHeapNode { public: CHeapNode(); virtual ~CHeapNode(); virtual void SetParent(CHeapNode * Lparent) = 0; virtual void SetChild(CHeapNode * LoldChild, CHeapNode * LnewChild) = 0; virtual void SetKey(T key) = 0; virtual CHeapNode * Insert(T key) = 0; virtual CHeapNode * Remove() = 0; virtual T GetKey() = 0; virtual T UpHeap(T key) = 0; virtual T DownHeap() = 0; virtual int Empty() = 0; virtual CHeapNode * NextNode(CHeapNode * node) = 0; virtual CHeapNode * PrevNode(CHeapNode * node) = 0; } ; template class CHeapSuperNode : public CHeapNode { friend class CHeapInternalNode; friend class CHeapLeafNode; friend class CHeap; public: CHeapSuperNode(CHeapNode * LnewNode); virtual ~CHeapSuperNode(); virtual void SetParent(CHeapNode * Lparent) { exit(-1); } ; virtual void SetChild(CHeapNode * LoldChild, CHeapNode * LnewChild) { Lroot_ = LnewChild; }; virtual void SetKey(T key) { exit(-1); }; virtual CHeapNode * Insert(T key) { return(Lroot_->Insert(key)); }; virtual CHeapNode * Remove(); virtual T GetKey() { return (T) NULL; }; virtual T UpHeap(T key) { return key; }; virtual T DownHeap(); virtual int Empty() { return Lroot_->Empty(); }; virtual CHeapNode * NextNode(CHeapNode * node) { return Lroot_->NextNode(this); }; virtual CHeapNode * PrevNode(CHeapNode * node) { return Lroot_->PrevNode(this); }; protected: CHeapNode * Lroot_; /* pointer to root of tree */ } ; template class CHeapInternalNode : public CHeapNode { public: CHeapInternalNode(T key, CHeapNode * LleftChild, CHeapNode * LrightChild ); virtual ~CHeapInternalNode(); virtual void SetParent(CHeapNode * Lparent) { Lparent_ = Lparent; } ; virtual void SetChild(CHeapNode * LoldChild, CHeapNode * LnewChild); virtual void SetKey(T key) { key_ = key; }; virtual CHeapNode * Insert(T key); virtual CHeapNode * Remove(); virtual T GetKey() { return key_; }; virtual T UpHeap(T key); virtual T DownHeap(); virtual int Empty() { return 0; }; virtual CHeapNode * NextNode(CHeapNode * node); virtual CHeapNode * PrevNode(CHeapNode * node); protected: T key_; /* this is my key value */ CHeapNode * Lparent_; /* my parent */ CHeapNode * LleftChild_; /* my left child */ CHeapNode * LrightChild_; /* my right child */ T DownHeapLeft(); T DownHeapRight(); } ; template class CHeapLeafNode : public CHeapNode { public: CHeapLeafNode(); virtual ~CHeapLeafNode(); virtual void SetParent(CHeapNode * Lparent) { Lparent_ = Lparent; } ; virtual void SetChild(CHeapNode * LoldChild, CHeapNode * LnewChild) { exit(1); }; virtual void SetKey(T key) { exit(-1); }; virtual CHeapNode * Insert(T key); virtual CHeapNode * Remove() { return NULL; }; virtual T GetKey() { return (T) NULL; } virtual T UpHeap(T key) { return (T) NULL; }; virtual T DownHeap() { return (T) NULL; }; virtual int Empty() { return 1; }; virtual CHeapNode * NextNode(CHeapNode * node) { return Lparent_; }; virtual CHeapNode * PrevNode(CHeapNode * node) { return Lparent_; }; protected: CHeapNode * Lparent_; /* my parent */ } ; /*************************************************************************/ /* Class : CHeap */ /* Descr : The CHeap class is a generic Heap class which can be used */ /* with the c++ template mechanism. */ /*************************************************************************/ template class CHeap { public: CHeap(); ~CHeap(); void Insert(T key); void Delete(T &key); int Empty() { return Lheap_->Empty(); }; int Search(T) { return 0; }; protected: CHeapSuperNode * Lheap_; /* pointer to heap supernode */ CHeapNode * LlastNode_; } ; #endif