00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00027 #define is_root(_ND) (_ND->m_id==1)
00028 #define odd_id(_ND) (_ND->m_id & 1)
00029 #define is_lchild(_ND) ((_ND!=NULL)&&!(is_root(_ND))&&(!odd_id(_ND)))
00030 #define is_rchild(_ND) ((_ND!=NULL)&&!(is_root(_ND))&&( odd_id(_ND)))
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 namespace Marsyas
00057 {
00058
00059 template <typename Type, typename Comparator>
00060 class Heap {
00061 private:
00062 class Node {
00063 public:
00064 Node* parent; Node* lchild; Node* rchild;
00065 Node* prev; Node* next;
00066 unsigned int m_id;
00067 Type* data;
00068 Node(unsigned int node_id, Type* d) {
00069 parent=NULL; lchild=NULL; rchild=NULL;
00070 prev=NULL; next=NULL;
00071 data=d; m_id=node_id;
00072 }
00073 ~Node() { }
00074 friend std::ostream& operator<<(std::ostream& o, Node* s) {
00075 o << "<" << s->m_id << "," << s->data << ",(";
00076 if (s->parent==NULL) { o<<"0"; } else { o<<s->parent->m_id; } o << ",";
00077 if (s->lchild==NULL) { o<<"x"; } else { o<<s->lchild->m_id; } o << ",";
00078 if (s->rchild==NULL) { o<<"x"; } else { o<<s->rchild->m_id; } o << ")>";
00079 return o;
00080 };
00081 };
00082
00083 public:
00084
00085 Node* first; Node* last;
00086
00087
00088 unsigned int node_count;
00089
00090 Comparator cmp;
00091
00092 Heap() { first=NULL; last=NULL; node_count=0; }
00093 virtual ~Heap() {
00094 while(first!=NULL) {
00095 last=first->next;
00096
00097 delete(first->data); delete(first);
00098 first=last;
00099 }
00100 }
00101
00102 bool empty() { return (node_count==0); };
00103
00104 Type* top() {
00105
00106 if (first==NULL) { throw "Heap::top() empty heap exception."; }
00107 else { return first->data; }
00108 };
00109 Type* pop() {
00110
00111 if (first==NULL) { throw "Heap::pop() empty heap exception."; }
00112
00113 Type* data = first->data;
00114
00115 if (is_root(last)) {
00116 delete(last); first=NULL; last=NULL; node_count=0;
00117 }
00118 else {
00119
00120 first->data = last->data;
00121
00122 if (is_lchild(last)) { last->parent->lchild=NULL; }
00123 else { last->parent->rchild=NULL; }
00124
00125 last=last->prev; delete(last->next); last->next=NULL;
00126
00127 Node* f = first;
00128 while (1) {
00129 Node* c = f->lchild;
00130
00131 if (c==NULL) { break; }
00132
00133 if (f->rchild!=NULL && (cmp(f->rchild->data,c->data))) { c=f->rchild; }
00134
00135
00136 if (cmp(f->data,c->data)) { break; }
00137
00138 Type* sw=c->data; c->data=f->data; f->data=sw;
00139
00140 f=c;
00141 }
00142
00143 node_count = (node_count>0) ? node_count-1 : 0;
00144 }
00145 return data;
00146 };
00147
00148 void push(Type* data) {
00149
00150 if (data==NULL) { return; }
00151 node_count++;
00152
00153 Node* n = new Node(node_count,data);
00154 if (first==NULL) {
00155 first=n; last=n;
00156 }
00157 else {
00158
00159 last->next=n; n->prev=last;
00160
00161 if (is_root(last)) {
00162 n->parent=last;
00163 last->lchild = n;
00164 }
00165 else if (is_lchild(last)) {
00166 n->parent=last->parent;
00167 last->parent->rchild=n;
00168 }
00169 else {
00170 n->parent=last->parent->next;
00171 last->parent->next->lchild=n;
00172 }
00173 last=n;
00174
00175 while (!is_root(n) && cmp(n->data,n->parent->data)) {
00176 Type* sw=n->data; n->data=n->parent->data; n->parent->data=sw;
00177 n = n->parent;
00178 }
00179 }
00180 };
00181 friend std::ostream& operator<<(std::ostream& o, Heap& s) {
00182 Node* c = s.first;
00183 while (c!=NULL) { o << c; c = c->next; } o << "\n";
00184 return o;
00185 };
00186 };
00187
00188 }
00189
00190
00191
00192
00193