Heap properties
easy 16 minA binary heap is a complete binary tree in which every parent is greater than or equal to its children (max-heap) or less than or equal to them (min-heap).
Check yourself
One question at a time. Each answer explains why.
Question 1 of 10
What is the relationship between a parent and its children in a max-heap?
In a 0-based array heap, what is the index of the parent of the node at index 3?
What makes a binary tree complete?
Where is the minimum element of a max-heap?
How do you calculate the left child index of the node at index i (0-based)?
What is the time complexity of reading the root element of a heap?
Is [9, 7, 8, 3, 7, 2] a valid max-heap?
What is the height of a heap holding 100 values?
Which list is NOT a valid min-heap?
You need to check whether a value x is stored somewhere in a heap of n values. How long does that take in the worst case?
Pick an answer to see why.
Not quite. The max-heap order compares each parent only with its own children: parent ≥ child. Siblings are not ordered against each other.
Not quite. parent(i) = (i - 1) // 2 = 2 // 2 = 1. Check: the children of 1 are 3 and 4.
Not quite. Zero-or-two children is a full tree, all leaves at one depth is a perfect tree, and height difference at most one is AVL balance. Complete means no gaps in level order.
Not quite. Every node with a child is at least that child, so the minimum has no children: it is one of the leaves, slots n // 2 to n - 1. It is not necessarily the last one.
Not quite. Left is 2i + 1 and right is 2i + 2. 2i is the 1-based formula, and (i - 1) // 2 is the parent.
Not quite. The root is always at index 0, so peeking is a single read. Removing it costs O(log n) because the heap must be repaired.
Not quite. 9 ≥ 7, 8; 7 ≥ 3, 7; 8 ≥ 2. Ties are allowed, siblings are not compared, and a lone left child in the last level is still complete.
Not quite. floor(log2 100) = 6, since 2^6 = 64 ≤ 100 < 128 = 2^7.
Not quite. In [2, 4, 3, 1, 6], index 3 (value 1) has parent index 1 (value 4), and 4 > 1. The others satisfy parent ≤ child on every edge.
Not quite. The heap order does not say whether x is in the left or right subtree, so you may have to look at every value. Use a BST or a hash set for membership tests.
Flashcards
Say your answer, then reveal it.
Card 1 of 12
What are the two heap properties?
Shape: a complete binary tree (levels full, last level filled left to right). Order: every parent is at least its children (max-heap) or at most its children (min-heap).
Say your answer, then reveal it.
What makes a binary tree complete?
Every level is completely filled except possibly the last, and the last level is filled from left to right with no gaps.
Say your answer, then reveal it.
Where is the maximum of a max-heap? The minimum of a min-heap?
At the root, index 0, so peeking costs O(1).
Say your answer, then reveal it.
Children and parent of index i in a 0-based array heap?
Left 2i + 1, right 2i + 2, parent (i - 1) // 2. In a 1-based heap: 2i, 2i + 1 and i // 2.
Say your answer, then reveal it.
Which slots of an n-element heap are leaves?
Slots n // 2 to n - 1, about half of them. The last parent is n // 2 - 1.
Say your answer, then reveal it.
Height of a heap with n nodes?
floor(log2 n), because 2^h ≤ n < 2^(h+1). A million values give height 19.
Say your answer, then reveal it.
Where is the minimum of a max-heap?
Somewhere among the leaves, slots n // 2 to n - 1. Finding it takes O(n).
Say your answer, then reveal it.
Is a heap sorted?
No. Only each parent is ordered against its own children; siblings and cousins can be in any order. A sorted array is a heap, but not the other way round.
Say your answer, then reveal it.
Costs of peek, insert, extract, build and search?
Peek O(1), insert O(log n), extract root O(log n), build from a list O(n), search O(n).
Say your answer, then reveal it.
How do you validate an array heap?
Check every edge: for each i from 1 to n - 1, compare a[(i - 1) // 2] with a[i]. O(n) time, O(1) space.
Say your answer, then reveal it.
Heap vs BST in one line?
A heap orders parent against child (fast extreme, slow search); a BST orders left against right (fast search and sorted order).
Say your answer, then reveal it.
What does the HEAP mnemonic stand for?
Highest (or lowest) Element Always at the Peak: the root.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.