An operator delete(\(i\)) for a binary heap data structure is to be designed to delete the item in the \(i\)-th node. Assume that the heap is implemented in an array and \(i\) refers to the \(i\)-th index of the array. If the heap tree has depth \(d\) (number of edges on the path from the root to the farthest leaf), then what is the time complexity to re-fix the heap efficiently after the removal of the element?
GATE CS - 2016,
Answer: B
The time is \(O(d)\) but not \(O(1)\).
To delete the element at index \(i\), replace it with the last element of the heap and shrink the heap by one. The moved element may now violate the heap property in one direction only: if it is larger than its parent (in a max-heap) it must sift up; if it is smaller than a child it must sift down. Either way it moves along a single root-to-leaf path, at most \(d\) levels, so re-fixing takes \(O(d)\) time.
It is not \(O(1)\) in general, since the element may have to travel the whole height. \(O(2^{d})\) and \(O(d\,2^{d})\) are the order of the number of nodes, which would mean rebuilding much of the heap instead of sifting one element.
Please log in or register to report this question.