binary heap คือต้นไม้ไบนารีสมบูรณ์ที่เก็บในอาร์เรย์ ซึ่งรักษา heap property: ใน min-heap พ่อแม่ทุกคนคือ ≤ ลูกของพวกเขา ดังนั้น ค่าต่ำสุดจึงอยู่ที่รากเสมอ. สิ่งนี้ทำให้เป็น implementation มาตรฐานของ 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)
