Questions tagged [min-heap]

144 questions
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
  • 103
  • 10
0
votes
1 answer

Heapify is giving me an incorrect output for a min-heap

I'm trying to build a min-heap, but I am failing to get the correct results. I am not sure what might be wrong. input = 209 97 298 54 110 27 250 455 139 181 446 206 478 90 88 output = 27 54 97 88 110 206 90 209 139 181 446 298 478 250 455 As you can…
Luis Lavieri
  • 3,689
  • 5
  • 32
  • 60
0
votes
2 answers

Heap sort, Understanding the basics

As a disclaimer I am new to this site and therefore, do not know very well how to ask questions. Please don't be too harsh because I really am trying to just understand how some of these concepts work. If i am missing understanding near the…
0
votes
1 answer

c++ priority_queue initialization. Why can we ignore const Compare&

class Star { public: // The distance between this star to the Earth. double distance() const { return sqrt(x_ * x_ + y_ * y_ + z_ * z_); } bool operator<(const Star& s) const { return distance() < s.distance(); } int ID_; double x_, y_,…
BufBills
  • 6,627
  • 8
  • 40
  • 67
0
votes
1 answer

Is it possible to use a min heap as a max heap?

I've implemented a minHeap class so I am curious if, without modifying the code, it would be possible to use the minHeap class as a max heap?
0
votes
1 answer

Creation of Min heap from an given array

Trace the creation of the heap indicated from the following lists showing each stage of the process a. {5, 13, 2, 25, 7, 17, 20, 8, 4} min heap I hope I'm doing this right. Before I proceed to next problems I wanted to make sure if this is…
user3478869
  • 107
  • 2
  • 6
  • 12
0
votes
2 answers

Standard Collection for MINIMUM or MAXIMUM HEAP

I want to know that what class from java's standard collection can be made a parent class of Min-Heap or Max-Heap? I develop the class Heap which can convert the heap to min or max depending upon strategy and used methods like add, toString, toArray…
user2801336
  • 167
  • 1
  • 2
  • 9
0
votes
2 answers

Better-than-O(n^2) algorithm for sorting a 2d array in Objective-C

I was asked this question in an interview once and I grew curious as to what the best answer is. I primarily got stuck with providing a solution that traces a 2-d array in a time that's better than O(n^2). Here's the question: /* Here's a helper…
Ravi
  • 7,029
  • 6
  • 36
  • 48
0
votes
1 answer

How do I subclass ArrayList, and require that must extend Comparable

I think what I'm trying to do is clear, but I'm no Generics expert. import java.util.ArrayList; public class MinHeap extends ArrayList { /* A simple wrapper around a List to make it a binary min-heap. */ public…
jameh
  • 1,009
  • 3
  • 11
  • 19
0
votes
1 answer

Is this a valid MaxHeap structure?

I am trying to convert a minHeap class to a maxHeap class. I was given the code for the minHeap class, one method of which was add. It looks like this: while (index > 1 && getParent(index).compareTo(newElement) > 0) The first node is automatically…
Andrew Martin
  • 5,249
  • 9
  • 46
  • 89
0
votes
1 answer

Minheap when values are the same C++

So I made a min heap of Binary trees. To test it I create a bunch of nodes with 2 different values. Say struct node { int frequency; int data; } Say frequency is what the heap primarily sorts by, but if the nodes frequency is the same, it is…
0
votes
1 answer

Popping/deleting nodes from a Huffman Tree Minheap

I'm having trouble popping correctly from a Huffman Tree. Right now I am creating a Huffman Tree based off a minheap and I want to do the following: If we assume A and B to be two different subtrees, I would say that A would be popped off first if…
user200081
  • 553
  • 2
  • 10
  • 24
-1
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,…
-1
votes
1 answer

Problem in implementing build-heap method for min-heap

I am trying to make min-heap algorithm and i am able to get insert,delete,pop ,trickleUp and trickleDown methods to work ,but i am having problems with the constructor which is to make use of build-heap algorithm. The method is simple as it just…
Meet Yeole
  • 71
  • 1
  • 8
1 2 3
9
10