Questions tagged [min-heap]

144 questions
25
votes
3 answers

Can max/min heap trees contain duplicate values?

I'm wondering if a max or min heap tree is allowed to have duplicate values? I've been unsuccessful in trying to find information regarding this with online resources alone.
Vimzy
  • 1,501
  • 6
  • 23
  • 49
24
votes
2 answers

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

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…
ideasman42
  • 30,245
  • 24
  • 136
  • 249
24
votes
3 answers

Comparator for min-heap in C++

I am trying to make a min-heap1 of longs 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{ …
Jakob Weisblat
  • 6,663
  • 8
  • 31
  • 61
21
votes
2 answers

Dijkstra algorithm. Min heap as a min-priority queue

I'm reading about Dijkstra's algorithm in CLRS, Third Edition (p. 662). Here is a part from the book I don't understand: If the graph is sufficiently sparse — in particular, E = o(V^2/lg V) — we can improve the algorithm by implementing the…
Maksim Dmitriev
  • 5,313
  • 9
  • 57
  • 124
19
votes
3 answers

easy way to maintain a min heap with stl?

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 #include
user268451
  • 659
  • 2
  • 7
  • 16
19
votes
3 answers

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

I am wondering why for creating a min heap using the priority_queue, the std::greater should be used? std::priority_queue, std::greater > min_heap; To me, since the smallest value is always located at the top of the heap, the…
Vahid Noormofidi
  • 510
  • 6
  • 13
19
votes
5 answers

Java, Finding Kth largest value from the array

I had an interview with Facebook and they asked me this question. Suppose you have an unordered array with N distinct values $input = [3,6,2,8,9,4,5] Implement a function that finds the Kth largest value. EG: If K = 0, return 9. If K = 1, return…
Hesam
  • 46,136
  • 63
  • 203
  • 340
15
votes
2 answers

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

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…
maxcountryman
  • 2,219
  • 1
  • 15
  • 42
12
votes
7 answers

Priority queue for user-defined types

I have the below struct struct node{ float val; int count; } I have several objects of this struct. Now, I want to insert these objects into a priority queue of STL such that the priority queue orders the items by count. Any idea on how to…
Programmer
  • 6,141
  • 20
  • 74
  • 114
11
votes
2 answers

min heap in python

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…
Setjmp
  • 23,834
  • 23
  • 67
  • 90
10
votes
2 answers

What is the easiest and most efficient way to make a min heap in Scala?

val maxHeap = scala.collection.mutable.PriorityQueue[Int] //Gives MaxHeap What is the most concise and efficient way to use Ordering to turn a PriorityQueue into a minHeap?
user3335040
  • 569
  • 6
  • 16
10
votes
1 answer

prove the algorithm that uses min-heap to merge k sorted lists

I'm reading CLRS and had some problem with the exercise 6.5-8. Give an O(n lg k)-time algorithm to merge k sorted lists into one sorted list, where n is the total number of elements in all the inputs lists. (Hint: use a min0heap for k-way…
jsz
  • 1,269
  • 9
  • 17
8
votes
1 answer

Max heap vs Min heap for kth smallest element

I am having trouble understanding why solutions for finding the kth smallest element uses a Max heap approach. And for the kth largest element a min heap approach. Wouldn't it make more sense to use min heap to find kth smallest element, since the…
user7722505
7
votes
1 answer

Python: Find running median with Max-Heap and Min-Heap

I'm trying to return the running median for a series of streaming numbers. To do that I use a max-heap (which stores the values on the lower half of the series) and a min-heap (which stores the values on the higher half of the series). In particular…
SergeGardien
  • 127
  • 2
  • 10
6
votes
2 answers

create PriorityQueue in O(n ) with custom comparator

I was trying to implement MST with Priorityqueue with custom comparator, but I am facing problem in building min-heap with it in O(n) time. The problem is only one constructor of Priorityqueue allows to create PriorityQueue in O(n), but it does not…
rainyday
  • 411
  • 2
  • 15
1
2 3
9 10