1

CLRS Exercise: 6.5-8

The operation HEAP-DELETE(A,i) deletes the item in node i from heap A. Give an implementation of HEAP-DELETE that runs in O(lg n) time for an n-element max-heap.

enter image description here

I wonder if the algorithm is wrong for the input A[10]={84,22,19,21,3,10,6,5,20}(Index starts with 1) and with A[6]=10 being deleted. Replacing the last node with A[6] would result in violating heap property, overlooking the parent value.

I wrote an algorithm for this and wanted to know if it works right or where am I going wrong ?

HEAP-DELETE(A,i)
  A[i]=A[A.heapsize]
  A.heapsize-=1
  while i>1 and A[parent(i)]<A[i]
    swap A[i] with A[parent(i)]
    i=parent(i);
Nick Zuber
  • 5,467
  • 3
  • 24
  • 48
sagar_jeevan
  • 761
  • 5
  • 16
  • 34
  • *Replacing the last node with A[6] would result in violating heap property* which exactly why we call "MAX-HEAPIFY" – Nick Zuber Jul 15 '16 at 19:14
  • But `MAX-HEAPIFY` is to sort the underlying nodes, its children. In this case `A[6]` becomes 20 after replacing with the last node and `A[6]=20` is larger than its parent which is 19. Its not replacing with the parent right? – sagar_jeevan Jul 15 '16 at 19:22

4 Answers4

4

When deleting a node from a max heap, the first thing we need to do is swap the target element with the last element, then delete the last element.

Now, we're faced with the problem of fixing the max heap since we just moved an element around. Let's refer to that element that we just moved as x.

There are three cases:

  • x is greater than its parent
  • x is less than its parent
  • x is equal to its parent

If x is equal to its parent, that's easy - we do nothing.

If x is less than its parent, all we need to do is a MAX-HEAPIFY (which I'm assuming you understand how that works from the comments), because we need to fix any mistakes below x.

If x is greater than its parent, we run into the issue that you've brought up. Handling this isn't too tricky - we just need to compare x to its parent, and if x is greater than the parent, we swap them. Continue this process until x is no longer greater than its parent or we reach the root (when x has no parents).

With that being said, the pseudocode you've posted looks right to me. Nice work :)

Nick Zuber
  • 5,467
  • 3
  • 24
  • 48
4

This is the pseudocode :

HEAP-DELETE(A, i):
  A[i] = A[A.heap-size]
  A.heap-size -= 1
  // Case : 8, 7 3, 5 6 1 2, 4 and delete 1
  while(i > 1 and A[Parent(i)] < a[i])
      swap A[i] with A[parent(i)]
      i = Parent(i)
  // Case : 10, 9 6, 8 7 5 4, 3 and delete 6
  MAX-HEAPIFY(A, i)
  • *"the element is already smaller than its parent"* : That premise is not always true. Take for instance this heap (in breadth-first order): `8, 7 3, 5 6 1 2, 4`, and delete the 1. Then 4 comes in its place, but the parent of that node is 3. – trincot Aug 04 '17 at 19:32
  • Yep, that corresponds to what the accepted answer described. – trincot Aug 05 '17 at 15:19
2

Here is a simpler version, your max-heap is A, and you want to delete the element at i position

DELETE(A, i)
  HEAP-INCREASE-KEY(A, i, A[0]+1)
  HEAP-EXTRACT-MAX(A)

Here is how it works, HEAP-INCREASE-KEY increase the value of the element you want to delete in order to be greater than the root, this will bring the element to the root of the heap, calling then the HEAP-EXTRACT-MAX, the root (the element you want to delete) is deleted (and returned) from the heap

E. Bekas
  • 21
  • 2
0

Not sure if it's a typo, but it looks like you actually increment here:

A.heapsize-=-1

And if A is actually a "property" of the array A, I don't think that you can even change it, but I could be wrong. Of course, if it's just some variable you have, it's fine.

Other than that, your pseudocode at least seems to be in working order. If it's still not working, maybe post the entire code?

logitropicity
  • 105
  • 1
  • 7
  • It was a typo. It had to be 1. I mean, the heap property that the parent should always be greater than its children. Executing the first code with the delete operation, this property is being violated right? – sagar_jeevan Jul 15 '16 at 19:00