Removing a node from the middle of the heap

Removing a node from the middle of the heap can be done in O (log n) if we can find the element on the heap in constant time. Suppose the cube node contains id as a field. Now, if we provide an identifier, how can we remove the node at O ​​(lg n) time?

One solution could be that we can have a location address in each node, where we store the node index on the heap. This array will be ordered using node ids. However, this requires an additional array. Is there any other good method to achieve the same.

PS: I encountered this problem when implementing the Shortist Path algorithm from Djikstra.

+5
source share
2 answers

(id, node) -, O (1) ( ). O (log n).

+1

. wikipedia

The operations commonly performed with a heap are:
create-heap: create an empty heap
find-max or find-min: find the maximum item of a max-heap or a minimum item of a min-heap, respectively
delete-max or delete-min: removing the root node of a max- or min-heap, respectively
increase-key or decrease-key: updating a key within a max- or min-heap, respectively
insert: adding a new key to the heap
merge joining two heaps to form a valid new heap containing all the elements of both.

, , . ( ).

0

All Articles