remove
Removes the corresponding place from the heap and detaches the node.
The operation must be idempotent. It means that calling this function again must do nothing at all.
Note for implementers! Usually heaps do not provide deletion of non-root nodes. However, it can be implemented using
Maybe<Priority>instead ofPrioritywith a new order on the new elements that works in the same way on wrapped inSomevalues and treatsNoneas the least element (in case of minimum-heap or the greatest in case of maximum-heap) by the following operations:
decrease priority of the element to
None(so that the node becomes the only least (greatest) node),pop the only minimum node.
That means that it is possible to implement the heap in the way that it can remove arbitrary element. Also, all popular implementations can do it without any problem of introducing new elements with new order. But if for some reason you can not implement the logic without introduction of new elements with new order, you can reimplement idea of Kotlin's Result:
Introduce
internalobjectLeastPriority.Instead of priority of type
Prioritystore alsoLeastPriorityby changing your type toAny?.When you need to compare two nodes' priorities, at first, check whether each of them is
LeastPriorityand if both of them are not the element, cast them toPriorityand use provided order.When you need to return the priority, just cast it to
Priority. It is not of the typePriorityonly when it is in process of the node removement.