Heap (Priority Queue)
6 min read.
A specialized tree-based data structure which is essentially a complete binary tree that satisfies the heap property. In a max heap, for any given node I, the value of I is greater than or equal to the values of its children.
Think of it this way: Think of an emergency room triage. Patients are not treated in order of arrival (FIFO), but by severity. The most critical case (highest priority) is always handled next, regardless of wait time.
Empty heap
Empty max-heap. Stored as an array: children of i are at 2i+1 and 2i+2.
Check your understanding
Answer from memory before you scroll back up. Pulling an idea out of your head is what makes it stay there.
What does it cost?
Sort each binary-heap operation by its time complexity.
Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.
O(1)
Drop items hereO(log n)
Drop items hereO(n)
Drop items hereQuiz
20 questionsWhere is the maximum element always located in a Max Heap?
Done with this lesson?
Sign in free to keep your progress, streak and quiz scores.