# HG changeset patch # User deba # Date 1198164082 0 # Node ID b5eba564bb602a9578dd92614c5c3d7e3bec182b # Parent 2bed3e806e1ebee951f6ff920fa497af99dd5c48 Bug fix in erase diff -r 2bed3e806e1e -r b5eba564bb60 lemon/bin_heap.h --- a/lemon/bin_heap.h Thu Dec 20 15:13:06 2007 +0000 +++ b/lemon/bin_heap.h Thu Dec 20 15:21:22 2007 +0000 @@ -120,30 +120,50 @@ private: static int parent(int i) { return (i-1)/2; } + static int second_child(int i) { return 2*i+2; } bool less(const PairType &p1, const PairType &p2) const { return comp(p1.second, p2.second); } - int bubble_up(int hole, PairType p); - int bubble_down(int hole, PairType p, int length); + int bubble_up(int hole, PairType p) { + int par = parent(hole); + while( hole>0 && less(p,data[par]) ) { + move(data[par],hole); + hole = par; + par = parent(hole); + } + move(p, hole); + return hole; + } + + int bubble_down(int hole, PairType p, int length) { + int child = second_child(hole); + while(child < length) { + if( less(data[child-1], data[child]) ) { + --child; + } + if( !less(data[child], p) ) + goto ok; + move(data[child], hole); + hole = child; + child = second_child(hole); + } + child--; + if( child=0 && h<=n ) { - iim.set(data[h].first, POST_HEAP); - if ( h 0) { + bubble_down(0, data[n], n); + } + data.pop_back(); } /// \brief Deletes \c i from the heap. /// - /// This method deletes item \c i from the heap, if \c i was - /// already stored in the heap. - /// \param i The item to erase. + /// This method deletes item \c i from the heap. + /// \param i The item to erase. + /// \pre The item should be in the heap. void erase(const ItemType &i) { - rmidx(iim[i]); + int h = iim[i]; + int n = data.size()-1; + iim.set(data[h].first, POST_HEAP); + if( h < n ) { + if ( bubble_up(h, data[n]) == h) { + bubble_down(h, data[n], n); + } + } + data.pop_back(); } @@ -289,44 +322,7 @@ } }; // class BinHeap - - template - int BinHeap::bubble_up(int hole, PairType p) { - int par = parent(hole); - while( hole>0 && less(p,data[par]) ) { - move(data[par],hole); - hole = par; - par = parent(hole); - } - move(p, hole); - return hole; - } - - template - int BinHeap::bubble_down(int hole, PairType p, int length) { - int child = second_child(hole); - while(child < length) { - if( less(data[child-1], data[child]) ) { - --child; - } - if( !less(data[child], p) ) - goto ok; - move(data[child], hole); - hole = child; - child = second_child(hole); - } - child--; - if( child