What is the time complexity of inserting into a binary min-heap?

Boost your GATE General Aptitude and CS Exam readiness with our dynamic quiz. Test your skills with comprehensive questions featuring hints and detailed solutions. Ace your GATE exam confidently!

Multiple Choice

What is the time complexity of inserting into a binary min-heap?

Explanation:
When you insert into a binary min-heap, you first place the new element at the end of the array (to keep the tree complete) and then percolate it up by repeatedly swapping with its parent if it is smaller. Each step is constant work, and you can move up at most one level per swap. The heap is a complete binary tree, so its height is proportional to log2 n, where n is the number of elements. Thus, in the worst case you bubble from the bottom to the root, performing about log n swaps, giving an overall time of O(log n). (In the best case you might do no swaps, which is O(1), but the standard measure is the worst-case time, hence O(log n).)

When you insert into a binary min-heap, you first place the new element at the end of the array (to keep the tree complete) and then percolate it up by repeatedly swapping with its parent if it is smaller. Each step is constant work, and you can move up at most one level per swap. The heap is a complete binary tree, so its height is proportional to log2 n, where n is the number of elements. Thus, in the worst case you bubble from the bottom to the root, performing about log n swaps, giving an overall time of O(log n). (In the best case you might do no swaps, which is O(1), but the standard measure is the worst-case time, hence O(log n).)

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy