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 11–15 of 32
The number of possible min-heaps containing each value from {1, 2, 3, 4, 5, 6, 7} exactly once is _____.
Answer: 80
The root has to be 1. The other 6 numbers are shared between the left and right subtrees, 3 numbers each. The number of ways to choose them is . Inside each 3-node subtree, the smallest number is the root, and the other two numbers can swap places. That gives 2 ways for each subtree.
Total = 20 2 2 = 80
Answer: 80
Total = 20 2 2 = 80
Answer: 80
An operator delete(i) for a binary heap data structure is to be designed to delete the item in the i-th node.Assume that the heap is implemented in an array and i refers to the i-th index of thearray.If the heap tree has depth d (number of edges on the path from the root to the farthest leaf),the n what is the time complexity to re-fix the heap efficiently after the removal of the element?
Answer: (B)
To delete node i, put the last element in position i. Then sift it either up or down. In both cases it moves along a single path from the root to a leaf, so it goes at most d levels . It can need all d levels, so the cost is not .
Answer: O(d) but not O(1)
Answer: O(d) but not O(1)
A complete binary min-heap is made by including each integer in [1,1023] exactly once. The depth of a node in the heap is the length of the path from the root of the heap to that node. Thus, the root is at depth 0. The maximum depth at which integer 9 can appear is_____. .
Answer: 8
In a min-heap, every ancestor of 9 has to be smaller than 9. Only the numbers 1 to 8 are smaller. So 9 can have at most 8 ancestors the depth is at most 8. This can happen: place 1, 2, , 8, 9 along one path (the heap with 1023 nodes has depths 0 to 9).
Answer: 8
Answer: 8
Consider a max heap, represented by the array: 40, 30, 20, 10, 15, 16, 17, 8, 4.
Now consider that a value 35 is inserted into this heap. After insertion, the new heap is
Now consider that a value 35 is inserted into this heap. After insertion, the new heap is
Answer: (B)
Put 35 in the next empty place, which is index 10. It becomes the child of 15.
- Compare 35 and 15: 35 15, so swap 35 moves under 30.
- Compare 35 and 30: 35 30, so swap 35 moves under 40.
- Compare 35 and 40: 35 40, so stop.
Final heap: 40, 35, 20, 10, 30, 16, 17, 8, 4, 15
Answer: 40, 35, 20, 10, 30, 16, 17, 8, 4, 15
- Compare 35 and 15: 35 15, so swap 35 moves under 30.
- Compare 35 and 30: 35 30, so swap 35 moves under 40.
- Compare 35 and 40: 35 40, so stop.
Final heap: 40, 35, 20, 10, 30, 16, 17, 8, 4, 15
Answer: 40, 35, 20, 10, 30, 16, 17, 8, 4, 15
Consider a complete binary tree where the left and the right subtrees of the root are max-heaps. The lower bound for the number of operations to convert the tree to a heap is
Answer: (A)
Both subtrees of the root are already max-heaps, so only the root can be out of place. Sift-down repairs it: compare the root with its larger child, swap if the child is bigger, and repeat one level lower. Each step moves down one level, and the height is about , so operations are enough.
Worst case: the root holds the smallest key. It must travel down to a leaf, so about swaps and comparisons are needed, and no method can do with fewer. So the lower bound is .
A bound like or cannot be right, because the work above is only .
Answer: (A)
Worst case: the root holds the smallest key. It must travel down to a leaf, so about swaps and comparisons are needed, and no method can do with fewer. So the lower bound is .
A bound like or cannot be right, because the work above is only .
Answer: (A)
Keep practising