A binary heap એ એક સંપૂર્ણ દ્વિસંગી વૃક્ષ છે જે એક array માં સંગ્રહિત છે જે heap property જાળવી રાખે છે: min-heap માં, દરેક માતા-પિતા તેના બાળકો કરતા ≤ છે, તેથી ન્યુનતમ હંમેશા મૂળમાં છે. આ priority queue ની પ્રમાણભૂત અમલ છે.
Array layout
text
1 index: 0 1 2 3 4
/ \ array: [1, 3, 5, 8, 4]
3 5 parent(i) = (i-1)//2
/ \ left(i) = 2i+1
8 4 right(i) = 2i+2
Operations
python
heapq
h = []
heapq.heappush(h, )
heapq.heappush(h, )
heapq.heappush(h, )
smallest = h[]
heapq.heappop(h)
