0

Why in most tutorials and documents (98% of what i watch or read) when they take about deleting node form a heap, they only delete the root node (first image where we delete 12 node root). Why not some node of its childrens (second image where we delete node 11 is an example scenario)?

I know that is not impossible to do that and how to do it but why everyone take only about the root node?

I watch about 7 courses in Arabic, other 7 in English and 4 in Turkish. And i read a lot of article about that also in different language!

But all of them delete the root node! Why? Note: I am a beginner in computer science.

As i write in my question details, i read and watch a lot of courses and articles about this topic

firs-scenario

enter image description here

Htam
  • 11
  • 1
  • Can you paste some example? Example can be either code or it can be diagram as well. – Suhail Akhtar Dec 20 '22 at 08:07
  • @SuhailAkhtar I edit my question with more detail – Htam Dec 20 '22 at 11:47
  • I guess you are forgetting the concept of `heapify`. In heap you can't delete random elements you can only delete the top elements of heap. By top elements I mean element 0 in your diagram. So what we do is we swap element `0` with element `12`. Then we pop element `12` and perform `heapify` because element `0` is at the top. Hope this helps. – Suhail Akhtar Dec 20 '22 at 12:07
  • We know that we can delete element from anywhere in a heap. Thus, swap the element to delete (let say its index is toDelete) with the last element in the heap, then while parent of toDelete is smaller then toDelete swap them till toDelete=0. But if is not smaller then toDelete then max-heapify from toDelete. But my question is why everyone say that we can remove only the root element? – Htam Dec 20 '22 at 12:48
  • You *can* remove a node from a heap. https://stackoverflow.com/a/8706363/56778 gives a detailed description of how to do it. It's a useful thing to do in some cases, but is not a standard function of the priority queue data structure, so it's not as well covered. – Jim Mischel Dec 21 '22 at 00:47
  • @JimMischel as i understand, they do that because the reallife application of heap generally does not require to remove elment from middle. Am i right? – Htam Dec 21 '22 at 09:21
  • @Htam that's exactly right. – Jim Mischel Dec 27 '22 at 21:28

0 Answers0