How does python order values in a heap? -


for example, list of random numbers:

>>> x = [1,24,5,1,5,1,23,6] >>> heapq.heapify(x) [1, 1, 1, 6, 5, 5, 23, 24] 

why arbitrarily 6,5,5 , not 5,5,6 or 5,6,5?

python docs contain following description of heapq:

heaps binary trees every parent node has value less or equal of children. implementation uses arrays heap[k] <= heap[2*k+1] , heap[k] <= heap[2*k+2] k, counting elements zero. sake of comparison, non-existing elements considered infinite. interesting property of heap smallest element root, heap[0].

you can verify example data:

>>> in xrange(len(x)): ...     print '{0} <= {1}'.format(x[i], x[i*2+1:i*2+3]) ... 1 <= [1, 1] 1 <= [6, 5] 1 <= [5, 23] 6 <= [24] 5 <= [] 5 <= [] 23 <= [] 24 <= [] 

for more information binary heaps can check wikipedia.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -