edited by
11,111 views
52 votes
52 votes

An array of integers of size $n$ can be converted into a heap by adjusting the heaps rooted at each internal node of the complete binary tree starting at the node $\left \lfloor (n - 1) /2 \right \rfloor$, and doing this adjustment up to the root node (root node is at index $0$) in the order $\left \lfloor (n - 1) /2 \right \rfloor$, $\left \lfloor (n - 3) /2 \right \rfloor$, ....., $0$. The time required to construct a heap in this manner is

  1. $O(\log n)$
  2. $O(n)$
  3. $O (n \log \log n)$
  4. $O(n \log n)$
edited by

4 Answers

Best answer
51 votes
51 votes

By using Build Heap method we can create heap from complete binary tree.

which will take $O(n)$.

Correct Answer: $B$

edited by
17 votes
17 votes

The above statement is actually algorithm for building a Heap of an input array A.

BUILD-HEAP(A) 
    heapsize := size(A); 
    for i := floor(heapsize/2) downto 1 
        do HEAPIFY(A, i); 
    end for 
END

Upper bound of time complexity is O(n) for above algo

5 votes
5 votes

Well, there are two methods fro constructing a heap:

1. Entering key into the heap one by one: O(nlogn)

2. Building heap from an array: O(n)

I will not discuss the entire method here, but I will mention the technique.

When we construct heap from an array we do not actually construct any tree. We create a heap by simply doing certain adjustment in the position in the array so that it behaves like a heap.

Specifically speaking, the relation is that for every parent node,  its child nodes will be at index (2i+1, 2i+2) where i=0,1,2.....

Now we know that number of leaf nodes the heap would (n+1)/2. So here we do nothing to the leaf nodes.

We simple perform adjustments on the remaining floor(n/2) elements.

The adjustment includes:

2 comparisons with the child node

And​ atmost 1 swap if required.

hence, overall time complexity is O(1)

So for adjusting, n/2 elements, it will take O(n) time.

This question has the description of constructing a heap from an array.

Hence, the answer would be O(n).

Answer:

Related questions

6.3k
views
1 answers
26 votes
Ishrat Jahan asked Nov 2, 2014
6,289 views
A program attempts to generate as many permutations as possible of the string, '$abcd$' by pushing the characters $a, b, c, d$ in the same order ... $dcba$cbad$cabd$
25.5k
views
9 answers
66 votes
Ishrat Jahan asked Nov 1, 2014
25,464 views
Let $P$ be a singly linked list. Let $Q$ be the pointer to an intermediate node $x$ in the list. What is the worst-case time complexity of the best-known algorithm to delete the node $x$ from the list ?$O(n)$O(\log^2 n)$O(\log n)$O(1)$
5.1k
views
4 answers
23 votes
Ishrat Jahan asked Nov 2, 2014
5,072 views
Which one of the following binary trees has its inorder and preorder traversals as $BCAD$ and $ABCD$, respectively?
3.4k
views
1 answers
3 votes
Ishrat Jahan asked Nov 2, 2014
3,397 views
Given below are several usages of the anchor tag in HTML.<A HREF = "http://www.gate.ac.in/HTML/BASIC/testpage.html">Test Me</A><A HREF = "/BASIC/testpage.html"> ... are valid?I and II onlyI and III onlyI, II and III onlyI, II, III and IV