Heap Trees and Priority Queues

Introduction

A heap is a complete binary tree that satisfies an ordering rule between every parent and its children: in a max-heap each parent is greater than or equal to its children, and in a min-heap each parent is less than or equal to its children. Notice what the heap property does NOT guarantee — there is no left-to-right ordering as in a binary search tree, so searching for an arbitrary value is still O(n). What you do get, cheaply and reliably, is constant-time access to the single most extreme element at the root, plus O(log n) insertion and removal. Because the tree is always complete, a heap needs no pointers at all: it can be packed into an array where the children of index i live at 2i+1 and 2i+2, and the parent at (i-1)/2.

That combination is exactly what a priority queue needs. A priority queue serves elements by priority rather than arrival order, and it underpins job schedulers, event simulation, Huffman coding, and the frontier in Dijkstra's and A* search algorithms — structures you will meet again later in this course and in algorithms courses. The two core maintenance routines are bubble-up (sift-up), used after appending a new element at the end of the array, and bubble-down (sift-down), used after moving the last element to the root when the root is extracted. Building a heap from an unordered array with heapify applies sift-down from the last internal node backwards, which runs in O(n) rather than the O(n log n) you might expect from n separate insertions.

As you watch, keep a pencil moving. Draw the tree and the array side by side, and after every swap check that both views still agree. Pay attention to when the speaker compares against a parent versus against children, how far a sifted element can travel (at most the height of the tree, hence O(log n)), and how the same two routines get reused for insertion, removal, heapify, and heapsort. Also note which videos use 0-based versus 1-based indexing, since the parent and child formulas change slightly between the two conventions.

Video 1: L-3.13: Introduction to Heap Tree with examples | Max Min Heap

Gate Smashers · 7:45 · 1,301,882 views · 5y ago · captions — Watch on YouTube

About this video

An introductory lecture on heap trees that, based on its title and description, defines what a heap is and walks through examples of both max-heaps and min-heaps. Expect coverage of the complete binary tree requirement and the parent-child ordering property that distinguishes a heap from other binary trees, illustrated with worked examples rather than code.

Why this video

This is the natural starting point for the first two subtopics — heap structure and the min-heap versus max-heap distinction — and Gate Smashers is a widely used exam-prep channel known for slow, example-driven board explanations that suit students seeing heaps for the first time.

Video 2: L-3.14: Insertion in Heap Tree | Max-Heap & Min-Heap Creation | Time Complexities

Gate Smashers · 11:33 · 889,227 views · 5y ago · captions — Watch on YouTube

About this video

The follow-up lecture in the same series, focused on inserting elements into a heap and building max-heaps and min-heaps from a sequence of values. The title also promises discussion of time complexities, so expect the per-insertion O(log n) cost and the overall cost of constructing a heap by repeated insertion to be addressed.

Why this video

It targets the insertion and bubble-up subtopic directly and gives you repeated traced examples, which is precisely the skill of stepping through comparisons and swaps that you will be asked to demonstrate.

Video 3: Heaps, heapsort, and priority queues - Inside code

Inside code · 19:01 · 156,915 views · 5y ago · captions — Watch on YouTube

About this video

A longer, more technical treatment that covers heaps together with heapsort and priority queues, with source code linked in the description. Expect the array-based representation, the sift-up and sift-down routines, extraction of the root, and how a heap is used to implement a priority queue and to sort in place.

Why this video

Inside code works at an implementation level, which fills in the removal, heapify, and priority-queue subtopics that the introductory lectures touch on more lightly, and connects the structure to a concrete application you can run and modify.

How These Videos Support the Learning Outcomes

Together these videos build the array-based heap skill set targeted by MO3: the first establishes the complete-binary-tree and ordering properties, the second drills insertion with bubble-up, and the third completes the picture with root removal, sift-down, heapify, and the priority queue interface so you can trace each operation and compute parent and child indices confidently. The structural emphasis also supports MO1, since heaps keep their shape complete by construction and therefore never degrade the way an unbalanced BST does, and the recurring discussion of time complexities feeds MO5, where you will weigh a heap's O(1) access to the extremum and O(log n) updates against the full ordered traversal and search capability that an AVL tree offers.

About These Videos

These videos were chosen from YouTube on October 2, 2026. Each has captions; the player turns them on. Videos on YouTube can be changed or removed — if one no longer plays, tell your instructor. If a player does not appear, use its Watch on YouTube link.