The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+13 votes
1k 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

asked in DS by Veteran (59.4k points)
edited by | 1k views

3 Answers

+16 votes
Best answer

answer = 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 it's parent, bubble it up to keep Max heap property

answered by Boss (42.4k points)
edited by
+7 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)
+5 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.1k 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

35,506 questions
42,827 answers
121,678 comments
42,181 users