Can you update the value of a heap node?


#1

Question

In the context of this lesson, can you update the value of a heap node?

Answer

Yes, you can update the value of a specific node in a heap. This is outside the scope of the lessons, but this will give a general explanation.

When updating the value of a specific node of a heap, this is usually referring to the decrease-key or increase-key operation. These operations will update the value of a key, to a value that is lower or higher, and then heapify the heap as necessary to maintain its properties.

Usually, decrease-key is only applied in min-heaps, while increase-key is only applied in max-heaps. The functions will essentially update the value of the node, then heapify-up that node until the heap property is restored.

However, you can apply these to both a min-heap and a max-heap if you wanted. When doing this however, the implementation would be different. Instead of using just heapify-up, we would have to use heapify-down. And, you might need to heapify the rest of the heap a few times to maintain the heap property.