In a heap can the node that's not the root be removed? For example in this diagram How would node with key 25 be removed?
Printable View
In a heap can the node that's not the root be removed? For example in this diagram How would node with key 25 be removed?
I'm assuming you are using the array representation of the heap.
Removing an arbitrary node is no different than removing the root node. Just replace the node with the last element of the heap (last element in the array), then do a heapify-down operation to preserve the heap property.
In principle, any node can be removed from a heap, including even the root node.
The efficiency of that operation, and the steps needed to maintain integrity of the heap, are the costs of doing that.
You'd replace the node you are removing with the last element in the heap. And upon thinking more about it, I think you need to heapify up as well as down to preserve the heap property. I think grumpy's implication that it might be more expensive than removing the root is probably correct.