37 37 votes 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 Data Structures gatecse-2004 data-structures binary-heap easy + – Kathleen 9.7k views answer comment Share Follow Print 0 reply Please log in or register to add a comment.
Best answer 41 41 votes 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 Akash Kanase answered Nov 22, 2015 • edited Jun 28, 2019 by Lakshman Bhaiya Akash Kanase comment Share Follow See all 2 Comments 2 2 Comments reply Nikhil_v commented Aug 1 reply Follow flag Just to confirm TC would NLOGN right ?? 0 0 replyShare EagerLearner commented Aug 6 reply Follow flag Nikhil_vYes, n nodes are inserted and at the worst case we need to correct it till the root which is of height log n..So, O(nlogn) 0 0 replyShare Please log in or register to add a comment.
14 14 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 Rakesh K answered Jan 6, 2017 Rakesh K comment Share Follow See all 5 Comments 5 5 Comments reply Show 2 previous comments Pranavpurkar commented Jul 24, 2022 reply Follow flag ASNR1010 The first one is the correct approach as per previous GATE questions are concerned. 1 1 replyShare manishankarkanrar commented Jul 22, 2024 reply Follow flag Hats off bro 0 0 replyShare raghu_46 commented Nov 15, 2025 reply Follow flag @RavGopal We need heapify for each new element inserted. No need to wait for second children. When the new node comes attach according to the full binary tree then apply heapify . (As mentioned in the question The elements are inserted one by one in the given order into a maxHeap.) 0 0 replyShare Please log in or register to add a comment.
6 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 Bhagirathi answered Sep 21, 2014 Bhagirathi comment Share Follow 0 reply Please log in or register to add a comment.