algorithm - How to delete in a heap data structure? -


i understand how delete root node max heap procedure deleting node middle remove , replace root repeatedly until desired node deleted?

  1. is o(log n) optimal complexity procedure?

  2. does affect big o complexity since other nodes must deleted in order delete specific node?

actually, can remove item middle of heap without trouble.

the idea take last item in heap and, starting current position (i.e. position held item deleted), sift if new item greater parent of old item. if it's not greater parent, sift down.

that's procedure max heap. min heap, of course, you'd reverse greater , less cases.

finding item in heap o(n) operation, if know in heap, removing o(log n).

i published heap-based priority queue devsource few years back. see a priority queue implementation in c#. has removeat method described.

full source @ http://www.mischel.com/pubs/priqueue.zip


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -