(2) / \ (13) (7) / \ / \ (17) (14) (22) (8) / (21)
after deleting min | after inserting 13 |
---|---|
7 / \ 13 8 /\ /\ 17 14 22 21 |
2 / \ 13 7 / \ / \ 15 14 22 8 / \ 20 17 |
initial heap | after getMin() | after insert(1) |
---|---|---|
1 / \ 2 3 |
2 / 3 |
1 / \ 3 2 |
initial heap | after insert(1) | after getMin() |
---|---|---|
2 / \ 2 3 / \ 4 5 |
1 / \ 2 2 / \ / 4 5 3 |
2 / \ 3 2 / \ 4 5 |
A similar example could be found if we make the choice to always promote the right child.
If all priorities are guaranteed to be distinct then the heap does remain the same. To see this, consider the path, from leaf to root, that the smallest item (call it x) follows when it is inserted. Each item along this path is moved one "downward" as x is moved into its correct position at the root. If x is then deleted, the last item along this path is moved into the root position; this item is then "bubbled down" the tree until it reaches the correct position. This has the effect of moving each item along the path "upward" by one level. This returns the tree to its original configuration.
initial BST | after delete(A) | after insert(A) |
---|---|---|
A \ B |
B |
B / A |