Questions tagged [min-heap]

181 questions
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
  • 4,064
  • 6
  • 39
  • 69
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
  • 8,005
  • 12
  • 48
  • 90
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
  • 105
  • 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
  • 187
  • 1
  • 2
  • 10
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,929
  • 6
  • 38
  • 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,149
  • 3
  • 12
  • 20
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,619
  • 10
  • 54
  • 92
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
  • 563
  • 2
  • 12
  • 24
-1
votes
1 answer

minimum and maximum number of comparisons needed when deletion in Binary Min heap

You are given a binary min heap of height p Let the minimum and maximum number of comparisons we might have to do when doing a delete min respectively are u and v. Then What will be the value of v-u?
-1
votes
1 answer

Is finding the min/max of BST considered to be O(1) time?

I was building a queue using a BST, but I realized that a min/max heap might be better. However, a BST might work, since if we store a reference to head/tail in the BST, then lookup is very close to O(1)..for example: (5) / \ …
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
-1
votes
1 answer

How the head is getting populated in below code of merging k sorted lists with min heap

The code is below uses a min heap along side linked list to store and manipulate. Here priority queue is being used and head is referenced to tail, but every time even though head is not populating value of last is being populated. The confusion is…
-1
votes
1 answer

Is golang container/heap effectively doing heap sort?

Is golang container/heap effectively doing heap sort on an underlying slice? It doesn't look like underlying array is always sorted. type IntHeap []int // ... other interface implementation func main() { a := []int{} h := IntHeap(a) …
Steve
  • 863
  • 3
  • 9
  • 21