Heap-Tree

Question 1

The number of possible min-heaps containing each value from {1, 2, 3, 4, 5, 6, 7} exactly once is ___________.

A
80
B
81
C
82
D
83
Question 1 Explanation: 
--> We have 7 distinct integers {1,2,3,4,5,6,7} and sort it
--> After sorting, pick the minimum element and make it the root of the min heap.
--> So, there is only 1 way to make the root of the min heap.
--> Now we are left with 6 elements
Question 2

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

A
Ω(logn)
B
Ω(n)
C
Ω(nlog n)
D
Ω(n2)
Question 2 Explanation: 
Since left and right subtree is already a heap. So we can apply Heapify (node) which take log n time.
Question 3

Consider the following array of elements. 〈89, 19, 50, 17, 12, 15, 2, 5, 7, 11, 6, 9, 100〉. The minimum number of interchanges needed to convert it into a max-heap is

A
4
B
5
C
2
D
3
Question 3 Explanation: 
Let's first draw heap from the given array,

→ Swap 100 & 15

→ Swap 100 & 50

→ Swap 89 & 100

∴ Total 3 interchanges are needed.
Question 4

Consider a binary max-heap implemented using an array.

Which one of the following array represents 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}
Question 4 Explanation: 
Option-A:

Violating a max heap property.
Option-B:

Violating a max heap property.
Option-C:
Question 5

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 _________.

A
8
B
9
C
10
D
11
Question 5 Explanation: 

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 6

Consider a binary max-heap implemented using an array.

What is the content of the array after two delete operations on the correct answer to the previous question?

A
{14,13,12,10,8}
B
{14,12,13,8,10}
C
{14,13,8,12,10}
D
{14,13,12,8,10}
Question 6 Explanation: 
Actual Graph:

Step 1: Delete 25

Step 2:

Step 3: Delete 16

Step 4:

Step 5:

∴ Binary array elements: 14, 13, 12, 8, 10.
Question 7

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

A
θ(log n)
B
θ(n)
C
θ(nlog n)
D
θ(n2)
Question 7 Explanation: 
Inserting an element into binary(either max or min) heap takes O(logn) for all cases, but in question they clearly mentioned that n elements and inserting one by one n elements, so it takes 2n time. So, O(n).
Note: We can also insert all the elements once, there will be no difference on time complexity.
Question 8

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:

A
θ(log2n)
B
θ(log2log2n)
C
θ(n)
D
θ(nlog2n)
Question 8 Explanation: 
Max heap is the complete binary tree that means each node has either zero children or two children except last level. So in worst case insertion of element is at last level. So, number of comparisons required at each level starting from root is equal to 1+2+4+8+16+---- this series is equal to "logn". All the elements are sorted, the binary search which will result in O(loglogn) number of comparisons.
Question 9

In a binary max heap containing n numbers, the smallest element can be found in time

A
O(n)
B
O(log n)
C
O(log log n)
D
O(1)
Question 9 Explanation: 
In a MAX heap the smallest values of the heap will always be present on the last level of heap and time complexity of reaching the last level of heap is O(n).
We have to search all the elements to reach the smallest element and heap using linear search.
To traverse all elements using linear search will take O(n) time.
There are 9 questions to complete.

Access quiz wise question and answers by becoming as a solutions adda PRO SUBSCRIBER with Ad-Free content

Register Now

If you have registered and made your payment please contact solutionsadda.in@gmail.com to get access