[Lemon-commits] deba: r3424 - lemon/trunk/lemon

Lemon SVN svn at lemon.cs.elte.hu
Thu Dec 20 16:21:22 CET 2007


Author: deba
Date: Thu Dec 20 16:21:22 2007
New Revision: 3424

Modified:
   lemon/trunk/lemon/bin_heap.h

Log:
Bug fix in erase




Modified: lemon/trunk/lemon/bin_heap.h
==============================================================================
--- lemon/trunk/lemon/bin_heap.h	(original)
+++ lemon/trunk/lemon/bin_heap.h	Thu Dec 20 16:21:22 2007
@@ -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<length && less(data[child], p) ) {
+	move(data[child], hole);
+	hole=child;
+      }
+    ok:
+      move(p, hole);
+      return hole;
+    }
 
     void move(const PairType &p, int i) {
       data[i] = p;
       iim.set(p.first, i);
     }
 
-    void rmidx(int h) {
-      int n = data.size()-1;
-      if( h>=0 && h<=n ) {
-	iim.set(data[h].first, POST_HEAP);
-	if ( h<n ) {
-	  bubble_down(h, data[n], n);
-	}
-	data.pop_back();
-      }
-    }
-
   public:
     /// \brief Insert a pair of item and priority into the heap.
     ///
@@ -185,16 +205,29 @@
     /// Compare from the heap.  
     /// \pre The heap must be non-empty.  
     void pop() {
-      rmidx(0);
+      int n = data.size()-1;
+      iim.set(data[0].first, POST_HEAP);
+      if (n > 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 <typename V, typename M, typename C>
-  int BinHeap<V,M,C>::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 <typename V, typename M, typename C>
-  int BinHeap<V,M,C>::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<length && less(data[child], p) ) {
-      move(data[child], hole);
-      hole=child;
-    }
-  ok:
-    move(p, hole);
-    return hole;
-  }
-
-
 } // namespace lemon
 
 #endif // LEMON_BIN_HEAP_H



More information about the Lemon-commits mailing list