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?
is o(log n) optimal complexity procedure?
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
Post a Comment