Wikipedia says "increase key" is O(log n) for a binary heap, but it is referring to an internal function. I'm not asking about that: Imagine the data structure had an external/public function named modifyElement(currentElement, newElement).
Some constraints:
- The
currentElementis in the data structure already, but it could be anywhere. newElementcan be more valuable, less valuable, or equal value to thecurrentElement.
If this API existed on a heap, what would its Big O notation be (in runtime cost)?
I think it would be O(n + log n)1. A heap is partially sorted, not completely sorted. I think that means you would have to, worst-case, traverse every element to find the currentElement you're looking for, which would be O(n). Once you find it, you need to modify it and "re-heapify" it, which will cost O(log n).
Is O(n) the correct answer? If not, please explain. Thanks!
1: Simplified to O(n)
