Questions tagged [min-heap]
181 questions
2
votes
2 answers
Approximate Order-Preserving Huffman Code
I am working on an assignment for an Algorithms and Data Structures class. I am having trouble understanding the instructions given. I will do my best to explain the problem.
The input I am given is a positive integer n followed by n positive…

Osuvaldo
- 274
- 3
- 10
2
votes
1 answer
Implementing a Min Heap with an Array: Insert and Remove Min (with duplicates)
I'm trying to implement a Min Heap in Java, but I'm having issues with inserting and removing elements (inserting at the end, removing the root as min). It seems to work for the most part (I use a program to visually display the heap and have been…

arsparfven
- 21
- 1
- 2
1
vote
2 answers
How to get the T value from std::cmp::Reverse::
I have a min-heap of i32 in Rust, and I want to calculate the sum of its elements and store it in i32.
let mut total_sum = 0;
for current in min_heap {
total_sum = current + total_sum;
}
I am getting the following error when compiling:
cannot…

Sero Mirzakhanyan
- 105
- 7
1
vote
1 answer
min priority queue question. dequeue doesn't work. says undefined for priority
this is a min-priority queue in javascript. i can't get dequeue to work. it says in console TypeError: Cannot read properties of undefined (reading 'priority'). i'm able to insert all the nodes into an array. when i console the array after…

AlmostThere
- 15
- 5
1
vote
1 answer
Clarification on min-heap's "Extract Minimum Element" method
I'm reading the book "Cracking the Coding Interview: 6th Edition" and I'm confused on their definition of "min-heaps" and how they say "Extract Minimum Element" works.
The way they say that Extract Minimum Element is implemented is as…

Brian K
- 548
- 1
- 4
- 17
1
vote
2 answers
Is the visited array really needed in Dijkstra's Algorithm using Priority Queue?
vector dijkstra(vector> &vec, int vertices, int edges, int source)
{
vector > adj[vertices];
for (int i=0;i

Shroud
- 35
- 7
1
vote
1 answer
BubbleDown function(min heap) not working
I have generated a minheap to this file but I think something I have missed but I can't identify what are the things I have missed. I have missed something on --private void bubbleDown() { }-- section but I can't find what are the things missed…

Tiyus98
- 15
- 7
1
vote
3 answers
Can i use the normal min heap method for solving "Merge k sorted array"
we have been given k sorted arrays. Lets say k =3
a1={1,4,7} a2={3,5} a3={2,6,7} now we are supposed to merge these 3 arrays in sorted order. Hence the output will be {1,2,3,4,5,6,7,7}.
Now in the tutorial that i am following they have maintained an…

Anonymous User
- 11
- 3
1
vote
0 answers
The soft heap contains at most n/2 power r-3 corrupted items at any given time. How is that so?
In this paper published by Bernard Chazelle, one of the lemmas (Lemma 5.2) states that "the soft heap contains at most n / 2^(r-3) corrupted items at any given time." I am struggling to understand it. It would be helpful if anyone can explain how it…

Somit
- 51
- 3
1
vote
2 answers
checking if a min heap array is valid
I have this function def validate(self) which should check if a given array is a valid min heap. I think it works but because my arrays have None at the beginning like [None, 2, 3, 5] it seems to run into problems and give me the error '<' not…

Mango Lemon
- 23
- 4
1
vote
2 answers
How to make a min heap of tuples sorted by the 2nd element?
So I can make a min heap by doing the following:
// Creates a min heap
priority_queue , greater > pq;
pq.push(5);
pq.push(1);
pq.push(10);
pq.push(30);
pq.push(20);
// One by one extract items from min heap
while (pq.empty()…

doctopus
- 5,349
- 8
- 53
- 105
1
vote
0 answers
Increasing Pointer Array Size - C++
I was working on creating a Min Heap Class (for practice) and it completely works, however, it seems to me that my increase capacity function for the pointer array, which holds the heap array, probably causes a memory leak. (excuse my terrible…

ShaZam
- 21
- 3
1
vote
2 answers
How can I implement downHeap() function in a min-heap?
I'm trying to write downHeap() function for a min-heap where I will check to see if a root node's child is less than the rootnode and if so, I will swap their values. The class Node is already defined for me and has a 'left' and 'right' child ptrs…

MHA
- 49
- 11
1
vote
0 answers
I am unable to figure out why my c++ code for huffman tree is working when I am not assigning the values in the heapify function to the variables
#include
#include
using namespace std;
float AverageCodeLength = 0;
// structure of a node containing the input symbol and its corresponding probability
struct Node {
char symbol;
float probability;
…

nikhil6041
- 55
- 1
- 8
1
vote
4 answers
Finding xth smallest element in unsorted array
I've been trying some coding algorithm exercises and one in particular topic has stood out to me. I've been trying to find out a good answer to this but I've been stuck in analysis paralysis. Let's say I have an array of unsorted integers and I want…

Andrew Huh
- 11
- 1