0

I have an array-based binary heap used for graph searching (though the purpose is irrelevant).
(The item at index 0 is the top of the heap.)

Every once in a while, the item at the top of the heap satisfies the criterion that I'm seeking, and thus I pop it and save it for later use.
Currently, I'm just putting these found items in a separate array and returning them to the user.

However, I'm wondering: is there any efficient way for me to keep the item in the front of the original array, by somehow simply readjusting the bounds of the "active" portion of the heap somehow (i.e., shifting the starting boundary of the active portion by one element) and keep going until I'm done?
Doing this naively messes up the structure of the heap.

user541686
  • 205,094
  • 128
  • 528
  • 886
  • Why do you want to keep the item there? You'll need a fixup step anyway (which of the other items becomes the new top? And which one takes the new top's place? etc.) so the only thing you really save is... copying a few items (which should be beyond cheap), and perhaps some array resizing. –  Jul 13 '14 at 23:28
  • @delnan: From a theoretical perspective, it's useless. From a practical perspective, I'm doing this in C++, and avoiding the movement of objects into a separate array helps reduce the requirements on the user's object type. Either way, the reason is unimportant, so don't worry about it. I'm specifically trying to get the answer to this particular question, not something else. If it's impossible then that's a valid answer for me. – user541686 Jul 13 '14 at 23:44

1 Answers1

0

The normal "pop" operation for an array-based binary heap moves the element being removed to the end of the array (so the array size can be decremented to remove it), so if that is what you want, you can keep track of it there. Indeed, using an array-based binary heap where you first insert all the elements into the heap with a priority function such that the desired last element has the highest priority, and then popping all the elements (leaving them at the end of the array) is how a heap-sort works.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • That doesn't work for me though; if I move the item to the end of the array then I can no longer push more items on the heap beyond the size of the array at that particular moment in time. I'm trying to keep it in the beginning of the array so I don't have to restrict the addition of items at the end. – user541686 Jul 14 '14 at 00:09
  • What heapsort does is to swap the item at index 0 with the last item in the heap and then fix it up. This shrinks the heap, but it still starts at 0. So in an array of size N the heap would always be between 0..k for some k and the array items above k are free for you to do as you will, such as to store items satisfying the criteria. You can even reverse index the array by putting a[i] at a[N-i-1] in which case the heap would live between a[N-k-1] and a[N-1] with the top of the heap always at a[N-1] and a[0]..a[k-1] free to store matching items. – mcdowella Jul 14 '14 at 04:40