Search code examples
cdata-structuresheap

delete keys other than root in heap, a valid practice?


I come across various views on whether it is valid for a heap to have an operation that deletes a random key other than the root (max or min), what s the standard implementation of heaps, is it a good practice to implement heap with a delete function for keys other than root?


Solution

  • A basic implementation of a binary heap will only provide the deletion of the root key, but more rich implementations can surely provide the deletion of any other node in the heap. (See Wikipedia ... "basic" and "internal" operations).

    However, unlike the root-delete operation, the deletion of another (randomly chosen) key needs the information as to which key to delete. And to avoid loss of efficiency, you then need an extra data structure to know where in the heap that key is currently stored. It would be a bad idea to scan the complete heap in search for a given key. So you would need an extra data structure that maps each key to an index in the heap array, and this data structure will need to be maintained, which slows down the other operations a bit as well -- though not their time complexity.