1015 - #include<iostream> using namespace std; /...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
#include<iostream> using namespace std; // Chordal Graph enum { SIZ = 1008, }; // little head template<typename Node, typename cmp = less<Node> > class Heap { public: Heap():b(0), pos(0){} void set(Node *n){b = n, pos=0;} void push(size_t cur); void pop(); size_t top(); bool empty(){ return pos<=0; } private: void update(size_t ); Heap(const Heap& ); Heap& operator=(const Heap& ); Node *b; cmp c; size_t pos; size_t h[SIZ]; }; template<typename Node, typename cmp> void Heap<Node, cmp>::push(size_t cur){ if(b[cur].ptr != -1){ update(b[cur].ptr); return ; } b[cur].ptr = pos; h[pos++] = cur; update(pos-1); } template<typename Node, typename cmp> void Heap<Node, cmp>::pop(){ if( pos <=0) return; b[h[0]].ptr = -1; if(--pos==0) return; h[0] = h[pos]; b[h[0]].ptr = 0; int r = 0, p = (r+1)*2-1; while(p < pos){ p ++; } if( c(b[h[r]], b[h[p]])){ break; } int t = h[r]; h[r] = h[p]; h[p] = t; b[h[r]].ptr = r; b[h[p]].ptr = p; r = p; p = (r+1)*2 -1;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
} } template<typename Node, typename cmp>
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 3

1015 - #include&lt;iostream&gt; using namespace std; /...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online