DSA for Interviews
Back to Trees, Graphs, and Recursion

Binary Search Trees & Heaps

BST: ordered tree, O(log N) operations. Heap: complete tree for priority queue, O(log N) push/pop. FIND_VIDEO: search 'binary search tree heap priority queue interview' — recommended channel: NeetCode / Abdul Bari. Aim for 10 min or under.

12 minutesVideo LessonPDF notes
🎯 Free Guest Mode: You are learning for free. Sign in to save your completion progress and quiz answers.
Loading progress...

Key moments

  1. Heap Structural Properties — Covers complete binary tree requirements and contrasts max heap vs min heap ordering constraints.
  2. Array Index Calculations — Derives zero-indexed parent and child arithmetic formulas for contiguous tree representation.
  3. MaxHeap Class Initialization — Sets up the internal list structure and helper methods for array navigation.
  4. Insertion and Heapify Up — Appends new values to the array end and bubbles them upward to preserve heap ordering.
  5. Extraction Overview — Explains root extraction by swapping with the tail element and initiating top-down restoration.
  6. Heapify Down Logic — Walks through child comparisons and swapping with the larger child down to leaf nodes.
  7. Bottom-Up Heap Construction — Identifies the last non-leaf index and outlines iterating backward to the root.
  8. O(N) Complexity Derivation — Calculates the summation of node heights across tree levels to prove linear time construction.
  9. Build Heap Implementation — Implements the complete build_heap routine using iterative calls to heapify_down.
PDF notes

Frequently asked questions

Why is build_heap O(N) instead of O(N log N)?

Most nodes reside near the bottom with minimal heights, so the sum of all downward paths converges to O(N).

Can an array containing null gaps represent a binary heap?

No, heaps must be complete binary trees so that array indices remain contiguous without null gaps.

How does 1-based indexing change the arithmetic formulas?

For index i, the parent is i // 2, left child is 2 * i, and right child is 2 * i + 1.

How was this lesson?

Your feedback helps us refine explanations and catch bugs.