Heaps, Priority Queues and Data Structure Operations Short Notes for Gate Cs
Heaps, Priority Queues and Data Structure Operations short notes for GATE CS: 10 study cards covering concepts, formulas, shortcuts and exam traps, plus solve
Heaps Priority Queues and Data Structure Operations short notes
Heap Structure Toolkit — Final Summary
Final Revision — Heap Structure, Height, Leaves
Height
h=⌊log2n⌋
Index formulas (1-based)
Parent of i: ⌊i/2⌋
Left child of i: 2i
Right child of i: 2i+1
Node classification
Internal nodes: indices $1$ to ⌊n/2⌋
Leaf nodes: indices ⌊n/2⌋+1 to n
Number of leaves: ⌈n/2⌉
Extreme element locations
Min-heap: minimum at root (index $1$), maximum at some leaf
Max-heap: maximum at root (index $1$), minimum at some leaf
Possible positions for the opposite extreme: ⌈n/2⌉
Quick checks
Height counts edges, not nodes.
Always confirm 0-based vs 1-based indexing.
For odd n: ⌊n/2⌋=(n−1)/2.
Formula Matrix
Formula Matrix
A[i]≥A[2i]∧A[i]≥A[2i+1]Cond: 1≤i≤⌊N/2⌋
i←⌊N/2⌋ down to $1$
Cond: 1-based indexing
O(N)Cond: Bottom-up Floyd build-heap
∑hi<NCond: hi is height of node i
T(n)=(Ln−1)T(L)T(R)Cond: L,R are subtree sizes
⌈N/2h+1⌉Cond: Nodes at height h
If You See This, Do This
If You See This, Do This
"forms a valid max-heap"Check A[i]≥ children for i≤⌊N/2⌋
"heapified array"Sift-down from ⌊N/2⌋ to $1$
"exact number of swaps"Count physical data movements only
"partial heapify"Sift-down only ⌊N/2⌋…k
"distinct max-heaps"Apply (Ln−1)T(L)T(R)
"time complexity"O(N), reject O(NlogN)
7 more cards in this chapter
Try a question
Answer it here to see how it works. Nothing is recorded until you sign in.
Question 1
Level 1: Warm-up
In a binary min-heap containing $15$ distinct elements stored in a $1$-based array, the second smallest element must be located at one of the child nodes of the root. The number of possible indices where the second smallest element can be stored is _____________.
Question 2
Level 1: Warm-up
A binary min-heap contains $20$ distinct elements. The maximum element can be stored at most in _____________ different positions in the underlying array.
Question 3
Level 1: Warm-up
A binary min-heap stores $128$ distinct elements. The height of this heap is _____________. (Answer in integer)
Question 4
Level 1: Warm-up
In a binary max-heap containing $15$ distinct elements stored in a 1-based array, the maximum possible index of the second largest element is _____________.
Question 5
Level 1: Warm-up
A binary min-heap contains 15 distinct elements. An optimal algorithm is used to find the maximum element in the heap by inspecting the minimum number of nodes. What is the maximum number of nodes this algorithm must inspect in the worst case?
Question 6
Level 1: Warm-up
In a min-heap implemented as a 1-based array of size 12, what is the highest possible index at which the maximum element can be stored?
Question 7
Level 1: Warm-up
Which of the following statements about binary heap operations is TRUE?
Question 8
Level 1: Warm-up
In a binary min-heap containing $63$ elements, a new element is inserted. In the worst case, the maximum number of swap operations that can be performed to restore the heap property during this single insertion is _____________.
Question 9
Level 1: Warm-up
In a binary max-heap containing $31$ distinct elements stored in a $1$-based array, the minimum element must be located at a leaf node due to the heap property. What is the minimum possible array index at which this minimum element can be stored?
Question 10
Level 1: Warm-up
In a binary heap stored in an array with 0-based indexing, the left child of the element at index $4$ is at index _____________.
Free preview ends here
Login to view the complete short notes
Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.
Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.
Built around you, not around a syllabus PDF
Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.
Revision that hits your weak spots
We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.
Questions calibrated to the real exam
Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.
Notes written for recall, not for volume
Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.
One place for everything
Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.
Honest progress
No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.
Unlock the whole course
Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.
Heaps, Priority Queues and Data Structure Operations Short Notes for GATE CS
Heaps, Priority Queues and Data Structure Operations short notes for GATE CS: 10 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.
Heap Structure Toolkit — Final Summary
Final Revision — Heap Structure, Height, Leaves
Height
h=⌊log2n⌋
Index formulas (1-based)
Parent of i: ⌊i/2⌋
Left child of i: 2i
Right child of i: 2i+1
Node classification
Internal nodes: indices $1$ to ⌊n/2⌋
Leaf nodes: indices ⌊n/2⌋+1 to n
Number of leaves: ⌈n/2⌉
Extreme element locations
Min-heap: minimum at root (index $1$), maximum at some leaf
Max-heap: maximum at root (index $1$), minimum at some leaf
Possible positions for the opposite extreme: ⌈n/2⌉
Quick checks
Height counts edges, not nodes.
Always confirm 0-based vs 1-based indexing.
For odd n: ⌊n/2⌋=(n−1)/2.
Formula Matrix
Formula Matrix
A[i]≥A[2i]∧A[i]≥A[2i+1]Cond: 1≤i≤⌊N/2⌋
i←⌊N/2⌋ down to $1$
Cond: 1-based indexing
O(N)Cond: Bottom-up Floyd build-heap
∑hi<NCond: hi is height of node i
T(n)=(Ln−1)T(L)T(R)Cond: L,R are subtree sizes
⌈N/2h+1⌉Cond: Nodes at height h
If You See This, Do This
If You See This, Do This
"forms a valid max-heap"Check A[i]≥ children for i≤⌊N/2⌋
"heapified array"Sift-down from ⌊N/2⌋ to $1$
"exact number of swaps"Count physical data movements only
"partial heapify"Sift-down only ⌊N/2⌋…k
"distinct max-heaps"Apply (Ln−1)T(L)T(R)
"time complexity"O(N), reject O(NlogN)
Traps and Invariants
Traps and Invariants
Build-heap is O(NlogN) via N insertions.
Check: Nodes at bottom have height 0, sum is geometric O(N).
Validate by checking only root or assuming sorted array.
Check: Audit every internal node to ⌊N/2⌋; siblings need not be sorted.
Stop sift-down after first swap or swap with smaller child.
Check: Trace continuously to leaf; swap with larger child for max-heap.
Heaps, Priority Queues and Data Structure Operations: Solved Questions with Step-by-Step Explanations (10 Problems)
Question 1 · Programming and Data StructuresNAT
In a binary min-heap containing $15$ distinct elements stored in a $1$-based array, the second smallest element must be located at one of the child nodes of the root. The number of possible indices where the second smallest element can be stored is _____________.
Correct Answer:
2.00
Step-by-Step Solution
Key idea: This is a direct concept recall question on secondary extrema location.
Step 1: In a min-heap, the smallest element is always at the root (index 1).
Step 2: The second smallest element must be greater than the root but smaller than or equal to all other elements.
Step 3: Because of the min-heap property (parent ≤ child), the second smallest cannot be a grandchild or deeper descendant, as its parent would be smaller than it, contradicting that it is the second smallest.
Step 4: Therefore, the second smallest must be a direct child of the root.
Step 5: In a 1-based array, the children of the root (index 1) are at indices 2 and 3.
Step 6: Thus, there are exactly 2 possible indices.
Answer: 2.00
Question 2 · Programming and Data StructuresNAT
A binary min-heap contains $20$ distinct elements. The maximum element can be stored at most in _____________ different positions in the underlying array.
Correct Answer:
10
Step-by-Step Solution
Key idea: In a min-heap, the maximum element must be at a leaf node. The number of leaves in a complete binary tree with n nodes is ⌈n/2⌉.
Step 1: Identify that in a min-heap, the maximum cannot be at an internal node (it would violate the heap property since children must be ≥ parent).
Step 2: Therefore, the maximum must be at a leaf.
Step 3: Count the leaves: ⌈n/2⌉=⌈20/2⌉=10.
Answer: 10
Common trap: Using ⌊n/2⌋=10 gives the same answer for even n, but for odd n it would be wrong. Always use ⌈n/2⌉ for leaf count.
Question 3 · Programming and Data StructuresNAT
A binary min-heap stores $128$ distinct elements. The height of this heap is _____________. (Answer in integer)
Correct Answer:
7
Step-by-Step Solution
Key idea: This is a direct formula application question. The height of a complete binary tree with n nodes is h=⌊log2n⌋.
Step 1: Identify n=128.
Step 2: Apply the height formula: h=⌊log2128⌋.
Step 3: Since 128=27, we have log2128=7.
Step 4: Therefore, h=⌊7⌋=7.
Answer: 7
Common trap: Students often count levels instead of edges. A tree with height 7 has 8 levels (level 0 through level 7), but height counts edges, not nodes.
Question 4 · Programming and Data StructuresNAT
In a binary max-heap containing $15$ distinct elements stored in a 1-based array, the maximum possible index of the second largest element is _____________.
Correct Answer:
3.00
Step-by-Step Solution
Key idea: This tests the structural constraint on where extreme elements can hide in a heap.
Step 1: In a max-heap, the largest element must be at the root (index $1$).
Step 2: The second largest element must be greater than all other elements except the root. Therefore, its parent must be the root. If its parent were any other node, that parent would be larger than the second largest element, which is a contradiction.
Step 3: The children of the root (index $1$) are at indices $2(1) = 2$ and $2(1) + 1 = 3$.
Step 4: The second largest element must be at index $2$ or $3$. The maximum possible index is $3$.
Answer: 3
Common trap: Assuming the second largest element can be anywhere in the tree, or confusing it with the minimum element (which must be at a leaf).
Question 5 · Programming and Data StructuresMCQ
A binary min-heap contains 15 distinct elements. An optimal algorithm is used to find the maximum element in the heap by inspecting the minimum number of nodes. What is the maximum number of nodes this algorithm must inspect in the worst case?
A.
7
B.
15
C.
8
D.
16
Correct Answer:
C
Step-by-Step Solution
Key idea: In a min-heap, the maximum element must be located at one of the leaf nodes because every parent is smaller than its children.
Step 1: The algorithm must inspect all leaf nodes to guarantee finding the maximum.
Step 2: For an array of size N=15 with 1-based indexing, the internal nodes are from index 1 to ⌊15/2⌋=7.
Step 3: The leaf nodes occupy the remaining indices, from 8 to 15.
Step 4: The number of leaf nodes is 15−7=8.
Step 5: In the worst case, the algorithm must inspect all 8 leaf nodes.
Answer: 8
Question 6 · Programming and Data StructuresMCQ
In a min-heap implemented as a 1-based array of size 12, what is the highest possible index at which the maximum element can be stored?
A.
6
B.
7
C.
11
D.
12
Correct Answer:
C
Step-by-Step Solution
Key idea: In a min-heap, the maximum element must be located at one of the leaf nodes because every parent is strictly smaller than its children.
Step 1: Identify the total number of elements N=12.
Step 2: The internal nodes of a 1-based array of size N occupy indices from 1 to ⌊N/2⌋.
Step 3: Calculate the last internal node: ⌊12/2⌋=6.
Step 4: The leaf nodes occupy the remaining indices, from 7 to 12.
Step 5: Since the maximum element must be a leaf, it can be stored at any index from 7 to 12. The highest possible index is 12.
Answer: 12
Question 7 · Programming and Data StructuresMCQ
Which of the following statements about binary heap operations is TRUE?
A.
Insert into a min-heap of n elements takes O(1) amortized time in the worst case
B.
Finding the minimum element in a max-heap of n elements takes O(logn) time
C.
Extract-Max from a max-heap of n elements takes O(logn) time in the worst case
D.
Decrease-key in a max-heap takes O(1) time if the index of the element is known
Correct Answer:
C
Step-by-Step Solution
Key idea: This is a statement truth question testing knowledge of heap operation complexities. Evaluate each statement by recalling the mechanics of each operation.
Step 1: Analyze option A — Insert into a min-heap.
Insertion places the new element at the end of the array
Then it percolates up to find its correct position
Percolate up takes O(logn) time in the worst case (traversing the height of the tree)
The statement claims O(1) amortized, which is false
Option A is FALSE
Step 2: Analyze option B — Finding the minimum in a max-heap.
In a max-heap, the maximum is at the root, but the minimum can be at any leaf
There are ⌈n/2⌉ leaves, and we must check all of them
This takes Θ(n) time, not O(logn)
Option B is FALSE
Step 3: Analyze option C — Extract-Max from a max-heap.
Reading the root (maximum) takes O(1) time
However, extraction also requires replacing the root with the last element and percolating down to restore the heap property
Percolate down takes O(logn) time in the worst case
Therefore, Extract-Max takes O(logn) time
Option C is TRUE
Step 4: Analyze option D — Decrease-key in a max-heap.
Decreasing a key might make it smaller than its children
This requires percolating down to restore the heap property
Percolate down takes O(logn) time, not O(1)
Option D is FALSE
Answer: C
Question 8 · Programming and Data StructuresNAT
In a binary min-heap containing $63$ elements, a new element is inserted. In the worst case, the maximum number of swap operations that can be performed to restore the heap property during this single insertion is _____________.
Correct Answer:
6.00
Step-by-Step Solution
Key idea: This is a bounding question on insertion mechanics. The number of swaps equals the height of the tree after insertion.
Step 1: Before insertion, the heap has 63 elements. The levels 0 through 5 are completely full (1+2+4+8+16+32=63).
Step 2: The new element is inserted at index 64, which is at level 6 (the deepest level).
Step 3: In the worst case, this new element percolates all the way up to the root (level 0).
Step 4: The path from index 64 to the root is: 64→32→16→8→4→2→1.
Step 5: The number of edges (and thus swaps) on this path is 6.
Answer: 6.00
Question 9 · Programming and Data StructuresNAT
In a binary max-heap containing $31$ distinct elements stored in a $1$-based array, the minimum element must be located at a leaf node due to the heap property. What is the minimum possible array index at which this minimum element can be stored?
Correct Answer:
16.00
Step-by-Step Solution
Key idea: This is an elimination question testing the structural constraints of heap extrema. The trap is ignoring the constraint that the minimum must be a leaf and guessing a small index like 2.
Step 1: Identify the location constraint for the minimum element.
In a max-heap, every parent is greater than or equal to its children. Therefore, an internal node can never be the minimum element because its children must be strictly smaller (since elements are distinct). The minimum element must be located at a leaf node.
Step 2: Determine the range of leaf indices.
For a heap of size N in $1$-based indexing, the internal nodes occupy indices $1$ to ⌊N/2⌋. The leaf nodes occupy indices ⌊N/2⌋+1 to N.
For N=31:
Last internal node index: ⌊31/2⌋=15.
First leaf node index: 15+1=16.
Last leaf node index: $31$.
Step 3: Find the minimum possible index.
The leaf nodes are at indices 16,17,…,31. The minimum possible index among these is $16$.
Answer: 16.00.
Question 10 · Programming and Data StructuresNAT
In a binary heap stored in an array with 0-based indexing, the left child of the element at index $4$ is at index _____________.
Correct Answer:
9
Step-by-Step Solution
Key idea: This tests the left child formula for 0-based indexing.
Step 1: Identify the indexing scheme: 0-based.
Step 2: Recall the left child formula for 0-based indexing: left(i)=2i+1.
Step 3: Substitute i=4: left(4)=2(4)+1=8+1=9.
Answer: 9
Common trap: Using the 1-based formula 2i=8, or using the right child formula 2i+2=10.