binary-heap

Finding last element of a binary heap

Hello everybody, quoting Wikipedia: It is perfectly acceptable to use a traditional binary tree data structure to implement a binary heap. There is an issue with finding the adjacent element on the last level on the binary heap when adding an element which can be resolved algorithmically... Any ideas on how such an ...

Efficient heaps in purely functional languages

As an exercise in Haskell, I'm trying to implement heapsort. The heap is usually implemented as an array in imperative languages, but this would be hugely inefficient in purely functional languages. So I've looked at binary heaps, but everything I found so far describes them from an imperative viewpoint and the algorithms presented are h...

Min Binary Heap issue

Hi all, i need help with this minheap code: #include < vector> using namespace std; class heap { vector <int> v; public: int hSize() { return v.size(); } int rsize() { return hSize() - 1; } int parent(int i) { return ...

Heap data structure

Trying to think of a lower bound to the position of say, the nth largest key in a max-heap. Assuming the heap's laid out in array. The upper bound's min(2^n-2, array size -1) i think, but is it always lower bounded by 0? ...

BubbleDown operation on binary min-heap does not work...

Hi, I'm trying to extract the minimum from a binary heap but it's not working. Here's my BubbleDown code: void heapBubbleDown(Heap * const heap, int idx) { int min; while(RIGHT(idx) < heap->count) { min = LEFT(idx); if(RIGHT(idx) < heap->count) { if(heap->items[LEFT(idx)] > heap->items[RIGHT(idx)])...

Re-adjusting a binary heap after removing the minimum element

After removing the minimum element in a binary heap, i.e. after removing the root, I understand that the heap must be adjusted in order to maintain the heap property. But the preferred method for doing this appears to be to assign the last leaf to the root and sift it down. I'm wondering why we don't take the lesser child of what used ...