in Programming in C
1,782 views
3 votes
3 votes

What is the time complexity to delete an arbitrary node from binary heap?

  1. O(n)
  2. O(log n)
  3. O(1)
  4. O(n log n)
     
in Programming in C
1.8k views

1 comment

But what about preserving the heap property again?? there would be some time required to make the heap property satisfiable again?
0
0

4 Answers

5 votes
5 votes

First let us know about what binary heap is: 

A binary heap is a complete binary tree which satisfies the heap ordering property. The ordering can be one of two types:

  • the min-heap property: the value of each node is greater than or equal to the value of its parent, with the minimum-value element at the root.
  • the max-heap property: the value of each node is less than or equal to the value of its parent, with the maximum-value element at the root.
  • Also, a Binary Heap is a complete tree (All levels are completely filled except possibly the last level and the last level has all keys as left as possible). This property of Binary Heap makes them suitable to be stored in an array.

 

The deletion of a random node would take O(n+log(n)) which equivalent to O(n) (worst case) as first we will find where the random element is in the heap which will take O(n) and then delete it which will further take O(log(n))

Therefore time complexity would be O(n)

1 comment

As it is a complete tree but not balanced one that's why searching takes o(n) time if balanced one it would take o(logn) time
0
0
3 votes
3 votes
Logn it should be....if we are deleting a node in binary heap which is nothing but complete binary tree, we take last element put it at postion from where node is deleted then adjust it..

That is nothing but....O(1):for putting last element at position of deleted element.

O(Logn): for adjust .

1+Logn=logn

1 comment

node to be deleted is to be searched in 0(n). Say if it is a MAX element in MIN heap. 0(n+logn)=0(n). cheers
0
0
1 vote
1 vote
Option A)O(n) , as the worst case complexity of deleting a node in binary heap is O(n)
0 votes
0 votes

The question has simply asked to delete an arbitrary node from binary heap.  It didn’t mention any best or worst case or for deleting all the nodes. Thus the average time is Θlogn. Option B is correct