• edited by
27,013 views
82 82 votes

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?

  1.  $O(1)$ 
  2.  $O(d)$ but not $O(1)$ 
  3.  $O(2^d)$ but not $O(d)$ 
  4.  $O(d \ 2^d)$ but not $O(2^d)$

6 Answers

Best answer
84 84 votes

Answer would be (B) $O(d)$ but not $O(1)$.. as we need to apply heapify.. and suppose if we are deleting root, in worst case would take $O(d)$ time..

• edited by
16 16 votes

here d= 2  now in worst case rooti.e. 50 will be deleted. so max 2(and 2 comparoision needed) swap needed . so for d length O(d) time will be needed .

since O(1) is also given b/c if we delete leaf node i.e. 33 then noneed to do anything.

So B is answer

9 9 votes

The question is about the time complexity of deleting an item in a binary heap.

A binary heap is a complete binary tree, which can be efficiently implemented as an array. The operations on a binary heap, like insert, delete, and extract max (or min for a min heap), involve maintaining the heap property after the operation. This is usually done using a procedure called "heapify" or "sift-down" that moves a node down the tree, swapping it with its children until the heap property is restored.

The operation delete(i) is supposed to delete the i-th node in the heap. Deleting a node involves two steps:

  1. Replace the node to be deleted with the last node in the heap.
  2. Heapify the heap to restore the heap property.

The replacement operation takes constant time, O(1), because we're dealing with an array and we have direct access to any element.

Heapify is the operation that can take more time. In the worst case, a node might have to be moved down the tree all the way to the leaf level. Since the heap is a complete binary tree, its depth d is approximately equal to log(n) where n is the number of nodes in the heap.

Therefore, the worst-case time complexity of the delete(i) operation is dominated by the heapify step, which is O(d), where d is the depth of the heap.

So, the answer to the question is "2. $O(d)$ but not $O(1)$"​

2 2 votes

When you remove an element like the root, you fill its spot with an element from the very bottom level (a leaf). This new element is likely out of place.

To restore the heap's order, this element must move down the tree, level by level, until it settles into its correct position. The longest journey this element can possibly take is from the root all the way back down to a leaf.

The length of this longest journey is exactly the depth (d) of the heap. Because the maximum number of steps is limited by the heap's depth, the time complexity is O(d), which is the same as O(logn).

0 0 votes
IF WE DELETE AN ELEMENT FROM HEAP, THAN IN PLACE OF THAT DELETED ELEMENT, WE PUT THE LAST ELEMENT OF ARRAY (HEAP). AND THAN WE HEAPIFY THE HEAP. IT WILL COST LOGN COMPARISONS IE DEPTH OF HEAP. THEREFORE ANSWER WOULD BE LOGN IE DEPTH-d. not o(1).
Answer:
Position:
Show:

Related questions

90 90 votes
10 answers 10 answers
38.8k
38.8k views
Sandeep Singh asked Feb 12, 2016
38,760 views
Consider the weighted undirected graph with $4$ vertices, where the weight of edge $\{i,j\}$ is given by the entry $W_{ij}$ in the matrix $W$. W=$\begin{bmatrix} 0&2 &8 &...
79 79 votes
5 answers 5 answers
35.6k
35.6k views
Sandeep Singh asked Feb 12, 2016
35,622 views
A queue is implemented using an array such that ENQUEUE and DEQUEUE operations are performed efficiently. Which one of the following statements is CORRECT ($n$ refers to ...
241 241 votes
14 answers 14 answers
62.0k
62.0k views
Sandeep Singh asked Feb 12, 2016
61,991 views
Let $Q$ denote a queue containing sixteen numbers and $S$ be an empty stack. $Head(Q)$ returns the element at the head of the queue $Q$ without removing it from $Q$. Simi...
109 109 votes
5 answers 5 answers
42.0k
42.0k views
Akash Kanase asked Feb 12, 2016
41,995 views
A complete binary min-heap is made by including each integer in $[1, 1023]$ exactly once. The depth of a node in the heap is the length of the path from the root of the h...