20 votes 20 votes Consider the binary tree in the figure below: Give different steps for deleting the node with key $5$ so that the structure is preserved. DS gate1991 data-structures binary-tree normal descriptive + – Akash Kanase asked Apr 18, 2016 • edited May 8, 2021 by gatecse Akash Kanase 3.6k views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
Best answer 21 votes 21 votes Since the given binary tree is a min-heap tree. First swap $27$ and $5$ Then delete $5$ Apply min-heapify And structure will be: ManojK answered Apr 19, 2016 • edited Apr 17, 2021 by Lakshman Bhaiya ManojK comment Share Follow See all 15 Comments See all 15 15 Comments reply Arjun commented Apr 19, 2016 reply Follow Share but deleting 1 is not told in question. 0 votes 0 votes ManojK commented Apr 19, 2016 reply Follow Share can we do first swap 27 and 5 And then delete 5 and apply minheapify 2 votes 2 votes ravi_ssj4 commented Aug 17, 2016 reply Follow Share is this the correct answer for sure ? 0 votes 0 votes ManojK commented Aug 17, 2016 reply Follow Share ravi do you have alternative than you can add here. –1 votes –1 votes ravi_ssj4 commented Aug 21, 2016 reply Follow Share @ManojK, No, not at the moment, I am yet to read Heaps properly. But I just wanted to know the general method when it comes to deleting a node in a Heap having 2 children. Like there are different ways to delete nodes with different no. children when it comes to BSTs. 0 votes 0 votes . commented Dec 1, 2016 i edited by . Dec 1, 2016 reply Follow Share We can use increase key operation where we can set the value of 5 to infinityO(1) and then apply minheapify down the pathO(log n) and then delete infinity O(1) 0 votes 0 votes anchitjindal07 commented Apr 14, 2017 reply Follow Share I think during first step, we should swap 5 and 17, not 27. Because in the subtree rooted at 5, the last element is 17. 27 is the last element in main heap. Correct me if I am wrong 0 votes 0 votes pawan kumarln commented Aug 22, 2017 reply Follow Share @ ManojK is correct First swap 27 and 5 Then delete 5 then heapify https://webdocs.cs.ualberta.ca/~holte/T26/heap-del.html 1 votes 1 votes itsvkp1 commented Oct 25, 2017 reply Follow Share is this correct method for solving this que ??? cant we replace 5 by its inorder successor or predecessor and after that min hepify the tree ??? 0 votes 0 votes junaid ahmad commented Oct 25, 2017 reply Follow Share @ itsvkp1 no you can't,that will be applicable to only BST not in a heap. 0 votes 0 votes Hemant Parihar commented Oct 25, 2017 reply Follow Share @achitjindal07 If you swap 17 and 5, and then delete instead of swapping 27 and 5. Then it is not correct because structure property of heap that is "All the level of heap should be completely filled except the last level and the last level should be left filled" is violated. 10 votes 10 votes rahul sharma 5 commented Nov 30, 2017 reply Follow Share 25 has right child but not left,it is not complete tree.It cannot be min heap.I think there is some mistake in question? 4 votes 4 votes tusharp commented Jan 14, 2019 reply Follow Share @rahul sharma 5 nice catch :) . 0 votes 0 votes ankit3009 commented Feb 2, 2021 reply Follow Share Yes it is not almost complete binary tree, so not min heap too. To satisfy the min heap, it must be almost complete binary tree. 0 votes 0 votes Pranavpurkar commented Aug 11, 2021 reply Follow Share 25 has right child but not left,it is not complete tree It is ACBT that’s why min heap possible but i am confused that can we delete 5 in min heap i mean in min heap always the root is deleted by default and we can’t delete from the middle of the tree bcoz then it will not remain ACBT and thus not min heap! so my doubt is how can we directly delete 5 in min heap without deleting the root 1 ? 1 votes 1 votes Please log in or register to add a comment.
0 votes 0 votes Sorry for orientation problem Rishi yadav answered Oct 4, 2017 Rishi yadav comment Share Follow See 1 comment See all 1 1 comment reply Kiyoshi commented Apr 25, 2021 reply Follow Share why you always have orientation problem... 2 votes 2 votes Please log in or register to add a comment.
0 votes 0 votes Assuming that we have stored this heap in an array structure, Procedure: Search for element $5$ using the sequential search in the array. Swap it with the last element, in this case, $27.$ Bubble down it so that the min-heap property is satisfied. Lakshman Bhaiya answered Apr 17, 2021 • reshown Apr 17, 2021 by Lakshman Bhaiya Lakshman Bhaiya comment Share Follow See all 0 reply Please log in or register to add a comment.