2

Can someone please explain the concept for deleting the i-th element in the min-heap that is represented by an array and maintain the heap property after deletion operation.

Left child of i-th node: 2*i + 1

Right child of i-th node: 2*i + 2

Parent of i-th node: (i-1)/2

That's how I tried, but this doesn't handles all the conditions properly:

void deleteKey(int i)
{
  if(i > capacity && i < 0)  //capacity : max size of heap
     return;

  heap_size--;               //current heap size

  //swapping last & required elements
  harr[heap_size] = harr[heap_size] ^ harr[i];  //harr[] : heap array
  harr[i] = harr[heap_size] ^ harr[i];     
  harr[heap_size] = harr[heap_size] ^ harr[i];

  int j = heap_size - 1;

  while(2*i <= j)
  {
     if(left(i)<= j)  //if there's only left node
     {
        if(right(i) <= j)  //if there is right too
        {
           //finds index with min value
           int x = harr[left(i)] < harr[right(i)] ? left(i) : right(i);

           //swaps array elements
           swap(&harr[x] , &harr[i]);

           //updating current & required node
           i = x;
        }

        else
        {
           swap(&harr[left(i)], &harr[i]);
           i = left(i); //updating current & required node
        }
     }
  }
}
6
  • 1
    " 2*1 + 1" is "2 * i + 1 " ? Commented Aug 14, 2017 at 13:46
  • 2
    Can someone please explain the concept for deleting the i-th element in the min-heap that is represented by an array and maintain the heap property after deletion operation -- You don't know a concept, yet you wrote a program implementing a concept you want explained. Confusing... Commented Aug 14, 2017 at 14:37
  • @PaulMcKenzie thanks for appreciating my efforts. Yeah, I am still learning. and when I am done with this code, I will let you know :) Commented Aug 14, 2017 at 16:26
  • Array index are used inorder to backtrack Commented Aug 17, 2017 at 3:29
  • what is the sample input you tried and how did you reach to conclusion it didn't work Commented Aug 17, 2017 at 3:30

2 Answers 2

1

This is the most beautiful explanation I came through while strolling. Seekers, take a look!

Sign up to request clarification or add additional context in comments.

2 Comments

Link-only answers are discouraged. The link might become stale. You should consider adding text that outlines the solution, and provide the link as the source, so that others can visit it if they need to.
Link isn't working.
0

Here a really good solution is available. Just check the delete operation of min heap.

http://www.geeksforgeeks.org/binary-heap/

3 Comments

what was the bug you found?
Actually, there were few cases which my algorithm wasn't able to handle. I found an amazing explaination & it worked beautifully.
That's not the best way to do it. See my link in comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.