Questions tagged [binary-heap]
225 questions
0
votes
3 answers
Why is balanced BST not used ubiquitously over Binary Heap in mutable structures?
It seems to me that Binary Search Tree can do everything a Binary heap can do, plus additional things.
| | Heap | Bal. BST |
---------------------------------------------
| Lookup min element | O(1) | O(1) …

Anoop
- 5,540
- 7
- 35
- 52
0
votes
1 answer
Heapq with equal priority
I am trying to create a hip of event. Thus, I have define a class Event which is inherited by my different events.
class Event:
def __init__(self, last_instant):
self.last_instant = last_instant # That's the prio criteria
class…

Mathieu
- 5,410
- 6
- 28
- 55
0
votes
0 answers
binary search delete min
I am wondering in the following code,
In the code below i is set to min child, why is this done instead of say just having i = i*2. Both ways 'lower' the level of the binary heap, I am just curious as to why the minimum would be chosen (Assuming…

adudelearning
- 25
- 6
0
votes
1 answer
How does comparing a binary heap's root and root's children to the heap's array length help determine whether you have a min heap?
Bellow is the method I'm confused by. Why do k, right, and left need to be greater than n?
// is subtree of pq[1..n] rooted at k a min heap?
private boolean isMinHeap(int k) {
if (k > n) return true;
int left = 2*k;
int…

Shalbert
- 1,044
- 11
- 17
0
votes
0 answers
How many binary heaps can I create from n elements WITH duplicates?
I know how to count the number of possible heaps with distinct elements such as {1, 2, 3, 4}.
But if I wanted to count the number of heaps looking like this:
{1, 2, 3, 3, 4}
How do I rewrite f(N) = (N−1 C L) * f(L) * f(R) to be able to work with…

elinoria
- 1
0
votes
1 answer
Is the median of a binary max-heap always a leaf node?
If I have a binary max-heap (a nearly complete binary tree with the max-heap property), then will the median always be a leaf node? I've found some examples where this is the case, but haven't found a counter example -- though this isn't enough for…

Paradox
- 4,602
- 12
- 44
- 88
0
votes
1 answer
Custom heap binary tree implementation - Random node deletion
I've been trying to solve this question. The problem statement is bit of a customized heap binary tree if you compare it with ADT definition of heap binary tree. In Heap binary tree you always do deleteMax/deletetMin depending upon the kind of heap…

RBT
- 24,161
- 21
- 159
- 240
0
votes
1 answer
Insert and heapsort to heap built with an array
I am trying to create a binary heap with arrays. I have succesfully made the heap with buildHeap and heapify. My problem is when I try to insert a new element into the array, and when I try to sort it with heapSort.
Here is what I have for the…

zsloan112
- 43
- 2
- 9
0
votes
1 answer
Plain C: Binary Heap Segmentation Faults/Reallocation Errors
I am VERY new to C, but I thought that I'd learn it while I'm learning basic data structures. Anyway, I'm having a problem wrapping my head about how/where errors are coming up in my code.
Basically, I'm getting two different kinds of…

Kellan Martin
- 26
- 1
- 5
0
votes
1 answer
Why deleteMin of heap takes O(logn)?
In my class's lecture slide, I have a heap and it has a method called deleteMin().
What it does, It deletes minimum value in heap. And it says it takes O(logn).
This is I can't understand.
In the heap structure, minimum value is always at the root…

LUKA
- 13
- 3
0
votes
1 answer
Getting different answers by building heap one by insertKey and other by buildHeap
I just tried to build a min heap and i am getting different answers for building a heap
Method 1 insert elements into the array and then call a build minheap method which applies minHeapify on the internal nodes.
Method 2 insert elements directly…

anshul6297
- 25
- 4
0
votes
2 answers
how can i check if a list is minimum binary heap(java)?
Hello I need help for my homework. How can i check if a list is minimum binary heap? Please tell me if i did it right:
public boolean check(int [] arr){
if(arr[0]==null){
return false;
}
for(int i=0 ; i

Itay Shechter
- 35
- 4
0
votes
1 answer
How many leaves are in a given heap and its level
I have a question and it goes like this:
There is a Max heap binary tree.
Let's assume that in the Heap there are (2^2017)-2017 nodes at the bottom-most level.
A)How many levels are there in the heap?
B)What are the number of the Leaves in the…

Jack
- 21
- 1
- 3
0
votes
1 answer
creating a binary heap implementation giving wrong result
I am implementing the binary heap following an online course and I have done the following:
from __future__ import division
class BinaryHeap(object):
def __init__(self, arr=None):
self.heap = []
def insert(self, item):
…

Luca
- 10,458
- 24
- 107
- 234
0
votes
0 answers
Insert and delete in a Heap in c
I have a problem in my program which should prompt the user to enter number and the program will heapify them. The program runs but shows a runtime error after inserting the first number.
I tried to fix it multiple times but in vain.
If anyone could…

Yacine
- 9
- 1
- 6