min-heap

Creating Min Heap from STL Priority Queue

丶灬走出姿态 提交于 2019-12-04 09:46:39
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 understand. d:\microsoft visual studio 10.0\vc\include\xutility(674): error C2064: term does not evaluate to

How to update elements within a heap? (priority queue)

末鹿安然 提交于 2019-12-03 12:37:34
When using a min/max-heap algorithm, priorities may change. One way to handle this is to removal and insert the element to update the queue order. For priority queues implemented using arrays, this can be a performance bottleneck that seems avoidable, especially for cases when the change to priority is small. Even if this is not a standard operation for a priority queue , this is a custom implementation which can be modified for my needs. Are there well known best-practice methods for updating elements in the min/max-heap? Background Info: I'm no expert in binary-trees, I inherited some code

min heap in python

柔情痞子 提交于 2019-12-03 11:48:44
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? 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 module, since it provides no key= or cmp= arguments like the sorting functions/methods do. def gen_wrapper(cmp):

Given K sorted lists of up to N elements in each list, return a sorted iterator over all the items

匆匆过客 提交于 2019-12-02 22:57:48
问题 Example: List 1: [1, 4, 5, 8, 9] List 2: [3, 4, 4, 6] List 3: [0, 2, 8] Would yield the following result: Iterator -> [0, 1, 2, 3, 4, 4, 4, 5, 6, 8, 8, 9] I am reluctant to create a "merge" method that accepts the k lists and merges the contents of the List to another List in the spirit of space complexity. Is this a k-way merge problem that can be implemented using "min Heap". Any pointers would be very helpful. public class CustomListIterator<E> implements Iterator<E>{ private boolean

Given K sorted lists of up to N elements in each list, return a sorted iterator over all the items

女生的网名这么多〃 提交于 2019-12-02 13:13:22
Example: List 1: [1, 4, 5, 8, 9] List 2: [3, 4, 4, 6] List 3: [0, 2, 8] Would yield the following result: Iterator -> [0, 1, 2, 3, 4, 4, 4, 5, 6, 8, 8, 9] I am reluctant to create a "merge" method that accepts the k lists and merges the contents of the List to another List in the spirit of space complexity. Is this a k-way merge problem that can be implemented using "min Heap". Any pointers would be very helpful. public class CustomListIterator<E> implements Iterator<E>{ private boolean canAddIterators = true; private boolean balanceTreeIteratorFlag = false; private E f_element; private E s

Pop max value from a heapq python, Is there a max-heap in Python? [duplicate]

别说谁变了你拦得住时间么 提交于 2019-11-30 16:28:07
Possible Duplicate: What do I use for a max-heap implementation in Python? I am trying to implement in some way the heapq of python but for a max-heap. A solution is using the (-1) and multiple with numbers of the queue but that doesn't help me as I need to store urls in the heap. So i want a max heapq where i can pop the largest value. Wrap the objects in a reverse-comparing wrapper: import functools @functools.total_ordering class ReverseCompare(object): def __init__(self, obj): self.obj = obj def __eq__(self, other): return isinstance(other, ReverseCompare) and self.obj == other.obj def _

How to update elements within a heap? (priority queue)

瘦欲@ 提交于 2019-11-30 14:59:22
问题 When using a min/max-heap algorithm, priorities may change. One way to handle this is to removal and insert the element to update the queue order. For priority queues implemented using arrays, this can be a performance bottleneck that seems avoidable, especially for cases when the change to priority is small. Even if this is not a standard operation for a priority queue, this is a custom implementation which can be modified for my needs. Are there well known best-practice methods for updating

Pop max value from a heapq python, Is there a max-heap in Python? [duplicate]

走远了吗. 提交于 2019-11-29 23:34:10
问题 Possible Duplicate: What do I use for a max-heap implementation in Python? I am trying to implement in some way the heapq of python but for a max-heap. A solution is using the (-1) and multiple with numbers of the queue but that doesn't help me as I need to store urls in the heap. So i want a max heapq where i can pop the largest value. 回答1: Wrap the objects in a reverse-comparing wrapper: import functools @functools.total_ordering class ReverseCompare(object): def __init__(self, obj): self

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

百般思念 提交于 2019-11-27 22:12:55
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 min heap creation TemplateRex The logical argument is as follows std::priority_queue is a container