min-heap

Comparator for min-heap in C++

流过昼夜 提交于 2019-12-20 12:06:23
问题 I am trying to make a min-heap 1 of long s in C++ using the STL make_heap , etc., but my comparator doesn't seem to be comparing properly. The following is my current comparator: struct greater1{ bool operator()(const long& a,const long& b) const{ return a>b; } }; However, when I do std::pop_heap(humble.begin(),humble.end(),g); where g is an instance of greater1 and humble is a heap who makes [9,15,15,25] when sort_heap is called, I get a 15 popped. Is my comparator correct? what might be

easy way to maintain a min heap with stl?

我是研究僧i 提交于 2019-12-20 09:36:35
问题 for user defined struct, as I understand, it's easy. Just overload the operator <. However, for int/float etc.., do I really need to overload operator < for int? Here is what I tried: #include <iostream> #include <algorithm> #include <vector> using namespace std; bool comp(const int& a, const int& b) { return a<b?false:true; } int main () { int myints[] = {10,20,30,5,15}; vector<int> v(myints,myints+5); vector<int>::iterator it; make_heap(v.begin(), v.end(), comp); cout << "initial min heap :

How can I implement a min-heap of f64 with Rust's BinaryHeap?

隐身守侯 提交于 2019-12-18 21:51:33
问题 I want to populate a binary heap with floats--more specifically, I'd like to implement a min-heap. It seems that floats do not support Ord and thus aren't usable out of the box. My attempts to wrap them have so far failed. However it seems that if I could wrap them then I could also implement Ord in such a way that it would effectively make BinaryHeap a min-heap. Here's an example of a wrapper I tried: #[derive(PartialEq, PartialOrd)] struct MinNonNan(f64); impl Eq for MinNonNan {} impl Ord

The reason of using `std::greater` for creating min heap via `priority_queue`

不羁的心 提交于 2019-12-17 09:41:11
问题 I am wondering why for creating a min heap using the priority_queue , the std::greater should be used? std::priority_queue<T, std::vector<T>, std::greater<T> > min_heap; To me, since the smallest value is always located at the top of the heap, the employed class should be std::less Update: On the other hand, since the default behavior of priority_queue (max heap) is to hold the greatest value at the top, it looks to me that the std::greater should be used for the max heap creation and not for

Is Min Heap Function

流过昼夜 提交于 2019-12-13 14:07:48
问题 I want to write a function that tells me whether a given list is a min heap. What I have written so far: def is_min_heap(L): return _is_min_heap(L, 0) def _is_min_heap(L, i): if #base case else: return (L[i] < L[2*i+1] and _is_min_heap(L, 2*i+1)) and (L[i] < L[2*i+2] and _is_min_heap(L, 2*1+2)) I am not sure what the base case should be and is my recursive calls correct? Also how can you control that the indexes are not eventually out of range? 回答1: You have three different cases for a given

min heap in python

允我心安 提交于 2019-12-09 09:37:36
问题 I'd like to store a set of objects in a min heap by defining a custom comparison function. I see there is a heapq module available as part of the python distribution. Is there a way to use a custom comparator with this module? If not, has someone else built a custom min heap? 回答1: Yes, there is a way. Define a wrapping class that implements your custom comparator, and use a list of those instead of a list of your actual objects. That's about the best there is while still using the heapq

Simple explanation of Frederickson's heap selection algorithm

徘徊边缘 提交于 2019-12-07 05:49:35
问题 Is there any simple explanation of Frederickson's heap selection algorithm to find the k'th ranked element in O(k) time in a min-heap available anywhere online? If not, can anyone explain the gut of the algorithm? 回答1: Try googling "frederickson heap select". http://160592857366.free.fr/joe/ebooks/ShareData/An%20Optimal%20Algorithm%20for%20Selection%20in%20a%20Min-Heap.pdf 来源: https://stackoverflow.com/questions/12014892/simple-explanation-of-fredericksons-heap-selection-algorithm

C++ min heap with user-defined type

爱⌒轻易说出口 提交于 2019-12-07 04:18:51
问题 I am trying to implement a min heap in c++ for a struct type that I created. I created a vector of the type, but it crashed when I used make_heap on it, which is understandable because it doesn't know how to compare the items in the heap. How do I create a min-heap (that is, the top element is always the smallest one in the heap) for a struct type? The struct is below: struct DOC{ int docid; double rank; }; I want to compare the DOC structures using the rank member. How would I do this? I

Creating Min Heap from STL Priority Queue

不问归期 提交于 2019-12-06 04:32:46
问题 I am creating a min heap from stl priority queue. Here is my class which I am using. class Plane { private : int id ; int fuel ; public: Plane():id(0), fuel(0){} Plane(const int _id, const int _fuel):id(_id), fuel(_fuel) {} bool operator > (const Plane &obj) { return ( this->fuel > obj.fuel ? true : false ) ; } } ; In main I instantiate an object thus. priority_queue<Plane*, vector<Plane*>, Plane> pq1 ; pq1.push(new Plane(0, 0)) ; I am getting an error from xutility which I am unable to

C++ min heap with user-defined type

余生长醉 提交于 2019-12-05 09:52:09
I am trying to implement a min heap in c++ for a struct type that I created. I created a vector of the type, but it crashed when I used make_heap on it, which is understandable because it doesn't know how to compare the items in the heap. How do I create a min-heap (that is, the top element is always the smallest one in the heap) for a struct type? The struct is below: struct DOC{ int docid; double rank; }; I want to compare the DOC structures using the rank member. How would I do this? I tried using a priority queue with a comparator class, but that also crashed, and it also seems silly to