retagged by
31,361 views
63 63 votes
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 ___________.

9 Answers

Best answer
100 100 votes

If a heap has $1023$ elements, it'll contain $512$ leaves and since it is a MIN-HEAP, the maximum will be present in the leaves. (Why? Assume it is not, then there will be some elements present under it and this will violate the min-heap property.)

We can visualise it this way. At each level starting with root level, number of elements are

$1-2-4-8-16-32-64-128-256-512$ (this is the last level, hence leaves)

So if we have $n$ elements in an array, we know that to find the maximum it takes $n-1$ comparisons.

In this case, $n = 512$, so the answer should be $511$.

Some other excellent questions on finding maximum-minimum:

edited by
21 21 votes

Answer : 511

In Binary min-heap , Maximum element will be present in the leaf nodes .

so, we already know that from "floor(n/2)+1 to n" leaf nodes are present

floor(1023/2)+1 to 1023 = 512 to 1023 => total 512 elements (put these all in an array i:e,[all the elements in array are unsorted] ) .

 

now we know that to find maximum in an array we will surely require minimum n-1 comparisons(you can apply any searching algorithm , n-1 comparison must be needed) .

so, 512-1 = 511 

4 4 votes
Complete binary tree is a binary tree,so

Total no.of nodes=no.of internal nodes +no. Of leaf nodes

N=L+I

N=L+L-1=2L-1

1023=2L-1

2L=1024

L=512

No. Of comparisons to find minimum or maximum= no.of elements -1

                                                                                            =512-1=511
2 2 votes

Minimum 511 comparisons  required.

A heap with n nodes satisfies a property that it has (1 to floor(n/2)) non leaf elements & (floor(n/2)+1 to n) leaf elements.

So, the non leaf node cannot contain the maximum element. To look for max element we have to search the leaves.

applying the above formulae, we'll get 512 leaves.

as we know for m elements, minimum m-1 comparison is required to find min/max element. 

so we need minimum of 512-1=511 comparisons to find the max element of heap.

 

1 1 vote
In a binary min-heap the maximum element will always remain in the leaf node

Let us consider that all the elements in the heap is stored in an array of n+1 elements starting from index 1.(We are keeping index 0 free for easier and minimal calculations, it can be done with index 0 also)

For a given node arr[i],

the left child will be – arr[2*i]

the right child will be – arr[2*i + 1]

 

The leaf nodes cannot have any child, so the value of 2*i will always exceed n, that means any leaf node will come after any non leaf node in the array representation, so we have to traverse the last floor(n/2) + 1 elements to get the maximum element.

Total number of leaves = 511 + 1 = 512

To find the maximum of 512 elements we have to do atleast 511 comparisons.
1 1 vote

In generally ,heap is in two forms i.e one is min heap and second one in max heap;

heap always follows complete binary tree;

min heap means parent<=child

max heap means parent>=child

In this question ,they are asking about in which place max element present in min heap(which contains 1023 element);

In min heap ,max element always present in leafs(even if it contains duplicates). 

Due to that reason ,0th -level contains 1 element

0th -level contains 1 element

1st -level contains 2elements

2nd  -level contains 4elements

...upto 8th level contains 2^0 +2^1+2^2+….+2^8=2^9-1(according to GP formula) which is equal to 511 elements.

remaining elements are 512 elements ,511 comparisions required to compare 512 elements(the elements which are present on the leafs)

Answer:
Position:
Show:

Related questions

59 59 votes
4 answers 4 answers
24.9k
24.9k views
Arjun asked Feb 12, 2020
24,866 views
Consider a double hashing scheme in which the primary hash function is $h_1(k)= k \text{ mod } 23$, and the secondary hash function is $h_2(k)=1+(k \text{ mod } 19)$. Ass...
63 63 votes
9 answers 9 answers
29.9k
29.9k views
Arjun asked Feb 12, 2020
29,889 views
The number of permutations of the characters in LILAC so that no character appears in its original position, if the two L’s are indistinguishable, is ______.
58 58 votes
5 answers 5 answers
30.9k
30.9k views
Arjun asked Feb 12, 2020
30,897 views
Consider a non-pipelined processor operating at $2.5$ GHz. It takes $5$ clock cycles to complete an instruction. You are going to make a $5$- stage pipeline out of this p...
64 64 votes
9 answers 9 answers
45.4k
45.4k views
Arjun asked Feb 12, 2020
45,370 views
A processor has $64$ registers and uses $16$-bit instruction format. It has two types of instructions: I-type and R-type. Each I-type instruction contains an opcode, a re...