Leaf indices in a binary heap
Heaps & Priority Queues

Answer & solution
Official answer: B, C
Our answer: (n-1)/2 and (n-3)/2
Givens and goal
A binary minheap contains an odd number n>100 of entries in a 1-indexed array. Which positions are not leaves? A (n+1)/2; B (n−1)/2; C (n−3)/2; D n.
Approach
Use the array child-index formula; heap ordering by key value is irrelevant to the tree shape.
Step-by-step solution
1. For a node at position i, its left child would be at 2i and right child at 2i+1. A node is internal if at least the left child exists, which means 2i≤n.
2. Thus internal positions are 1 through floor(n/2); positions floor(n/2)+1 through n are leaves.
3. Write n=2k+1. Then floor(n/2)=k and the proposed positions become A k+1, B k, C k−1, D 2k+1.
4. Positions k and k−1 lie in the internal range. In particular k has children 2k and 2k+1. Positions k+1 and 2k+1 exceed the internal boundary and have no children.
5. Therefore B and C are not leaves. The condition n>100 ensures k−1 is positive and valid; the minheap property affects stored values, not this index boundary.

![Question 41, GATE 2024: The array [82,101,90,11,111,75,33,131,44,93] is “heapified”. Which first three entries result? A 82,90,101; B 82,11,93; C 131,11,93; D 131,111,90. The original does not specify min-heap versus max-heap; the official answer uses the usual bottom-up max-heap interpretation.](../../../assets/questions/CS-2024-S1-Q41-1.webp)

![Question 12, GATE 2023: Which 1-indexed array is a max-heap? A [23,17,10,6,13,14,1,5,7,12]; B [23,17,14,7,13,10,1,5,6,12]; C [23,17,14,6,13,10,1,5,7,15]; D [23,14,17,1,10,13,16,12,7,5].](../../../assets/questions/CS-2023-Q12-1.webp)







![Question 37, GATE 2015: Insert 35 into the max-heap array (indices 1–9) [40,30,20,10,15,16,17,8,4]. A [40,30,20,10,15,16,17,8,4,35]; B [40,35,20,10,30,16,17,8,4,15]; C [40,30,20,10,35,16,17,8,4,15]; D [40,35,20,10,15,16,17,8,4,30].](../../../assets/questions/CS-2015-S1-Q37-1.webp)




![Question 59, GATE 2009: Which zero-based array is a binary max-heap? A[25, 12, 16, 13, 10, 8, 14]; B[25, 14, 13, 16, 10, 8, 12]; C[25, 14, 16, 13, 10, 8, 12]; D[25, 14, 12, 13, 10, 8, 16].](../../../assets/questions/CS-2009-Q59-1.webp)
![Question 59, GATE 2009: Which zero-based array is a binary max-heap? A[25, 12, 16, 13, 10, 8, 14]; B[25, 14, 13, 16, 10, 8, 12]; C[25, 14, 16, 13, 10, 8, 12]; D[25, 14, 12, 13, 10, 8, 16].](../../../assets/questions/CS-2009-Q59-2.webp)
![Question 60, GATE 2009: Perform two delete-max operations on the valid heap from Q59. Result? A[14, 13, 12, 10, 8]; B[14, 12, 13, 8, 10]; C[14, 13, 8, 12, 10]; D[14, 13, 12, 8, 10].](../../../assets/questions/CS-2009-Q60-1.webp)
![Question 60, GATE 2009: Perform two delete-max operations on the valid heap from Q59. Result? A[14, 13, 12, 10, 8]; B[14, 12, 13, 8, 10]; C[14, 13, 8, 12, 10]; D[14, 13, 12, 8, 10].](../../../assets/questions/CS-2009-Q60-2.webp)
![Question 60, GATE 2009: Perform two delete-max operations on the valid heap from Q59. Result? A[14, 13, 12, 10, 8]; B[14, 12, 13, 8, 10]; C[14, 13, 8, 12, 10]; D[14, 13, 12, 8, 10].](../../../assets/questions/CS-2009-Q60-3.webp)



![Question 76, GATE 2006: A ternary max-heap is stored level-by-level, left-to-right, in a zero-based array. Root is a[0]; children of i are 3i+1,3i+2,3i+3. Insert by appending then moving upward until the parent is at least as large. Which array is a valid heap?
Options:
(A) [1,3,5,6,8,9]
(B) [9,6,3,1,8,5]
(C) [9,3,6,8,5,1]
(D) [9,5,6,8,3,1]](../../../assets/questions/CS-2006-Q76-1.webp)
![Question 76, GATE 2006: A ternary max-heap is stored level-by-level, left-to-right, in a zero-based array. Root is a[0]; children of i are 3i+1,3i+2,3i+3. Insert by appending then moving upward until the parent is at least as large. Which array is a valid heap?
Options:
(A) [1,3,5,6,8,9]
(B) [9,6,3,1,8,5]
(C) [9,3,6,8,5,1]
(D) [9,5,6,8,3,1]](../../../assets/questions/CS-2006-Q76-2.webp)
![Question 77, GATE 2006: A ternary max-heap is stored level-by-level, left-to-right, in a zero-based array. Root is a[0]; children of i are 3i+1,3i+2,3i+3. Insert by appending then moving upward until the parent is at least as large. Start from the valid heap [9,5,6,8,3,1] and insert 7,2,10,4 in that order. What final array results?
Options:
(A) [10,7,9,8,3,1,5,2,6,4]
(B) [10,9,8,7,6,5,4,3,2,1]
(C) [10,9,4,5,7,6,8,2,1,3]
(D) [10,8,6,9,7,2,3,4,1,5]](../../../assets/questions/CS-2006-Q77-1.webp)
![Question 77, GATE 2006: A ternary max-heap is stored level-by-level, left-to-right, in a zero-based array. Root is a[0]; children of i are 3i+1,3i+2,3i+3. Insert by appending then moving upward until the parent is at least as large. Start from the valid heap [9,5,6,8,3,1] and insert 7,2,10,4 in that order. What final array results?
Options:
(A) [10,7,9,8,3,1,5,2,6,4]
(B) [10,9,8,7,6,5,4,3,2,1]
(C) [10,9,4,5,7,6,8,2,1,3]
(D) [10,8,6,9,7,2,3,4,1,5]](../../../assets/questions/CS-2006-Q77-2.webp)
![Question 77, GATE 2006: A ternary max-heap is stored level-by-level, left-to-right, in a zero-based array. Root is a[0]; children of i are 3i+1,3i+2,3i+3. Insert by appending then moving upward until the parent is at least as large. Start from the valid heap [9,5,6,8,3,1] and insert 7,2,10,4 in that order. What final array results?
Options:
(A) [10,7,9,8,3,1,5,2,6,4]
(B) [10,9,8,7,6,5,4,3,2,1]
(C) [10,9,4,5,7,6,8,2,1,3]
(D) [10,8,6,9,7,2,3,4,1,5]](../../../assets/questions/CS-2006-Q77-3.webp)

![Question 37, GATE 2004: Insert 32,15,20,30,12,25,16 one at a time into an empty binary max-heap, using upward restoration after each insertion. Which resulting heap is obtained? The four source diagrams are encoded below as level-order arrays (root first, left-to-right at each level).
Options:
(A) [32,30,25,15,12,20,16]
(B) [32,25,30,12,15,20,16]
(C) [32,30,25,15,12,16,20]
(D) [32,25,30,12,15,16,20]](../../../assets/questions/CS-2004-Q37-1.webp)





![Question 2.11, GATE 1996: For array [89,19,40,17,12,10,2,5,7,11,6,9,70], find the minimum arbitrary pair swaps to obtain a max-heap.](../../../assets/questions/CS-1996-Q36-1.webp)
![Question 14(a), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Identify the data structure.](../../../assets/questions/CS-1991-Q63-1.webp)
![Question 14(a), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Identify the data structure.](../../../assets/questions/CS-1991-Q63-2.webp)
![Question 14(a), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Identify the data structure.](../../../assets/questions/CS-1991-Q63-3.webp)
![Question 14(b), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Show the steps deleting key 5 while retaining the structure.](../../../assets/questions/CS-1991-Q64-1.webp)
![Question 14(b), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Show the steps deleting key 5 while retaining the structure.](../../../assets/questions/CS-1991-Q64-2.webp)
![Question 14(b), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Show the steps deleting key 5 while retaining the structure.](../../../assets/questions/CS-1991-Q64-3.webp)
![Question 14(c), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Give pseudocode to delete an arbitrary node of an n-node min-heap, with worst-case complexity. Distinguish a supplied node index from a supplied key.](../../../assets/questions/CS-1991-Q65-1.webp)
![Question 14(c), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Give pseudocode to delete an arbitrary node of an n-node min-heap, with worst-case complexity. Distinguish a supplied node index from a supplied key.](../../../assets/questions/CS-1991-Q65-2.webp)
![Question 14(c), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Give pseudocode to delete an arbitrary node of an n-node min-heap, with worst-case complexity. Distinguish a supplied node index from a supplied key.](../../../assets/questions/CS-1991-Q65-3.webp)