Questions tagged [max-heap]
148 questions
0
votes
2 answers
Python heapq Priority Queue Maxheap
I understand that priority queues using heapq are implemented as a minheap. I need to make a priority queue implemented as a maxheap that sorts elements by an AWS datetime string. I want elements with the most recent datetime to be popped off of the…

the_lost_intern
- 37
- 1
- 5
0
votes
1 answer
Why is maxHeap initialisation syntax different from minHeap through priority_queue?
I was trying to create a minHeap using the regular syntax but found out that
maxHeap is initialised as:
priority_queue maxHeap;
whereas minHeap is initailised as:
priority_queue, greater> minHeap;
Any idea to why is it…

theebugger
- 77
- 9
0
votes
1 answer
Create dictionary from a list of lists by using maxheap in Python
What I know is that creating a maxheap in Python is below:
The syntax of heapq.heappush(heap, value)
li = [5, 7, 9, 1, 3]
maxheap = [-val for val in li]
heapq.heapify(maxheap)
heapq.heappush(maxheap, -10)
print('maxheap = ', maxheap) …

user2761895
- 1,431
- 4
- 22
- 38
0
votes
0 answers
Defining priority for a max-heap priority queue given tie-breaking rules
I have data with the following attributes: name, unit price, quantity, timestamp. e.g.
[Alice, 40.0, 20, 1000000000]
[Ben, 30.0, 35, 1000000001]
[Chris, 50.0, 25, 1000000002]
[Dolly, 20.0, 50, 1000000002]
[Erin, 20.0, 50, 1000000003]
[Fred, 30.0,…

Cogicero
- 1,514
- 2
- 17
- 36
0
votes
0 answers
Search function in a array-based MaxHeap java
I wrote this code, but it gives arrayoutofboundexception at the last index, but it is working for other indexes. please help :) In main, im passing the value of i = 0;
public int recursiveSearch(int key, int i)
{
int left = ((2*i) + 1);
…

Osama Jutt
- 1
- 1
0
votes
2 answers
Given an array of integers, needing run operations on a Max_Heap. Getting error "segmentation fault", Any ideas? (C++)
This is one of my first posts on here so please bear with me.
I am working on an assignment and have looked at various sources to create and solve this problem.
Prompt for problem
I have written the max_heapify, print, build_heap, and delete_root…

Morgan
- 9
- 2
0
votes
0 answers
How to map a String to a PriorityQueue inside a HashMap in Java?
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
HashMap> hmap = new HashMap>();
for (long i = 0; i <…

nerd1996
- 1
- 1
0
votes
2 answers
How to convert an arraylist to a max heap in java using priority queue
I know we can create a max heap using priority queue using Collections.reverseOrder(), but I need to pass the ArrayList in that place also. I tried to create a custom comparator just in case, but it doesnt seem to work. I would like to know the…

Uddhav Navneeth
- 11
- 1
- 2
0
votes
2 answers
How many comparisons a call to removeMin() will make in max heap of 7-ary tree?
Assume that a max heap with 10^6 elements is stored in a complete 7-ary tree. Approximately how many comparisons a call to removeMin() will make?
5000
50
10^6
500
5
My solution: The number of comparisons should be equal to the number of leaf nodes…

Hashir
- 390
- 5
- 20
0
votes
1 answer
maxHeapify and Heapsort not giving correct output
I am a beginner in competitive coding. I am trying to implement maxHeapify and HeapSort functions both of which seems to be not working.Trying a lot to debug but couldn't.
#include
#include
#include
…

Kaustav Ghosh
- 25
- 1
- 3
0
votes
1 answer
Priority Queue in Java (Max Heap declaration)
I wish to make a max heap in Java,but couldn't understand this. can someone explain this to me
Question: Given a string, sort it in decreasing order based on the frequency of characters.
Input="tree"
Output Expected:"eetr"
So I need a max heap using…

AKASH SHIRALE
- 3
- 3
0
votes
1 answer
Max Heap Insertion and Sorting Java
I am trying to construct a Max Heap and as each new value is inserted the value gets shifted up or down into its correct position, I have yet to implement a shift down function so as of right now I'm using a test that should only require the program…

Krystian Rusin
- 27
- 6
0
votes
2 answers
Issues in the ConcurrentHeap implementation
Here is my MaxHeap implementation. Most of the time it works but occasionally it just causes few values out of order. I want it to be threadsafe. Tried debugging many times but couldn't identify the issue. Can anyone please point out the issues in…

Simple Fellow
- 4,315
- 2
- 31
- 44
0
votes
0 answers
i am having problems with my downheap function
I am trying to write a down heap function for my heap but it keeps getting stuck in an infinite loop and I'm not sure why. also, i am not sure how to make it so it doesn't check outside of my last variable which is part of the problem i think
this…

cdude
- 41
- 6
0
votes
1 answer
Max heap in javascript?
I am trying to write the function for creating a max heap in javascript
My current code is
var arr = [5, 9, 6, 7, 1, 3, 8]
var heap = []
for (var i = 0; i < arr.length; i++) {
addtoheap(arr[i]);
}
function addtoheap(term) {
…

Tanmay Bhattacharya
- 551
- 1
- 5
- 16