Heap Tree · Data Structures
32 questions · asked in 20 of 27 years (2000–2026)
MCQ 27 · MSQ 1 · NAT 41-mark 19 · 2-mark 13
2000Questions per year2026
Questions 21–25 of 32
We have a binary heap on n elements and wish to insert n more elements (not necessarily one after another) into this heap. The total time required for this is
Answer: (B)
The heap already holds elements, and more are added, so the final heap has elements. The words "not necessarily one after another" allow us to add them as one batch instead of one insert at a time.
Batch method: copy the new elements to the end of the array (one write each, so work). Then run Build-Heap on the whole array of elements.
Build-Heap sifts down every internal node. A node at height costs , and about nodes have height . The total is , because the sum equals 2.
So the upper bound is . Every new element must be handled at least once, so the lower bound is . Together the time is .
Doing separate inserts at each gives , but that is only the cost of the slower method, not the best possible time.
Answer: (B)
Batch method: copy the new elements to the end of the array (one write each, so work). Then run Build-Heap on the whole array of elements.
Build-Heap sifts down every internal node. A node at height costs , and about nodes have height . The total is , because the sum equals 2.
So the upper bound is . Every new element must be handled at least once, so the lower bound is . Together the time is .
Doing separate inserts at each gives , but that is only the cost of the slower method, not the best possible time.
Answer: (B)
Consider the process of inserting an element into a Max Heap, where the Max Heap is represented by an array. Suppose we perform a binary search on the path from the new leaf to the root to find the position for the newly inserted element, the number of comparisons performed is:
Answer: (B)
In a max heap, the keys on the path from the new leaf up to the root are in sorted order (they grow towards the root). A heap is a complete binary tree, so this path has about nodes.
Because the heap is stored in an array, the parent of index is at , so any node of the path can be found directly from its index. That lets us do binary search on the path.
Binary search on sorted items takes comparisons. Here , so the number of comparisons is .
Answer: (B)
Because the heap is stored in an array, the parent of index is at , so any node of the path can be found directly from its index. That lets us do binary search on the path.
Binary search on sorted items takes comparisons. Here , so the number of comparisons is .
Answer: (B)
Which of the following sequences of array elements forms a heap?
Answer: (C)
In a max heap stored in an array with indices starting at 1, the node at index has children at and . Each parent must be at least as large as its children. With 10 elements, only indices 1 to 5 have children, so check those.
(A) a[4] = 6 has child a[8] = 12, and 12 is larger. Not a heap.
(B) a[4] = 6 has child a[9] = 7, and 7 is larger. Not a heap.
(D) a[4] = 7 has child a[8] = 12, and 12 is larger. Not a heap.
(C) 23 ≥ 17, 14; 17 ≥ 7, 13; 14 ≥ 10, 1; 7 ≥ 5, 6; 13 ≥ 12. Every parent is at least as large as its children, so it is a heap.
Answer: (C)
(A) a[4] = 6 has child a[8] = 12, and 12 is larger. Not a heap.
(B) a[4] = 6 has child a[9] = 7, and 7 is larger. Not a heap.
(D) a[4] = 7 has child a[8] = 12, and 12 is larger. Not a heap.
(C) 23 ≥ 17, 14; 17 ≥ 7, 13; 14 ≥ 10, 1; 7 ≥ 5, 6; 13 ≥ 12. Every parent is at least as large as its children, so it is a heap.
Answer: (C)
A 3-ary max heap is like a binary max heap, but instead of 2 children, nodes have 3 children. A 3-ary heap can be represented by an array as follows: The root is stored in the first location, a[0], nodes in the next level, from left to right, is stored from a[1] to a[3]. The nodes from the second level of the tree from left to right are stored from a[4] location onward. An item x can be inserted into a 3-ary heap containing n items by placing x in the location a[n] and pushing it up the tree to satisfy the heap property.
Suppose the elements 7, 2, 10 and 4 are inserted, in that order, into the valid 3-ary max heap found in the above question (the array 9, 5, 6, 8, 3, 1), Which one of the following is the sequence of items in the array representing the resultant heap?
Suppose the elements 7, 2, 10 and 4 are inserted, in that order, into the valid 3-ary max heap found in the above question (the array 9, 5, 6, 8, 3, 1), Which one of the following is the sequence of items in the array representing the resultant heap?
Answer: (A)
The 3-ary heap from the previous part is in to . The children of are , , , so the parent of is .
Insert 7 at : parent = 5 < 7, so swap. Then the parent = 9 > 7, so stop. Array: 9, 7, 6, 8, 3, 1, 5.
Insert 2 at : parent = 6 > 2, so no swap. Array: 9, 7, 6, 8, 3, 1, 5, 2.
Insert 10 at : parent = 6 < 10, so swap. Then the parent = 9 < 10, so swap again. Array: 10, 7, 9, 8, 3, 1, 5, 2, 6.
Insert 4 at : parent = 9 > 4, so no swap.
Final array: 10, 7, 9, 8, 3, 1, 5, 2, 6, 4.
Answer: (A)
Insert 7 at : parent = 5 < 7, so swap. Then the parent = 9 > 7, so stop. Array: 9, 7, 6, 8, 3, 1, 5.
Insert 2 at : parent = 6 > 2, so no swap. Array: 9, 7, 6, 8, 3, 1, 5, 2.
Insert 10 at : parent = 6 < 10, so swap. Then the parent = 9 < 10, so swap again. Array: 10, 7, 9, 8, 3, 1, 5, 2, 6.
Insert 4 at : parent = 9 > 4, so no swap.
Final array: 10, 7, 9, 8, 3, 1, 5, 2, 6, 4.
Answer: (A)
A 3-ary max heap is like a binary max heap, but instead of 2 children, nodes have 3 children. A 3-ary heap can be represented by an array as follows: The root is stored in the first location, a[0], nodes in the next level, from left to right, is stored from a[1] to a[3]. The nodes from the second level of the tree from left to right are stored from a[4] location onward. An item x can be inserted into a 3-ary heap containing n items by placing x in the location a[n] and pushing it up the tree to satisfy the heap property.
Which one of the following is a valid sequence of elements in an array representing 3-ary max heap?
Which one of the following is a valid sequence of elements in an array representing 3-ary max heap?
Answer: (D)
With the array starting at index 0 and 3 children per node, the children of a[i] are a[3i+1], a[3i+2] and a[3i+3]. So a[0] has children a[1], a[2], a[3], and a[1] has children a[4], a[5], a[6]. Each array has 6 elements, so only a[0] and a[1] have children (a[1] has only a[4] and a[5]). In a max heap every parent must be at least as large as its children.
(A) The root is 1, smaller than its child 3. Not a heap.
(B) a[1] = 6 has child a[4] = 8, which is larger. Not a heap.
(C) a[1] = 3 has child a[4] = 5, which is larger. Not a heap.
(D) The root 9 is at least 5, 6 and 8. And a[1] = 5 is at least its children a[4] = 3 and a[5] = 1. It is a valid 3-ary max heap.
Answer: (D)
(A) The root is 1, smaller than its child 3. Not a heap.
(B) a[1] = 6 has child a[4] = 8, which is larger. Not a heap.
(C) a[1] = 3 has child a[4] = 5, which is larger. Not a heap.
(D) The root 9 is at least 5, 6 and 8. And a[1] = 5 is at least its children a[4] = 3 and a[5] = 1. It is a valid 3-ary max heap.
Answer: (D)
Keep practising