Questions tagged [min-heap]
181 questions
                    
                    43
                    
            votes
                
                3 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
        
- 42,413
- 44
- 197
- 320
                    38
                    
            votes
                
                4 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
        
- 748
- 10
- 17
                    37
                    
            votes
                
                4 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,871
- 8
- 30
- 56
                    26
                    
            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
        
- 7,450
- 9
- 37
- 65
                    23
                    
            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,985
- 12
- 73
- 138
                    20
                    
            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,565
- 25
- 78
- 125
                    20
                    
            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
        
- 759
- 2
- 8
- 16
                    20
                    
            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
        
- 1,562
- 1
- 24
- 51
                    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
        
- 52,260
- 74
- 224
- 365
                    12
                    
            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
                    12
                    
            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
        
- 649
- 1
- 7
- 17
                    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
        
- 27,279
- 27
- 74
- 92
                    11
                    
            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,387
- 9
- 17
                    8
                    
            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
        
- 137
- 2
- 11
                    6
                    
            votes
                
                6 answers
            
        Priority Queue of an array of integers in java
I want to compare by the second element of the array [[0, 30],[5, 10],[15, 20]].
PriorityQueue heap = new PriorityQueue(intervals.length, (a, b) -> a[1] - b[1]);
But I am getting an error as below
Line 8: error: array required, but Object… 
         
    
    
        Suhas Ramesh
        
- 63
- 1
- 1
- 5