The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+13 votes
1.4k views

The elements $32, 15, 20, 30, 12, 25, 16,$ are inserted one by one in the given order into a maxHeap. The resultant maxHeap is

  1.  
asked in DS by Veteran (52.1k points)
edited by | 1.4k views

3 Answers

+17 votes
Best answer

The answer is option A.

Just keep inserting elements making sure resulting Tree is nearly Complete. (Heap Property) .

While inserting any node, if you find that  Value of New Node $>$ Value of its parent, bubble it up to keep Max heap property

answered by Boss (41k points)
edited by
+8 votes
Insert each node as the left most leaf and check if it is less than the parent or not, if not then swap it with the parent

   32

   32
   /
15

   32
   /  \
15  20

Now 30 cannot be inserted as 15's child, so it will be in 15's place with 15 as it's child

    32
    /  \
 30  20
  /
15

     32
     /  \
  30  20
  /  \
15  12

25 cannot be inserted as 20's child, so it will be in place of 20, with 20 as it's child

        32
        /  \
     30   25
    /  \      /
 15  12  20

        32
        /   \
     30    25
    /  \      /  \
 15  12  20  16
answered by Active (1.8k points)
+6 votes
Option a is correct just try to insert an element and shift whenever necessary Here shift operation is performed when 30 and 25 is inserted
answered by Boss (14.4k points)
Answer:

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,811 questions
54,540 answers
188,429 comments
75,605 users