Consider the following list of elements.
h = [38, 203, 1, 45, 39, 10, 34, 90, 10, 2, 100, 1]
If make this into an array based heap it will look in the following way.
import heapq
heapq.heapify(h)
# now we have a heap that looks like this
# [1, 2, 1, 10, 39, 10, 34, 90, 45, 203, 100, 38]
What is the best way of finding out the position of 39
in this heap?
One way of finding out would be to pop items from the heap until it returns 39, that way we know its position if we keep track of the number of times we popped an item from the heap. However, that's not very efficient as we modify the heap itself.
Is there a better way to solve the problem?