Fundamentals/Priority Queue and Heap
← PrevNext →
Given a node in a min-heap that may violate the heap property (its key is smaller than its parent's), restore the heap by repeatedly swapping the node with its parent until the parent's key is no longer larger.
Input
node: a heap node with a key field and a parent reference (or null if root)
Output
No return value. Mutates keys in place so the path from node up to the root is non-decreasing.
Examples
Example 1
Inputnode = leaf in heap [1, 2, 3]
Outputheap remains [1, 2, 3]
Example 2
Inputnode = root in single-node heap [1]
Outputheap remains [1]
Constraints
node is non-null.
Only the path from node to root may be modified.