Questions tagged [max-heap]

148 questions
0
votes
1 answer

Finding the maximum and minimum value inside a "max-min" heap in constant time

So, I was given an "opposite structure" to a "min-max" heap, that is called a "max-min" heap. For every node in even depth, the value of the node is greater or equal to the value of its children. For every node in odd depth, the value of the node…
0
votes
1 answer

java - Max heap property not obeyed by PriorityQueue

I was trying out the problem Top K Frequent Elements and I am not asking for how its implemented but what is going wrong in my implementation. My implementation idea: Declare a HashMap to count the frequency of each number. Declare a max heap with…
ng.newbie
  • 2,807
  • 3
  • 23
  • 57
0
votes
1 answer

How to identify vertices that violates Max Heap property?

I am learning binary heap now and I do not really understand this question about Build Heap that violates the property of Max Heap. Could someone explain the answer below for me please?
vt-0307
  • 63
  • 1
  • 7
0
votes
0 answers

Max Heap - percolateDown Method

I have the workings of a percolateDown method but it doesn't totally work because the operator < is undefined for the argument type(s) E and E. I was wondering if I could get some advice on how to compare the three statements I have that are using…
0
votes
0 answers

Max-Heapify Function

In maxHeapify, why is it necessary to check that the left and right indices are ≤ heap->size after calculating the index of the left and right children? For this question a suggested response was to check the index sizes against heap->size to…
0
votes
1 answer

Fixing a surprising Python UnboundLocalError

Hi so I am trying to write a code that sorts an array from a given node such that it has a max heap property. The code is as follows: def left_child(index): return (index * 2) + 1 def right_child(index): return (index + 1) * 2 def…
Lyxcoder
  • 33
  • 5
0
votes
1 answer

Python MaxHeap Bubble_Down method

I am trying to write a bubble_down method for a MaxHeap class and am having trouble with, what looks like, indexing problems. I think what is mainly confusing me, is that it seems like the indexing in this problem is having to start at 1, which…
0
votes
1 answer

Providing a comparator and collection to PriorityQueue

I have a Map freqMap where each value is the frequency of the key Now I want to build a priorityQueue from this map I was expecting a constructor like PriorityQueue pq = new PriorityQueue(freqMap.keySet(), comparator); But there is…
nicku
  • 279
  • 2
  • 6
0
votes
1 answer

The max value in the max heap isn't the correct value

The program takes as input: (string)’yyyy-mm-dd’, (int)sensor id, (int)covid level. The expected output is: yyyy-mm-dd,covid level. Input: 2022−09−08, 23, 371; 2022−09−08, 2, 3171; 2022−09−08, 12, 43; 2021−03−21, 4, 129 Output: 2022 −09 −08, 3171 I…
robotman46
  • 15
  • 5
0
votes
1 answer

priority queue question. priority is undefined in while loop. how to enqueue element to queue?

doing a priority queue as a min-heap in javascript. console keeps returning priority is undefined in the while loop. what is the problem? / how to enqueue element to queue? //min-heap class PriorityQueue { constructor(){ this.values = []; …
0
votes
0 answers

Time complexity of a "modified" heap

Suppose that I call a heap* as a heap that does not need to be left aligned at the last level, nor does it need to be full. Now, given a max heap* (which has the analogous property of a max heap-that is, parent's value>child's value), to perform an…
0
votes
1 answer

Sorting a Max Heap returns a sorted list in decending order. Should be in ascending order?

My professor gave us a walk through on creating a Max Heap class using an ArrayList. He then tasked us with writing a maxHeapSort method. I've almost been successful in sorting the heap in descending order, but I was under the assumption a sort…
0
votes
1 answer

k closest point to origin

I am trying to solve this problem. https://leetcode.com/problems/k-closest-points-to-origin/ I have used maxheap and following is my code. This works well for points= [[1,3],[-2,2]], k = 1. However for points = [[3,3],[5,-1],[-2,4]], k = 2, this is…
khushbu
  • 567
  • 2
  • 8
  • 24
0
votes
1 answer

Build heap in O(n) : max number of comparisons

I studied an algorithm to build a heap that's O(n): I was wondering what's the max number of comparisons we have to make for a particular value in order to find it's final height. Could a leaf in the left-subtree end up as a leaf in the right…
tonythestark
  • 519
  • 4
  • 15
0
votes
1 answer

Does heapify(int rootIndex) only builds heap for the heap rooted at the input rootIndex?

So the heapify operation is something like this (say we are discussing binary max heaps). maxHeapify(int rootIndex){ int largestIndex = rootIndex; int largest = heap[rootIndex]; if (largest < heap[leftChildIndexOf(rootIndex)]){ …
godwa_rao
  • 187
  • 1
  • 10