Questions tagged [min-heap]

181 questions
0
votes
1 answer

Python: min heap swap count

Although there are lots of questions that have already been asked and answered regarding heap implementation in python, I was unable to find any practical clarifications about indexes. So, allow me to ask one more heap related question. I'm trying…
Ramiil
  • 59
  • 8
0
votes
1 answer

How does one write an add function for a min heap?

I'm trying to write an add function for a min heap in Java but I can't seem to write one that works properly. I have already tried two solutions for the add method but neither seem to work. I've tried using an int to store the previous position and…
Megan
  • 31
  • 5
0
votes
1 answer

Why is maxHeap initialisation syntax different from minHeap through priority_queue?

I was trying to create a minHeap using the regular syntax but found out that maxHeap is initialised as: priority_queue maxHeap; whereas minHeap is initailised as: priority_queue, greater> minHeap; Any idea to why is it…
theebugger
  • 77
  • 9
0
votes
1 answer

heaps and priority queue with pseudocode

I have a question at school that i need to find the kth minimum elements in a min heap. the run time that was required is o(k^2) and i understood how to do that. but if i can get it down to o(k*logk) that i get a bonus. i thought about doing a…
lanz
  • 29
  • 3
0
votes
1 answer

Min-heap insert function doesn't work properly

Can anyone help me with my insert function, please? I don't know why it doesn't insert the data correctly. In the test file, the expected array is: [None, -12, -11, -6, -9, -3, -5, -2, -1, -4] But the function returns: [None, -12, -9, -6, -11, -3,…
0
votes
1 answer

How should I solve my heap select problem?

I'm trying to find the k-th smallest element in a unordered array. I should use two min-heaps h1, h2 where the first one has all the array keys and the second one has h1's root as the only element. The algorithm should iterate k-1 times and on every…
ImZac
  • 11
  • 3
0
votes
1 answer

Min Heap implementation using Arrays in java

Am trying to implement min Heap using arrays. The problem am facing is when i poll an element technically it should return an minimum element from the Heap. But using the code i wrote it doesn't gives the minimum element Output: inserted heap: 1 3…
Swapnil Padaya
  • 687
  • 5
  • 14
0
votes
1 answer

Heapify Down Method in Min Heap

Currently trying to grasp the Min Heap with Repl here: https://repl.it/@Stylebender/Minheap#index.js Min Heap has a capacity of 5. Once I insert the 6th element (50) we swap the positions of the elements at index 0 and index 5 and eject the…
Mike Chan
  • 167
  • 1
  • 11
0
votes
0 answers

comparator for min heap

we know that in order to declare a minimum heap in c++ we can use the STL as follows: priority_queue,greater> minheap; So please can someone explain to me why the greatercomparator need to be passed. I mean to ask that can…
0
votes
2 answers

std::greater on a an std::pair of a double and a class

Does std::greater work when you have a std::pair of int and a class? I am trying to create a priority queue of pairs, ordered by the first element: std::priority_queue, std::vector>,…
0
votes
2 answers

MinHeap with 2 fields... removeMin and remove given key

I have a program to read a list of countries and their GDPs from a file and to insert them into a MinHeap. The heap is a collection(array) of Country objects. A Country object has two fields. A string field called name, which holds the two-letter…
0
votes
0 answers

Trying to implement min-heap using logic of max-heap

I am trying to implement min-heap which includes methods like insert,delete and heap sort.I am using implementation of max-heap and trying to convert it to min-heap.But,i am having some minor issues.It's a very straight-forward method ,but i am…
Meet Yeole
  • 71
  • 2
  • 8
0
votes
2 answers

Implemenation Of A Min Heap Using An Array

I am trying to write a program to represent a min heap using an array. I know that the min heap is similar to a tree data structure where the root node is smaller than it's children. Here is my code:- # include # include int…
Neel
  • 131
  • 3
  • 9
0
votes
1 answer

How to swap the new added number into the correct position in binary heap?

This question of my homework has passed a list where index 1 is the new node and is also the root. Then I have to check if it's children is smaller then itself and swap it with the smaller child. I've written some code but it's not working. def…
Victoriavv
  • 93
  • 1
  • 9
0
votes
1 answer

Find top k tag at any point in time, from a stream of tags

I need to find the top k tags from a stream of tags, at any point in time during the flow. I am able to find the top K tags at the end of the flow, by using HashMap and PriorityQueue of size K. But, i am not sure on how to modify this approach to…
Aman
  • 159
  • 2
  • 15