Questions tagged [max-heap]
148 questions
2
votes
1 answer
How do you implement a max-heap?
So I've read up on heaps, and I understand the basic concept,
but not how to actually implement it.
Basically, my issue is
How you know where to put a new node?
How you know which node to replace the root with when you delete?

Joe legrae
- 137
- 2
- 4
2
votes
4 answers
binary heap - how and when to use max-heapify
i'm reading about the heap data structure, and i can't figure out when to use max heapify function and why.
I wrote a insert function that will always keep the heap a max-heap and i can't see when will max-heapify ever be used.
Can you please…

Dan Dinu
- 32,492
- 24
- 78
- 114
2
votes
1 answer
Constructing Min/Max Binary Heap
Given an inorder-traversal list, what's the best way to create a Binary Min/Max Heap?
I'm trying to confine with the following constructs:
No array to be used in the binary-heap. Implementation is node-based.
BinaryNode { value, parent, l_child,…

gvaish
- 9,374
- 3
- 38
- 43
2
votes
3 answers
Java Priority Queue of Objects :: A way to check an object's member?
Suppose I have an array of integers:
[ 1,2,3,4,5,6,1,2,3,1,2... ]
I want to know the K most frequent elements. The phrase "K most frequent" immediately makes me think of a Max Heap data structure, so I've decided to create a custom object to both…

Pete
- 1,511
- 2
- 26
- 49
2
votes
2 answers
Is it possible to build a max heap from two heap without rebuilding the heap?
I recently took my Computer Science exam and there was a question like that.
There are two max-heaps (array implemented). You need to come up with an algorithm that
merges these two max-heaps and creates a new max-heap (array implemented)
Solution…

Lapaaci
- 119
- 7
2
votes
2 answers
Why is deletion in binary heap a O(logN) operation, when the underlying structure is an array
I understand that deletion in a Min/Max heap occurs always at the root, and when it happens, the deleted node is replaced by the last node in the Binary Heap, then the node is heapify downward to find its correct position, making this on average a…

Jimmy
- 377
- 4
- 5
- 16
2
votes
2 answers
PriorityQueue with lambda expression
Im trying to understand how lambda functions work with heaps in Java. the function below is to create a max heap
PriorityQueue pq = new PriorityQueue<>((x, y) -> y - x);
Can someone tell me what (x, y) -> y - x) does?
Im told that "The…

Link Ali
- 21
- 2
2
votes
2 answers
Using heap sort, append an array elements
I have given an array int A[] = {12,10,9,2,11,8,14,3,5};
In this array, 1st 4 elements(from index 0 to index 3) follow max heap condition. But last 5 elements(index 4 to index 8) don't follow max heap condition. So, I have to write a code so that…

Shehab
- 59
- 5
2
votes
1 answer
I am not able to get the desired output for max heap array, can somebody tell me the changes to be made
the python code is:
def max_heapify(i, arr, n):
l = 2*i
r = 2*i+1
largest = i
if (2*i <= n-1 and arr[l] > arr[i]):
largest = l
if (2*i+1 <= n-1 and arr[r] > arr[largest]):
largest = r
if largest != i:
…

anadi
- 63
- 1
- 6
2
votes
1 answer
Min/Max Heap implementation in Python
This is my implementation of a MinHeap and MaxHeap in python. This uses a comparator to reverse the sequence of storage in the MaxHeap
import heapq
class MinHeap:
def __init__(self):
self.heap = []
def push(self, item):
…

Melissa Stewart
- 3,483
- 11
- 49
- 88
2
votes
1 answer
MinHeap/MaxHeap implementation in Python
I scoured the web to come up with this implementation of MinHeap and Maxheap in Python.
import heapq
class MinHeap:
def __init__(self):
self.heap = []
def push(self, item):
heapq.heappush(self.heap, item)
def…

Melissa Stewart
- 3,483
- 11
- 49
- 88
2
votes
1 answer
Improving Key Search Time Complexity in a Priority Queue Heap
I have a custom Task class which contains a priority value as well as some additional fields, shown below:
class Task{
int ID;
int Priority;
int Time;
public Task(int i, int p, int t){
this.ID = i;
this.Priority =…

Chase
- 61
- 6
2
votes
1 answer
Max and Min heap with the same elements
Consider the following example. I am adding random numbers to min heap and at the same time I am adding the same numbers in the same order to the max heap. So at the end those 2 heaps will have the same numbers with difference one being min heap and…

kvway
- 494
- 4
- 16
2
votes
2 answers
Array representation of max heap
I am trying to create a max heap , the logic is simple , if parent is smaller then one of its childre, swap them. I tried implementing it using
void maxHeapify( int i , int *a , int n ){
int largest = i;
int left = ( i * 2 ) + 1;
…

J.dd
- 145
- 15
2
votes
1 answer
Maxheap vs priorityqueue confusion
Suppose we want to sort a hashmap based on the value. We implement a priorityQueue with comparator to do this. As a result, the resulting pq is sorting from the largest to the smallest from index 0 to end.
Here is the…

kkk
- 35
- 7