IntermediateAbout 20 min

    Heap (Priority Queue)

    Read the full article on Substack

    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

    array0

    Empty max-heap. Stored as an array: children of i are at 2i+1 and 2i+2.

    1/19

    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

    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 here

    O(log n)

    Drop items here

    O(n)

    Drop items here
    6 pieces left to place

    Quiz

    20 questions
    Question 1 of 20
    Score: 0

    Where 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.

    Keep going