A min-max heap can be useful to implement a double-ended priority queue because of its constant time find-min and find-max operations. We can also retrieve the minimum and maximum elements in the min-max heap in O(log2 n) time. Sometimes, though, we may also want to delete any node in the min-max heap, and this can be done in O(log2 n) , according to the paper which introduced min-max heaps:
...
The structure can also be generalized to support the operation
Find(k)(determine the kth smallest value in the structure) in constant time and the operationDelete(k)(delete the kth smallest value in the structure) in logarithmic time, for any fixed value (or set of values) ofk....
How exactly do I perform a deletion of the kth element on a min-max heap?
What led me to develop this solution (which I'm not 100% sure is correct) is the fact that I've actually found a solution to delete any node in a min-max heap, but it's wrong.
The wrong solution is can be found here (implemented in C++) and here (implemented in Python). I'm going to present the just mentioned wrong Python's solution, which is more accessible to everyone:
The solution is the following:
Now, suppose we have the following min-max heap:
as far as I've checked this is a valid min-max heap. Now, suppose we want to delete the element 55, which in an 0-based array would be found at index 9 (if I counted correctly).
What the solution above would do is simply put the last element in the array, in this case 31, and put it at position 9:
it would delete the last element of the array (which is now 55), and the resulting min-max heap would look like this:
and finally it would "trickle-down" from the
position(i.e. where now we have the number 31)."tricle-down" would check if we're in an even (or min) or odd (or max) level: we're in an odd level (3), so "trickle-down" would call "trickle-down-max" starting from 31, but since 31 has no children, it stops (check the original paper above if you don't know what I'm talking about).
But if you observe that leaves the data structure in a state that is no more a min-max heap, because 54, which is at even level and therefore should be smaller than its descendants, is greater than 31, one of its descendants.
This made me think that we couldn't just look at the children of the node at
position, but that we also needed to check from thatpositionupwards, that maybe we needed to use "trickle-up" too.In the following reasoning, let
xbe the element atpositionafter we delete the element that we wanted to delete and before any fix operations has run. Letpbe its parent (if any).The idea of my algorithm is really that one, and more specifically is based on the fact that:
If
xis on a odd level (like in the example above), and we exchange it with its parentp, which is on an even level, that would not break any rules/invariants of the min-max heap from the newx's position downwards.The same reasoning (I think) can be done if the situation would be reversed, i.e.,
xwas originally in a even position and it would be greater than its parent.Now, if you noticed, the only thing that could need a fix is that, if
xwas exchange with its parent and it's now in a even (and respectively odd) position we may need to check if it's smaller (and respectively greater) than the node at the previous even (and respectively odd) level.This of course didn't seem to be the whole solution to me, and of course I also wanted to check if the previous parent of
x, i.e.p, is in a correct position.If
p, after the exchange withx, is on a odd (and respectively even) level, it means it could be smaller (and respectively greater) than any of its descendants, because it was previously in a even (and respectively odd) level. So, I thought we needed a "trickle-down" here.Regarding the fact if
pis in a correct position with respect to its ancestors, I think the reasoning would be similar to the one above (but I'm not 100% sure).Putting this together I came up with the solution:
This seems to work according to an implementation of a min-max heap that I made and that you can find here.
Note also that the solution run in O(log2 n) time, because we're just calling "push-up" and "push-down" which run in that order.