0

I'm trying to implement the binary heap. I've successfully implemented display() and insert() functions.

Problem : Inside deleteHeap() function

Approach 1.Copy the heap's root into a variable

2.Now copy the heap's last element into the root.Decrement the heap node count n

3.Now check if the property of heap is violated.

4.Start from top.Mark root node as parent node. Check if any of the children are greater than the parent node.

5.Swap the largest child with parent and mark this child as new parent.

6.If parent_index<=n/2 return;

7.Else goto step 5.

Note The program is behaving anomalously while running. I recommend you to run it once.!

**Code**

#include<stdio.h>
#include<stdlib.h>
int a[100],n=0;
void display()
{
    int i=1;
    printf("\nThe heap contains - ");
    while(i<=n)
        printf("%d ",a[i++]);

}
void fixheap1()
{
    int j=n,temp;//Child's index
    int i=n/2;//Parent's index
    while(a[j]>a[i] && i!=0)
    {
        temp=a[j];
        a[j]=a[i];
        a[i]=temp;

        i=i/2;
        j=j/2;
    }
}
void insert(int key)
{
    a[++n]=key;
    fixheap1();
}
int findGreatest(int j,int k)
{
    if(a[j]>a[k])
        return j;
    else 
        return k;
}
void fixheap2()
{
    int i=1,j,k,temp,max;
    while(i<=n/2)
    {
        j=2i;
        k=2i+1;
        if(k>n && j<=n)
            max=j;
        else
            if(j>n)
                break;
        else
            max=findGreatest(j,k);

        temp=a[max];
        a[max]=a[i];
        a[i]=temp;

        i=max;
    }
}
void deleteHeap()
{
    int key=a[1];
    a[1]=a[n];
    n--;
    fixheap2();
    printf("\nElement successfully deleted %d",key);
}
int main()
{

    int i,choice=0,key;
    while(choice!=5)
    {
        system("clear");
        printf("\n\t\tHeaps\n1.Insert into heap\n2.Display heap\n3.Heap sort\n4.Delete from heap\n5.Exit\n\nChoice - ");
        scanf("%d",&choice);

        switch(choice)
        {
            case 1: printf("\nEnter the number of elements to be inserted - ");
                scanf("%d",&i);
                printf("\nEnter the elements - ");
                while(i--)
                {
                    scanf("%d",&key);
                    insert(key);
                }
                printf("\nElements successfully inserted");
                break;
            case 2: display();
                break;
            case 3:
                break;
            case 4: deleteHeap();
                break;
        }

        getchar();
        getchar();
    }
    printf("\n\nThank You\n\n");
}

Edit 1: After someone pointed out the mistake I've updated my function

void fixheap2()
{
    int i=1,j,k,temp,max;
    while(i<=n/2)
    {
        j=2i;
        k=2i+1;
        if(k>n && j<=n) //k does not exists
            max=j;
        else
            if(j>n) //j also doesn't exist
                break;
        else
            max=findGreatest(j,k);  //find the index of greater
        if(a[max]>a[i]) //if the child is greater than root
        {
            temp=a[max];
            a[max]=a[i];
            a[i]=temp;
            i=max;
        }
        else
            break;
    }
}

Sample Input and Output :

        Heaps
1.Insert into heap
2.Display heap
3.Heap sort
4.Delete from heap
5.Exit

Choice - 1

Enter the number of elements to be inserted - 6

Enter the elements - 6 5 4 3 2 1

Elements successfully inserted


        Heaps
1.Insert into heap
2.Display heap
3.Heap sort
4.Delete from heap
5.Exit

Choice - 2

The heap contains - 6 5 4 3 2 1 


        Heaps
1.Insert into heap
2.Display heap
3.Heap sort
4.Delete from heap
5.Exit

Choice - 4

Element successfully deleted 6


        Heaps
1.Insert into heap
2.Display heap
3.Heap sort
4.Delete from heap
5.Exit

Choice - 2

The heap contains - 1 5 4 3 2 


        Heaps
1.Insert into heap
2.Display heap
3.Heap sort
4.Delete from heap
5.Exit

Choice - 4

Element successfully deleted 1
Nitin Pandey
  • 649
  • 1
  • 9
  • 27

2 Answers2

2

Your fixheap2 fails to consider the case where both children are valid but have keys smaller than the parent's. In that situation, the heap property has been restored, and you should stop swapping with children (or you'll re-invalidate it).

Sneftel
  • 40,271
  • 12
  • 71
  • 104
  • I added that condition but still the code is not working properly. It shows correct output for the first delete but after that it fails. – Nitin Pandey Feb 16 '15 at 18:52
0

First mistake has been pointed out above by @Sneftel

Alright! I noticed there is a small error in the program which compiler ignores and is not noticed easily.

In EDIT 1 Section

j=2i
k=2i+1

Apparently the 2i is not evaluated to 2*i which is obvious as we are coding in c but, what's interesting is it's evaluated to zero.

It is a silly mistake though.

So if i have a program

int main()
{
    int i=21,j,k;
    j=2i+1;
    k=2i;
    printf("\n%d\n%d\n",j,k);
}

Output will be

1
0
Nitin Pandey
  • 649
  • 1
  • 9
  • 27