Grafalgo
Library of useful data structures and algorithms
|
00001 00009 #ifndef DHEAP_H 00010 #define DHEAP_H 00011 00012 //#include "stdinc.h" 00013 #include "Adt.h" 00014 00015 namespace grafalgo { 00016 00017 typedef int keytyp; 00018 00023 class Dheap : public Adt { 00024 public: Dheap(int,int); 00025 ~Dheap(); 00026 00027 // common methods 00028 void clear(); 00029 void resize(int); 00030 void expand(int); 00031 void copyFrom(const Dheap&); 00032 00033 // access methods 00034 index findmin() const; 00035 keytyp key(index) const; 00036 00037 // predicates 00038 bool member(index) const; 00039 bool empty() const; 00040 int size() const; 00041 00042 // modifiers 00043 void insert(index,keytyp); 00044 void remove(index); 00045 index deletemin(); 00046 void changekey(index,keytyp); 00047 00048 string& toString(string&) const; 00049 private: 00050 int d; 00051 int hn; 00052 00053 index *h; 00054 int *pos; 00055 keytyp *kee; 00056 00057 index minchild(index); 00058 void siftup(index,int); 00059 void siftdown(index,int); 00060 00061 void makeSpace(int); 00062 void freeSpace(); 00063 }; 00064 00068 inline int Dheap::findmin() const { return hn == 0 ? 0 : h[1]; } 00069 00074 inline int Dheap::deletemin() { 00075 if (hn == 0) return 0; 00076 index i = h[1]; remove(h[1]); 00077 return i; 00078 } 00079 00084 inline keytyp Dheap::key(index i) const { return kee[i]; } 00085 00090 inline bool Dheap::member(index i) const { return pos[i] != 0; } 00091 00095 inline bool Dheap::empty() const { return hn == 0; }; 00096 00099 inline int Dheap::size() const { return hn; }; 00100 00101 } // ends namespace 00102 00103 #endif