Questions tagged [binary-heap]

225 questions
0
votes
1 answer

Give an algorithm that finds an arbitrary item X in a binary heap using at most roughly 3N/4 comparisons

This question is in the book of Mark Allen Weiss in exerice 10 in chapter 6 about heaps. Could you please help me to solve this problem? I was able to find a solution that gives a 3N/4 comparison in most cases, but there is cases where my algorithm…
0
votes
1 answer

Order Notation of pop-max in a binary heap

I need to write a pop_max function for a binary heap that removes the max element. The solution given is as below: void pop_max() { assert(!m_heap.empty()); int tmp = (size()+1)/2; for (int i = tmp+1; i < size(); i++) { if (m_heap[tmp]…
Linus
  • 115
  • 2
  • 10
0
votes
1 answer

Checking if Inserting values in order into an initially empty minimum binary heap is correct

Can someone tell me if I have produced the right minimum binary heap after inserting these values in order into an initially empty minimum binary heap? [23, 41, 19, 59, 10, 15, 40]
yowhatsup123
  • 287
  • 1
  • 11
0
votes
0 answers

Why does my search in the heap return None?

I am working on a heap structure. Whenever I added 10 or more nodes to the tree, for some reason the data switches from a node to a None type. def _FindLast(self,root,index,node_count): if root is None or index>= node_count: #…
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
0
votes
1 answer

Can't print binary heap in python

I try to make a binary heap in python but I get trouble printing it. I make sure that the logic in the program is right but when I want to try printing it I get the wrong result. This is what I want for program output: Input: 6 1 2 1 3 1…
ryand
  • 121
  • 10
0
votes
0 answers

Variable created with new inside constructor is setted to 0 after constructor call

Context I have my own implementation of binary heap with decrease-key functionality. I'm using it on my Contraction Hierarchies implementation. When initializing a BinaryHeap, all works normally, but after the construction finishes, all the values…
BMarin
  • 55
  • 6
0
votes
1 answer

How do binary heaps determine their parent if it involves a half (i.e. 1.5)?

I am learning about Binary Heaps and I understand that to get the left child the formula is (2 x index)+1 and to get the right child is (2 x index)+2. That makes sense to me but getting the parent node is where I don't really understand. I know…
Eric Petela
  • 177
  • 1
  • 8
0
votes
1 answer

What are the best known bounds for comparing two binary heaps for equality?

In particular without modifying the input. I've so far been unable to find anything on this, I wonder if it has a solution better than the obvious O(n log n) time.
U2EF1
  • 12,907
  • 3
  • 35
  • 37
0
votes
4 answers

What is the purpose of a priority queue when heaps exist?

I know priority queues tend to use heaps but what is the point of a priority queue when they basically seem the same as heaps? I initially thought all priority queues use hash maps to keep track of all object's locations in the heap, making finding…
user13985180
  • 151
  • 1
  • 1
  • 9
0
votes
1 answer

Priority Queue with heap sort not working correctly

I am attempting to write my own priority queue with a min heap sort but i am not getting the correct output. My input data is 8 elements with different priorities: [0] : 14 [1] : 5 [2] : 10 [3] : 9 [4] : 6 [5] : 3 [6] : 1 [7] : 2 The output should…
WDUK
  • 1,412
  • 1
  • 12
  • 29
0
votes
1 answer

Heapify Down Method in Min Heap

Currently trying to grasp the Min Heap with Repl here: https://repl.it/@Stylebender/Minheap#index.js Min Heap has a capacity of 5. Once I insert the 6th element (50) we swap the positions of the elements at index 0 and index 5 and eject the…
Mike Chan
  • 167
  • 1
  • 11
0
votes
0 answers

Priority Queues(heaps) implementation for parallel processing

Given "n" threads and "m" jobs of varying time taken to complete. If there is a free thread, it immediately takes the next job from the list. If a thread has started processing a job, it doesn’t interrupt or stop until it finishes processing the…
0
votes
0 answers

How to implement increase key operation in a binary heap

I want to implement a max binary heap in c. The header that I written: typedef struct max_heap_t *max_heap_ptr; max_heap_ptr max_heap_create(size_t element_size, size_t key_size, int (*key_compare)(void *a, void *b)); size_t…
Vincenzo
  • 45
  • 3
  • 11
0
votes
1 answer

How to improve this code for finding the k largest elements of an array?

The following code to find the k largest elements of an array is causing a TLE error. How can I optimize it to make it run faster? import heapq for _ in range(int(input())): n,k=map(int,input().split()) …