Questions tagged [binary-heap]
225 questions
0
votes
1 answer
What is the time complexity of clearing a heap?
I have googled for lots of websites and they all say "the time complexity of clearing a heap is O(n log n)." The reason is:
Swapping the tailing node the root costs O(1).
Swapping "the new root" to suitable place costs O(level) = O(log n).
So…

Zhicheng Zhang
- 59
- 1
- 7
0
votes
1 answer
Having issues implementing remove within my Binary Heap/Priority Queue implementation
I'm trying to implement a remove method for my binary heap implementation.
class Node {
constructor(priority) {
this.priority = priority;
}
}
class PriorityQueue {
constructor() {
this.heap = [null];
}
remove() {
const toRemove…

KamiWar
- 41
- 5
0
votes
2 answers
want to upgrade a linked list to a priority heap, but I need delete by value
My data structure needs three operations:
insert an element at a random place in the ordering
find and remove smallest element
(rarely) delete an element by via some key returned at insert time
The existing code is a single-linked list and does a…

Swiss Frank
- 1,985
- 15
- 33
0
votes
1 answer
C++ Poll operation in my max binary heap working terribly slow
I'm implementing a max binary heap in C++ and it seems to work fine.
However, when I'm repeating poll operation (delMax method in my code) until the heap if empty it takes way too long, it's longer than building the heap itself, in case of 10^7…

greenbanzai
- 79
- 2
- 7
0
votes
1 answer
How to get a heap object (tree) in python?
I don't understand python's heapq module. You can push and pop, but it doesn't
return an actual heap object ... So I ask myself, does it recreate the tree every single time
I push on it? Why is it not giving a heap object? Do you know an alternative…

user3435407
- 1,019
- 4
- 15
- 31
0
votes
2 answers
checking if array is empty without getting NullPointerException
I'm getting a NullPointerException error when I check if an array is empty. It should return null when the array is empty, but I cannot find why it's not working. Any hints on what I am doing wrong? Here's my entire code:
import…

devil0108
- 37
- 1
- 1
- 6
0
votes
1 answer
C++ Keep track of items in Binary Heap
I am implementing a Max Binary Heap for a problem where I have a lot of servers.
When a request occurs, the server with max current capacity will be allocated to serve and this is the reason why binary heap is used.
What I need to know is that,…

meliksahturker
- 922
- 2
- 11
- 20
0
votes
1 answer
How to use a BinaryHeap when I can't implement Ord?
I have a the following Rust trait:
trait Task {
fn time(&self) -> std::time::Duration;
fn run(self);
}
I need to keep instances of T: Task in some kind of sorted list where I can pop the one with the lowest value for time(). My plan was to…

Anders
- 8,307
- 9
- 56
- 88
0
votes
1 answer
Why is this code correct while it should clearly run into an infinite loop?
I have been having a problem with this code for a while. The placement of recursive call of the function does not seem right.
i tried running the code and yes it does run into a infinite loop.
// I DEFINE HEAP STRUCTURE AS :
struct heap_array
{
…

chaitanya_12789
- 95
- 7
0
votes
1 answer
why is siftdown working in heapsort, but not siftup?
I have a programming assignment as follows:
You will need to convert the array into a heap using only O(n) swaps, as was described in the lectures. Note that you will need to use a min-heap instead of a max-heap in this problem. The first line of…

sudheer naidu
- 164
- 2
- 10
0
votes
2 answers
Having problems while inserting values into a max binary heap
I created a binary heap using C. Here are the functions to initialize the heap and insert the values
int *heapArray; // pointer to array of elements in heap
int capacity = 0; // maximum possible size of min heap, initially set to 0
int heap_size; //…

Bob K
- 69
- 1
- 9
0
votes
1 answer
About the time complexity of Min Max Heap DeleteMin
In the for loop of following code snippet about min-max heap delete-min procedure, why last=(*n)/2? Is that because at the worse case that the x has to be inserted to the grandchild of the grandchild of ..., and for example: tree height 5: min max…

Kindred
- 1,229
- 14
- 41
0
votes
1 answer
Comparing methods to build an entire binayHeap from a list of keys
While reading a post on building a Binary Heap. I got confused in approach1.
The author's approach1(O(n*log n)):
Given a list of keys, build a binary heap by inserting each key one at a time.
Since you are starting with a list of one item, the list…

Anu
- 3,198
- 5
- 28
- 49
0
votes
0 answers
Why are the elements in my heapq not ordered python?
I am using a simple heapq in python with custom elements on which I implemented the lt function.
class Edge:
def __init__(self, cost, u, v):
self.u = u
self.v = v
self.cost = cost
def weight(self):
w =…

Traoré Moussa
- 433
- 1
- 6
- 22
0
votes
2 answers
binary heap instead of a binary search tree
what are couple of scenarios where you will be using a binary heap instead of a binary search tree?
I have a basic understanding of each structure. Id like your input on that if possible.

lam97
- 17
- 9