Questions tagged [min-heap]
181 questions
0
votes
1 answer
Given K sorted lists of up to N elements in each list, return a sorted iterator over all the items
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…

vatsa82
- 21
- 3
0
votes
1 answer
How can I change this code from min heap to max heap
I have the implementation for Dijkstra with min heap, and I tried to change min heap to max heap to find the maximum path, but I could not, the output was wrong
so, please could you help me to change this implementation to max heap?
many…

Salwa
- 9
- 1
0
votes
2 answers
What is the best way to implement a PriorityQueue in java?
I am trying to implement my own PriorityQueue class from scratch (not using any existing Java imports or libraries). I know that I want to use a min-heap Data Structure. But I visualize a heap as a form on Binary Search Tree. Should I be using…

devwraps
- 37
- 4
0
votes
1 answer
Algorithm for creating min-heap
I got this question in an exam, but I'm not sure that I understand what it wants me to do. Can you please clarify that if I did the correct thing?
Question
An integer array A is passed to the function makeHeap. if A[0] contains n, then A[1] to…

FutureSci
- 1,750
- 1
- 19
- 27
0
votes
2 answers
Java: how to priority queue HashSet in order of SIZE (smallest first)?
I am trying to implement a priority queue that will order HashSets in order of their size (i.e. smallest HashSets will have the highest priority).
How may I implement this in Java?
Below is my attempt that successfully orders HashSets by their…

Tai
- 101
- 1
- 1
- 10
0
votes
1 answer
Null pointer exception when proving min heap in a binary tree
This code is to find out if the binary tree is min heap or not, I keep getting a NullPointerException at the 3rd if statement in public static boolean isMinHeap, I am unsure as to why I keep getting this exception since I thought the code would be…

Ryota
- 11
- 1
- 7
0
votes
1 answer
min-heap with zero based array C++
Below is my program to build a min-heap using a 0 based array with standard logic from the book. I am using 2*i+1 for left child and 2*i+2 for right child since its a zero based array, still I am getting a wrong output. What am I missing?
#include…

hakuna
- 6,243
- 10
- 52
- 77
0
votes
0 answers
Increase values in MinHeap
I have a MinHeap of Integer For Example.
I would like to ask if there is way to make increase for some value from the middle of the heap and still save the heap Sorted correctly .
Thanks

Roey Mizrahi
- 21
- 6
0
votes
0 answers
minHeap implementation error
I am trying to implement a minHeap of size k.
Although the properties of the min-heap are maintained within the array that is created, the values are not right.
Example=> k=5; input: 5,3,9,6,13,1 ; output is : 1 3 5 6 13 while the correct output…

dpm
- 235
- 2
- 16
0
votes
2 answers
Representation of an array as a Heap
I am trying to represent an array in form of a Minimum-Heap. And I am facing a problem in one of the leaf nodes, where parent is greater than (12 grater than 6) the right child. I am not understanding what is wrong in my coding, please help.
Here…

Johnny Sins
- 71
- 1
- 10
0
votes
2 answers
Building MIN-HEAP in Python
I've got to build a complete MIN-HEAP implementation in Python, without using built-in heap functions.
So I've got definitions of parent, left child and right child, which take in account that python number list elements from 0:
from random import…

kjubus
- 443
- 3
- 8
- 21
0
votes
1 answer
Using a Min-Heap to Sort Words
I am learning to use heaps and as an exercise I am trying to write a program using a heap class I have created to sort words. I have read in words from a file and added them to the heap successfully. I am having some trouble figuring out how I can…

JCCS
- 591
- 8
- 22
0
votes
2 answers
NullPointerException when inserting into binary heap?
I'm trying to insert values into an initially empty binary heap.
This is the relevant code:
public class minHeap
{
int[] array;
int n;
public void minHeap()
{
array = new int [16];
n = 0;
}
public void…

itsmetheperson
- 77
- 1
- 12
0
votes
1 answer
I'm using a min-heap here, but i'm doing it wrong. I'm trying to print out some sheep in breadth order while also being sorted lightest to heaviest
My heap is suppposed to print out in breadth order and sort the sheep from lightest to heaviest (min-heap). My test file should add 15 sheep and remove at least 5. I havent attempted the remove part yet because i'm getting an array index out of…

Coltstick
- 5
- 4
0
votes
1 answer
C: Trying to implement a minHeapSort and then printing it - cannot get for-conditions to work properly
The for's I have trouble with are the one in buildHeap and buildMinHeap. Previously, those didn't trigger at all which is why I know the printing part should more or less work as it printed the nodes properly, if with the unsorted array.
Currently…

enenra
- 111
- 1
- 11