Heap-Tree
Question 1 |
Consider the array representation of a binary min-heap containing 1023 elements. The minimum number of comparisons required to find the maximum in the heap is _______.
511 |
n=1023
= Ceil(1023/2)
= 512
So, the maximum element is also part of n/2.
So, we have to subtract from the total elements
= 512-1
= 511
Question 2 |
The minimum number of interchanges needed to convert the array
89, 19, 40, 17, 12, 10, 2, 5, 7, 11, 6, 9, 70
into a heap with the maximum element at the root is
0 | |
1 | |
2 | |
3 |
Question 3 |
Consider any array representation of an n element binary heap where the elements are stored from index 1 to index n of the array. For the element stored at index i of the array (i≤n), the index of the parent is
i-1 | |
⌊i/2⌋ | |
⌈i/2⌉ | |
(i+1)/2 |
Left Child is at index: 2i
Right child is at index: 2*i+1
Question 4 |
In a heap with n elements with the smallest element at the root, the 7th smallest element can be found in time
Θ(n log n) | |
Θ(n) | |
Θ(log n) | |
Θ(1) |
1 + 2 + 4 + 6 + 8 + 16 + 32
Which is constant then we can find the 7th smallest element in Θ(1) time.
Question 5 |
Which of the following sequences of array elements forms a heap?
{23, 17, 14, 6, 13, 10, 1, 12, 7, 5} | |
{23, 17, 14, 6, 13, 10, 1, 5, 7, 12} | |
{23, 17, 14, 7, 13, 10, 1, 5, 6, 12} | |
{23, 17, 14, 7, 13, 10, 1, 12, 5, 7} |
In this every children and parent satisfies Max heap properties.
Question 6 |
The elements 32, 15, 20, 30, 12, 25, 16 are inserted one by one in the given order into a Max Heap. The resultant Max Heap is
Question 7 |
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
Ω(logn) | |
Ω(n) | |
Ω(nlog n) | |
Ω(n2) |
Question 8 |
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 _________.
8 | |
9 | |
10 | |
11 |
This is not possible because it violates a property of complete binary tree.
We have total [0, 1023] elements. It means that
∴ 20 + 21 + 22 + ⋯ + 2k = 1024
Add if 1(2(k+1)-1)/(2-1) [using formula for sum of k terms k+1 in G.P]
= 2(k+1) - 1 = 1024 - 1 = 1023
∴ The level ‘9’ at the depth of 8.
Actually we have 1023 elements, we can achieve a complete binary min heap of depth 9, which would cover all 1023 elements, but the max depth of node 9 can be only be 8.
Question 9 |
23, 17, 10, 6, 13, 14, 1, 5, 7, 12 | |
23, 17, 14, 7, 13, 10, 1, 5, 6, 12 | |
23, 17, 14, 6, 13, 10, 1, 5, 7, 15 | |
23, 14, 17, 1, 10, 13, 16, 12, 7, 5 |